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 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'brand/brand_router.go') 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) -- cgit v1.2.3