From d43b356fc302d61728a08f08dbdf474906e3fa82 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 27 Sep 2022 22:36:29 +0530 Subject: added some PUT request handlers --- brand/brand_router.go | 36 ++++++++++++++++++++++++++++++++++++ client/client_router.go | 36 ++++++++++++++++++++++++++++++++++++ item/item_router.go | 22 ++++++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/brand/brand_router.go b/brand/brand_router.go index 264c693..5d9a163 100644 --- a/brand/brand_router.go +++ b/brand/brand_router.go @@ -41,6 +41,42 @@ func Routes(route *gin.Engine) { ctx.JSON(http.StatusOK, brands) }) + b.POST("/new", func(ctx *gin.Context) { + var b brand.Brand + ctx.BindJSON(&b) + _, err := brand.SaveBrand(b) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to add new brand %v to DB: %v\n", b, err.Error()) + return + } + + log.Printf("Successfully saved new brand to DB: %v", b) + ctx.JSON(http.StatusOK, nil) + }) + + b.PUT("/:brandId", func(ctx *gin.Context) { + id := ctx.Param("brandId") + objectId, err := primitive.ObjectIDFromHex(id) + if err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to modify brand, Error parsing ID: %v\n", err.Error()) + return + } + + var b brand.Brand + ctx.BindJSON(&b) + err = brand.ModifyBrand(objectId, b) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to modify brand %v: %v\n", objectId, err.Error()) + return + } + + log.Printf("Modified brand %v to %v.\n", objectId, b) + ctx.JSON(http.StatusOK, nil) + }) + b.DELETE("/:brandId", func(ctx *gin.Context) { id := ctx.Param("brandId") objectId, err := primitive.ObjectIDFromHex(id) diff --git a/client/client_router.go b/client/client_router.go index 3011a8b..2fd14fc 100644 --- a/client/client_router.go +++ b/client/client_router.go @@ -40,6 +40,42 @@ func Routes(route *gin.Engine) { ctx.JSON(http.StatusOK, clients) }) + c.POST("/new", func(ctx *gin.Context) { + var c client.Client + ctx.BindJSON(&c) + _, err := client.SaveClient(c) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to add new client %v to DB: %v\n", c, err.Error()) + return + } + + log.Printf("Successfully saved new client to DB: %v", c) + ctx.JSON(http.StatusOK, nil) + }) + + c.PUT("/:clientId", func(ctx *gin.Context) { + id := ctx.Param("clientId") + objectId, err := primitive.ObjectIDFromHex(id) + if err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to modify client, Error parsing ID: %v\n", err.Error()) + return + } + + var c client.Client + ctx.BindJSON(&c) + err = client.ModifyClient(objectId, c) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to modify client %v: %v\n", objectId, err.Error()) + return + } + + log.Printf("Modified client %v to %v.\n", objectId, c) + ctx.JSON(http.StatusOK, nil) + }) + c.DELETE("/:clientId", func(ctx *gin.Context) { id := ctx.Param("clientId") objectId, err := primitive.ObjectIDFromHex(id) diff --git a/item/item_router.go b/item/item_router.go index cc58793..d86af68 100644 --- a/item/item_router.go +++ b/item/item_router.go @@ -55,6 +55,28 @@ func Routes(route *gin.Engine) { ctx.JSON(http.StatusOK, nil) }) + i.PUT("/:itemId", func(ctx *gin.Context) { + id := ctx.Param("itemId") + objectId, err := primitive.ObjectIDFromHex(id) + if err != nil { + ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to modify item, Error parsing ID: %v\n", err.Error()) + return + } + + var i item.Item + ctx.BindJSON(&i) + err = item.ModifyItem(objectId, i) + if err != nil { + ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + log.Printf("ERROR: Failed to modify item %v: %v\n", objectId, err.Error()) + return + } + + log.Printf("Modified item %v to %v.\n", objectId, i) + ctx.JSON(http.StatusOK, nil) + }) + i.DELETE("/:itemId", func(ctx *gin.Context) { id := ctx.Param("itemId") objectId, err := primitive.ObjectIDFromHex(id) -- cgit v1.2.3