aboutsummaryrefslogtreecommitdiff
path: root/item/controller.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 19:18:27 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 19:18:27 +0530
commit520ed8f6387e0744a21a52912418e3acb08d18a5 (patch)
tree7865e20c8e1828d7b1a7d513b08dc282b291aa05 /item/controller.go
parent020fc720379255832aad369dfc2dece9a1cf4699 (diff)
different brands and items for different users
Diffstat (limited to 'item/controller.go')
-rw-r--r--item/controller.go73
1 files changed, 70 insertions, 3 deletions
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)