diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-09-27 22:36:29 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-09-27 22:36:29 +0530 |
commit | d43b356fc302d61728a08f08dbdf474906e3fa82 (patch) | |
tree | 7c54e49bb86820f83b152f5d2e5034accc9e7287 /brand | |
parent | 019fb13d4ea1ce9f905edbacff39717892af01dc (diff) |
added some PUT request handlers
Diffstat (limited to 'brand')
-rw-r--r-- | brand/brand_router.go | 36 |
1 files changed, 36 insertions, 0 deletions
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) |