diff options
| author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-11 20:55:48 +0530 | 
|---|---|---|
| committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-11 20:55:48 +0530 | 
| commit | bc154857fb5569d7c1fa9785cc891cb927a6a156 (patch) | |
| tree | 590c9f6a00a1b97b2ee45cfa5a767558089affe0 /item | |
| parent | 8a47978ca17d2f251d67d12b0e34fa26bb1e4ace (diff) | |
removed per-user itemsv0.17.0
Diffstat (limited to 'item')
| -rw-r--r-- | item/controller.go | 101 | ||||
| -rw-r--r-- | item/hooks.go | 13 | ||||
| -rw-r--r-- | item/item.go | 7 | ||||
| -rw-r--r-- | item/router.go | 1 | ||||
| -rw-r--r-- | item/service.go | 22 | ||||
| -rw-r--r-- | item/validators.go | 58 | 
6 files changed, 17 insertions, 185 deletions
diff --git a/item/controller.go b/item/controller.go index 9993688..bb2eb94 100644 --- a/item/controller.go +++ b/item/controller.go @@ -31,17 +31,8 @@ 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), userId) +	err = getBrandItems(&items, uint(id))  	if err != nil {  		ctx.Error(err)  		ctx.Abort() @@ -49,7 +40,6 @@ func handleGetBrandItems (ctx *gin.Context) {  	}  	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success",  		"data": items,  	})  } @@ -57,16 +47,7 @@ func handleGetBrandItems (ctx *gin.Context) {  func handleGetBrands (ctx *gin.Context) {  	var brands []Brand -	uId, ok := ctx.Get("UserID") -	if !ok { -		ctx.Error(e.ErrUnauthorized) -		ctx.Abort() -		return -	} - -	userId := uId.(uint) - -	err := getBrands(&brands, userId) +	err := getBrands(&brands)  	if err != nil {  		ctx.Error(err)  		ctx.Abort() @@ -74,7 +55,6 @@ func handleGetBrands (ctx *gin.Context) {  	}  	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success",  		"data": brands,  	})  } @@ -83,16 +63,6 @@ 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) @@ -101,7 +71,6 @@ func handleSaveBrand (ctx *gin.Context) {  	}  	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success",  		"data": brand,  	})  } @@ -116,24 +85,6 @@ 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 = checkBrandOwnership(brand.ID, brand.UserID) -	if err != nil { -		ctx.Error(err) -		ctx.Abort() -		return -	} -  	err = brand.del()  	if err != nil {  		ctx.Error(err) @@ -141,24 +92,13 @@ func handleDelBrand (ctx *gin.Context) {  		return  	} -	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success", -	}) +	ctx.JSON(http.StatusOK, nil)  }  func handleGetItems (ctx *gin.Context) {  	var items []SavedItem -	uId, ok := ctx.Get("UserID") -	if !ok { -		ctx.Error(e.ErrUnauthorized) -		ctx.Abort() -		return -	} - -	userId := uId.(uint) - -	err := getItems(&items, userId) +	err := getItems(&items)  	if err != nil {  		ctx.Error(err)  		ctx.Abort() @@ -166,7 +106,6 @@ func handleGetItems (ctx *gin.Context) {  	}  	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success",  		"data": items,  	})  } @@ -175,16 +114,6 @@ 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) @@ -193,7 +122,6 @@ func handleSaveItem (ctx *gin.Context) {  	}  	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success",  		"data": item,  	})  } @@ -208,23 +136,6 @@ 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 = checkItemOwnership(item.ID, item.UserID) -	if err != nil { -		ctx.Error(err) -		ctx.Abort() -		return -	} -  	err = item.del()  	if err != nil {  		ctx.Error(err) @@ -232,7 +143,5 @@ func handleDelItem (ctx *gin.Context) {  		return  	} -	ctx.JSON(http.StatusOK, gin.H{ -		"message": "success", -	}) +	ctx.JSON(http.StatusOK, nil)  } diff --git a/item/hooks.go b/item/hooks.go index 74b6860..5056cb1 100644 --- a/item/hooks.go +++ b/item/hooks.go @@ -20,22 +20,11 @@ package item  import (  	"gorm.io/gorm"  	"vidhukant.com/openbills/errors" -	e "errors"  )  func (i *SavedItem) BeforeSave(tx *gorm.DB) error {  	var err error -	// also checks if brand actually exists -	err = checkBrandOwnership(i.BrandID, i.UserID) -	if err != nil { -		if e.Is(err, errors.ErrBrandNotFound) { -			// this error has a better error message for this case -			return errors.ErrBrandNotFound -		} -		return err -	} -  	err = i.validate()  	if err != nil {  		return err @@ -60,7 +49,7 @@ func (b *Brand) BeforeDelete(tx *gorm.DB) error {  	}  	// delete all items -	err := db.Where("brand_id = ? and user_id = ?", b.ID, b.UserID).Delete(&SavedItem{}).Error +	err := db.Where("brand_id = ?", b.ID).Delete(&SavedItem{}).Error  	if err != nil {  		return err  	} diff --git a/item/item.go b/item/item.go index 3f911fa..617a662 100644 --- a/item/item.go +++ b/item/item.go @@ -20,7 +20,6 @@ package item  import (  	"gorm.io/gorm"  	d "vidhukant.com/openbills/db" -	"vidhukant.com/openbills/user"  )  var db *gorm.DB @@ -32,8 +31,6 @@ func init() {  type Brand struct {  	gorm.Model -	UserID uint      `json:"-"` -	User   user.User `json:"-"`  	Name   string  } @@ -41,7 +38,7 @@ type Item struct {  	Name          string  	Description   string  	HSN           string -	UnitOfMeasure string // TODO: probably has to be a custom type +	UnitOfMeasure string  	UnitPrice     string // float  	GSTPercentage string // float  } @@ -51,6 +48,4 @@ type SavedItem struct {  	Item  	BrandID            uint  	Brand              Brand -	UserID             uint      `json:"-"` -	User               user.User `json:"-"`  } diff --git a/item/router.go b/item/router.go index fab973f..b953d09 100644 --- a/item/router.go +++ b/item/router.go @@ -33,7 +33,6 @@ func Routes(route *gin.RouterGroup) {  	i := route.Group("/item")  	{  		i.GET("/", handleGetItems) -		//i.GET("/:id", handleGetBrandItems)  		i.POST("/", handleSaveItem)  		i.DELETE("/:id", handleDelItem)  	} diff --git a/item/service.go b/item/service.go index 80faff0..0b2afaf 100644 --- a/item/service.go +++ b/item/service.go @@ -21,12 +21,7 @@ import (  	"vidhukant.com/openbills/errors"  ) -func getBrandItems(items *[]SavedItem, id, userId uint) error { -	err := checkBrandOwnership(id, userId) -	if err != nil { -		return err -	} - +func getBrandItems(items *[]SavedItem, id uint) error {  	// get items  	res := db.Model(&SavedItem{}).Where("brand_id = ?", id).Find(&items) @@ -35,7 +30,6 @@ func getBrandItems(items *[]SavedItem, id, userId uint) error {  		return res.Error  	} -	// returns 404 if either row doesn't exist or if the user doesn't own it  	if res.RowsAffected == 0 {  		return errors.ErrEmptyResponse  	} @@ -43,8 +37,8 @@ func getBrandItems(items *[]SavedItem, id, userId uint) error {  	return nil  } -func getBrands(brands *[]Brand, userId uint) error { -	res := db.Where("user_id = ?", userId).Find(&brands) +func getBrands(brands *[]Brand) error { +	res := db.Find(&brands)  	// TODO: handle potential errors  	if res.Error != nil { @@ -66,14 +60,13 @@ func (b *Brand) upsert() error {  func (b *Brand) del() error {  	// delete brand -	res := db.Where("id = ? and user_id = ?", b.ID, b.UserID).Delete(b) +	res := db.Where("id = ?", b.ID).Delete(b)  	// TODO: handle potential errors  	if res.Error != nil {  		return res.Error  	} -	// returns 404 if either row doesn't exist or if the user doesn't own it  	if res.RowsAffected == 0 {  		return errors.ErrNotFound  	} @@ -81,8 +74,8 @@ func (b *Brand) del() error {  	return nil  } -func getItems(items *[]SavedItem, userId uint) error { -	res := db.Where("user_id = ?", userId).Preload("Brand").Find(&items) +func getItems(items *[]SavedItem) error { +	res := db.Preload("Brand").Find(&items)  	// TODO: handle potential errors  	if res.Error != nil { @@ -103,14 +96,13 @@ func (i *SavedItem) upsert() error {  }  func (i *SavedItem) del() error { -	res := db.Where("id = ? and user_id = ?", i.ID, i.UserID).Delete(i) +	res := db.Where("id = ?", i.ID).Delete(i)  	// TODO: handle potential errors  	if res.Error != nil {  		return res.Error  	} -	// returns 404 if either row doesn't exist or if the user doesn't own it  	if res.RowsAffected == 0 {  		return errors.ErrNotFound  	} diff --git a/item/validators.go b/item/validators.go index b808ae4..63c254f 100644 --- a/item/validators.go +++ b/item/validators.go @@ -32,11 +32,11 @@ func (b *Brand) validate() error {  		return errors.ErrEmptyBrandName  	} -	// make sure GSTIN is unique +	// make sure brand name is unique  	var count int64  	err := db.Model(&Brand{}).  		Select("name"). -		Where("name = ? and user_id = ?", b.Name, b.UserID). +		Where("name = ?", b.Name).  		Count(&count).  		Error @@ -75,7 +75,7 @@ func (i *SavedItem) validate() error {  	var count int64  	err = db.Model(&SavedItem{}).  		Select("name, brand_id"). -		Where("brand_id = ? and name = ? and user_id = ?", i.BrandID, i.Name, i.UserID). +		Where("brand_id = ? and name = ?", i.BrandID, i.Name).  		Count(&count).  		Error @@ -89,55 +89,3 @@ func (i *SavedItem) validate() error {  	return nil  } - -func checkBrandOwnership(brandId, userId uint) error { -	var brand Brand -	err := db. -		Select("id", "user_id"). -		Where("id = ?", brandId). -		Find(&brand). -		Error - -	// TODO: handle potential errors -	if err != nil { -		return err -  } - -	// brand doesn't exist -	if brand.ID == 0 { -		return errors.ErrNotFound -	} - -	// user doesn't own this brand -	if brand.UserID != userId { -		return errors.ErrForbidden -	} - -	return nil -} - -func checkItemOwnership(itemId, userId uint) error { -	var item SavedItem -	err := db. -		Select("id", "user_id"). -		Where("id = ?", itemId). -		Find(&item). -		Error - -	// TODO: handle potential errors -	if err != nil { -		return err -  } - -	// item doesn't exist -	if item.ID == 0 { -		return errors.ErrNotFound -	} - -	// user doesn't own this item -	if item.UserID != userId { -		return errors.ErrForbidden -	} - -	return nil -}  |