From 520ed8f6387e0744a21a52912418e3acb08d18a5 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sun, 3 Sep 2023 19:18:27 +0530 Subject: different brands and items for different users --- item/controller.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 3 deletions(-) (limited to 'item/controller.go') diff --git a/item/controller.go b/item/controller.go index b4e27c1..cf9683d 100644 --- a/item/controller.go +++ b/item/controller.go @@ -31,8 +31,17 @@ func handleGetBrandItems (ctx *gin.Context) { return } + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + var items []SavedItem - err = getBrandItems(&items, uint(id)) + err = getBrandItems(&items, uint(id), userId) if err != nil { ctx.Error(err) ctx.Abort() @@ -48,7 +57,16 @@ func handleGetBrandItems (ctx *gin.Context) { func handleGetBrands (ctx *gin.Context) { var brands []Brand - err := getBrands(&brands) + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + err := getBrands(&brands, userId) if err != nil { ctx.Error(err) ctx.Abort() @@ -65,6 +83,16 @@ func handleSaveBrand (ctx *gin.Context) { var brand Brand ctx.Bind(&brand) + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + brand.UserID = userId + err := brand.upsert() if err != nil { ctx.Error(err) @@ -88,6 +116,16 @@ func handleDelBrand (ctx *gin.Context) { var brand Brand brand.ID = uint(id) + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + brand.UserID = userId + err = brand.del() if err != nil { ctx.Error(err) @@ -103,7 +141,16 @@ func handleDelBrand (ctx *gin.Context) { func handleGetItems (ctx *gin.Context) { var items []SavedItem - err := getItems(&items) + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + err := getItems(&items, userId) if err != nil { ctx.Error(err) ctx.Abort() @@ -120,6 +167,16 @@ func handleSaveItem (ctx *gin.Context) { var item SavedItem ctx.Bind(&item) + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + item.UserID = userId + err := item.upsert() if err != nil { ctx.Error(err) @@ -143,6 +200,16 @@ func handleDelItem (ctx *gin.Context) { var item SavedItem item.ID = uint(id) + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + item.UserID = userId + err = item.del() if err != nil { ctx.Error(err) -- cgit v1.2.3