From 3637ed7f0a50e36f3acfbacc0f81bb7141e7a2d4 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 25 Sep 2022 16:11:32 +0530 Subject: added GET routes --- brand/brand_router.go | 15 +++++++++++++-- client/client_router.go | 15 +++++++++++++-- go.mod | 2 +- go.sum | 4 ++-- invoice/invoice_router.go | 45 +++++++++++++++++++++++++++++++++++++++------ item/item_router.go | 15 +++++++++++++-- 6 files changed, 81 insertions(+), 15 deletions(-) diff --git a/brand/brand_router.go b/brand/brand_router.go index 74432dd..c6aa661 100644 --- a/brand/brand_router.go +++ b/brand/brand_router.go @@ -11,13 +11,24 @@ import ( func Routes(route *gin.Engine) { b := route.Group("/brand") { + b.GET("/", func(ctx *gin.Context) { + // TODO: add functionality to filter results + brands, err := brand.GetBrands(nil) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to read brands from DB: %v\n", err.Error()) + } + + ctx.JSON(http.StatusOK, brands) + }) + b.POST("/", func(ctx *gin.Context) { var x brand.Brand ctx.Bind(&x) err := x.Save() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add new brand \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to add new brand \"%s\": %v\n", x.Name, err.Error()) } log.Println("Added new brand to database: ", x.Name) @@ -30,7 +41,7 @@ func Routes(route *gin.Engine) { err := x.Delete() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete brand \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to delete brand \"%s\": %v\n", x.Name, err.Error()) } log.Println("Delete brand: ", x.Name) diff --git a/client/client_router.go b/client/client_router.go index a36ec48..cc9e19d 100644 --- a/client/client_router.go +++ b/client/client_router.go @@ -10,13 +10,24 @@ import ( func Routes(route *gin.Engine) { c := route.Group("/client") { + c.GET("/", func(ctx *gin.Context) { + // TODO: add functionality to filter results + clients, err := client.GetClients(nil) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to read clients from DB: %v\n", err.Error()) + } + + ctx.JSON(http.StatusOK, clients) + }) + c.POST("/", func(ctx *gin.Context) { var x client.Client ctx.Bind(&x) err := x.Save() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add new client \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to add new client \"%s\": %v\n", x.Name, err.Error()) } log.Println("Added new client to database: ", x.Name) @@ -29,7 +40,7 @@ func Routes(route *gin.Engine) { err := x.Delete() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete client \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to delete client \"%s\": %v\n", x.Name, err.Error()) } log.Println("Deleted client: ", x.Name) diff --git a/go.mod b/go.mod index e05d315..ab696a9 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/MikunoNaka/OpenBills-server go 1.19 require ( - github.com/MikunoNaka/OpenBills-lib v0.0.1 + github.com/MikunoNaka/OpenBills-lib v0.0.2 github.com/gin-gonic/gin v1.8.1 ) diff --git a/go.sum b/go.sum index 108f850..7338d42 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/MikunoNaka/OpenBills-lib v0.0.1 h1:tJN2G4Hi75PahP8zWqymThZv3fize+zuw+fl99MmNsI= -github.com/MikunoNaka/OpenBills-lib v0.0.1/go.mod h1:uAM49uISC12jAgqstIgehBzSd9QBKUZDde6INRvLyGU= +github.com/MikunoNaka/OpenBills-lib v0.0.2 h1:65sqsQpxSd8IbaQpQmnKdKBjIY2CAzzYk6u5mi534X4= +github.com/MikunoNaka/OpenBills-lib v0.0.2/go.mod h1:uAM49uISC12jAgqstIgehBzSd9QBKUZDde6INRvLyGU= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= diff --git a/invoice/invoice_router.go b/invoice/invoice_router.go index d3f2d82..2752675 100644 --- a/invoice/invoice_router.go +++ b/invoice/invoice_router.go @@ -10,13 +10,24 @@ import ( func Routes(route *gin.Engine) { i := route.Group("/invoice") { + i.GET("/", func(ctx *gin.Context) { + // TODO: add functionality to filter results + invoices, err := invoice.GetInvoices(nil) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to read invoices from DB: %v\n", err.Error()) + } + + ctx.JSON(http.StatusOK, invoices) + }) + i.POST("/", func(ctx *gin.Context) { var x invoice.Invoice ctx.Bind(&x) err := x.Save() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to generate new invoice #%d: %v", x.InvoiceNumber, err.Error()) + log.Printf("ERROR: Failed to generate new invoice #%d: %v\n", x.InvoiceNumber, err.Error()) } log.Printf("Generated new invoice #%d.\n", x.InvoiceNumber) @@ -29,7 +40,7 @@ func Routes(route *gin.Engine) { err := x.Delete() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete invoice #%d: %v", x.InvoiceNumber, err.Error()) + log.Printf("ERROR: Failed to delete invoice #%d: %v\n", x.InvoiceNumber, err.Error()) } log.Printf("Deleted invoice invoice #%d.\n", x.InvoiceNumber) @@ -39,13 +50,24 @@ func Routes(route *gin.Engine) { transport := route.Group("/transport") { + transport.GET("/", func(ctx *gin.Context) { + // TODO: add functionality to filter results + transports, err := invoice.GetTransports(nil) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to read transport vehicles from DB: %v\n", err.Error()) + } + + ctx.JSON(http.StatusOK, transports) + }) + transport.POST("/", func(ctx *gin.Context) { var x invoice.Transport ctx.Bind(&x) err := x.Save() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add transport vehicle \"%s\": %v", x.VehicleNum, err.Error()) + log.Printf("ERROR: Failed to add transport vehicle \"%s\": %v\n", x.VehicleNum, err.Error()) } log.Printf("Added new transport vehicle to database: \"%s\"\n", x.VehicleNum) @@ -58,7 +80,7 @@ func Routes(route *gin.Engine) { err := x.Delete() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete transport vehicle \"%s\": %v", x.VehicleNum, err.Error()) + log.Printf("ERROR: Failed to delete transport vehicle \"%s\": %v\n", x.VehicleNum, err.Error()) } log.Printf("Deleted transport vehicle: \"%s\"\n", x.VehicleNum) @@ -68,13 +90,24 @@ func Routes(route *gin.Engine) { transporter := route.Group("/transporter") { + transporter.GET("/", func(ctx *gin.Context) { + // TODO: add functionality to filter results + transporters, err := invoice.GetTransporters(nil) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to read transporters from DB: %v\n", err.Error()) + } + + ctx.JSON(http.StatusOK, transporters) + }) + transporter.POST("/", func(ctx *gin.Context) { var x invoice.Transporter ctx.Bind(&x) err := x.Save() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add transporter \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to add transporter \"%s\": %v\n", x.Name, err.Error()) } log.Printf("Added new transporter to database: \"%s\"\n", x.Name) @@ -87,7 +120,7 @@ func Routes(route *gin.Engine) { err := x.Delete() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete transporter \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to delete transporter \"%s\": %v\n", x.Name, err.Error()) } log.Printf("Deleted transporter: \"%s\"\n", x.Name) diff --git a/item/item_router.go b/item/item_router.go index 1af42c0..f57dd2e 100644 --- a/item/item_router.go +++ b/item/item_router.go @@ -11,13 +11,24 @@ import ( func Routes(route *gin.Engine) { i := route.Group("/item") { + i.GET("/", func(ctx *gin.Context) { + // TODO: add functionality to filter results + items, err := item.GetItems(nil) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to read items from DB: %v\n", err.Error()) + } + + ctx.JSON(http.StatusOK, items) + }) + i.POST("/", func(ctx *gin.Context) { var x item.Item ctx.Bind(&x) err := x.Save() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to add new item \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to add new item \"%s\": %v\n", x.Name, err.Error()) } log.Println("Added new item to database: ", x.Name) @@ -30,7 +41,7 @@ func Routes(route *gin.Engine) { err := x.Delete() if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Printf("ERROR: Failed to delete item \"%s\": %v", x.Name, err.Error()) + log.Printf("ERROR: Failed to delete item \"%s\": %v\n", x.Name, err.Error()) } log.Println("Deleted item: ", x.Name) -- cgit v1.2.3