diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-11 21:06:36 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-11 21:06:36 +0530 |
commit | 997b038761b8dd606e4041d8c8dc8bbf6f036033 (patch) | |
tree | 156bab13e8c2566e67df43189a612b7cebccda14 | |
parent | 99aee928a64649be75c4698b8e856e3ea05bc695 (diff) |
Removed Brand and added BrandName and Category as item fieldsv0.18.0
-rw-r--r-- | item/controller.go | 71 | ||||
-rw-r--r-- | item/hooks.go | 25 | ||||
-rw-r--r-- | item/item.go | 11 | ||||
-rw-r--r-- | item/router.go | 8 | ||||
-rw-r--r-- | item/service.go | 55 | ||||
-rw-r--r-- | item/validators.go | 48 | ||||
-rw-r--r-- | main.go | 2 |
7 files changed, 10 insertions, 210 deletions
diff --git a/item/controller.go b/item/controller.go index bb2eb94..b68f2aa 100644 --- a/item/controller.go +++ b/item/controller.go @@ -24,77 +24,6 @@ import ( "strconv" ) -func handleGetBrandItems (ctx *gin.Context) { - id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) - if err != nil { - ctx.Error(e.ErrInvalidID) - return - } - - var items []SavedItem - err = getBrandItems(&items, uint(id)) - if err != nil { - ctx.Error(err) - ctx.Abort() - return - } - - ctx.JSON(http.StatusOK, gin.H{ - "data": items, - }) -} - -func handleGetBrands (ctx *gin.Context) { - var brands []Brand - - err := getBrands(&brands) - if err != nil { - ctx.Error(err) - ctx.Abort() - return - } - - ctx.JSON(http.StatusOK, gin.H{ - "data": brands, - }) -} - -func handleSaveBrand (ctx *gin.Context) { - var brand Brand - ctx.Bind(&brand) - - err := brand.upsert() - if err != nil { - ctx.Error(err) - ctx.Abort() - return - } - - ctx.JSON(http.StatusOK, gin.H{ - "data": brand, - }) -} - -func handleDelBrand (ctx *gin.Context) { - id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) - if err != nil { - ctx.Error(e.ErrInvalidID) - return - } - - var brand Brand - brand.ID = uint(id) - - err = brand.del() - if err != nil { - ctx.Error(err) - ctx.Abort() - return - } - - ctx.JSON(http.StatusOK, nil) -} - func handleGetItems (ctx *gin.Context) { var items []SavedItem diff --git a/item/hooks.go b/item/hooks.go index 5056cb1..cd8d47c 100644 --- a/item/hooks.go +++ b/item/hooks.go @@ -19,7 +19,6 @@ package item import ( "gorm.io/gorm" - "vidhukant.com/openbills/errors" ) func (i *SavedItem) BeforeSave(tx *gorm.DB) error { @@ -32,27 +31,3 @@ func (i *SavedItem) BeforeSave(tx *gorm.DB) error { return nil } - -func (b *Brand) BeforeSave(tx *gorm.DB) error { - err := b.validate() - if err != nil { - return err - } - - return nil -} - -func (b *Brand) BeforeDelete(tx *gorm.DB) error { - // if ID is 0, brand won't be deleted - if b.ID == 0 { - return errors.ErrNoWhereCondition - } - - // delete all items - err := db.Where("brand_id = ?", b.ID).Delete(&SavedItem{}).Error - if err != nil { - return err - } - - return nil -} diff --git a/item/item.go b/item/item.go index 617a662..3a6235f 100644 --- a/item/item.go +++ b/item/item.go @@ -26,16 +26,13 @@ var db *gorm.DB func init() { db = d.DB - db.AutoMigrate(&SavedItem{}, &Brand{}) -} - -type Brand struct { - gorm.Model - Name string + db.AutoMigrate(&SavedItem{}) } type Item struct { Name string + BrandName string + Category string Description string HSN string UnitOfMeasure string @@ -46,6 +43,4 @@ type Item struct { type SavedItem struct { gorm.Model Item - BrandID uint - Brand Brand } diff --git a/item/router.go b/item/router.go index b953d09..a8a3f16 100644 --- a/item/router.go +++ b/item/router.go @@ -22,14 +22,6 @@ import ( ) func Routes(route *gin.RouterGroup) { - b := route.Group("/brand") - { - b.GET("/", handleGetBrands) - b.GET("/:id/items", handleGetBrandItems) - b.POST("/", handleSaveBrand) - b.DELETE("/:id", handleDelBrand) - } - i := route.Group("/item") { i.GET("/", handleGetItems) diff --git a/item/service.go b/item/service.go index 0b2afaf..b7d3490 100644 --- a/item/service.go +++ b/item/service.go @@ -21,61 +21,8 @@ import ( "vidhukant.com/openbills/errors" ) -func getBrandItems(items *[]SavedItem, id uint) error { - // get items - res := db.Model(&SavedItem{}).Where("brand_id = ?", id).Find(&items) - - // TODO: handle potential errors - if res.Error != nil { - return res.Error - } - - if res.RowsAffected == 0 { - return errors.ErrEmptyResponse - } - - return nil -} - -func getBrands(brands *[]Brand) error { - res := db.Find(&brands) - - // TODO: handle potential errors - if res.Error != nil { - return res.Error - } - - if res.RowsAffected == 0 { - return errors.ErrEmptyResponse - } - - return nil -} - -func (b *Brand) upsert() error { - res := db.Save(b) - // TODO: handle potential errors - return res.Error -} - -func (b *Brand) del() error { - // delete brand - res := db.Where("id = ?", b.ID).Delete(b) - - // TODO: handle potential errors - if res.Error != nil { - return res.Error - } - - if res.RowsAffected == 0 { - return errors.ErrNotFound - } - - return nil -} - func getItems(items *[]SavedItem) error { - res := db.Preload("Brand").Find(&items) + res := db.Find(&items) // TODO: handle potential errors if res.Error != nil { diff --git a/item/validators.go b/item/validators.go index 63c254f..d098f4e 100644 --- a/item/validators.go +++ b/item/validators.go @@ -24,38 +24,16 @@ import ( "vidhukant.com/openbills/errors" ) -func (b *Brand) validate() error { - // trim whitespaces - b.Name = strings.TrimSpace(b.Name) - - if b.Name == "" { - return errors.ErrEmptyBrandName - } - - // make sure brand name is unique - var count int64 - err := db.Model(&Brand{}). - Select("name"). - Where("name = ?", b.Name). - Count(&count). - Error - - if err != nil { - return err - } - - if count > 0 { - return errors.ErrNonUniqueBrandName - } - - return nil -} - func (i *SavedItem) validate() error { // trim whitespaces i.Name = strings.TrimSpace(i.Name) + i.BrandName = strings.TrimSpace(i.BrandName) + i.Category = strings.TrimSpace(i.Category) i.Description = strings.TrimSpace(i.Description) i.HSN = strings.TrimSpace(i.HSN) + i.UnitOfMeasure = strings.TrimSpace(i.UnitOfMeasure) + i.UnitPrice = strings.TrimSpace(i.UnitPrice) + i.GSTPercentage = strings.TrimSpace(i.GSTPercentage) var err error @@ -71,21 +49,5 @@ func (i *SavedItem) validate() error { return errors.ErrInvalidGSTPercentage } - // check if item with same name and brand already exists - var count int64 - err = db.Model(&SavedItem{}). - Select("name, brand_id"). - Where("brand_id = ? and name = ?", i.BrandID, i.Name). - Count(&count). - Error - - if err != nil { - return err - } - - if count != 0 { - return errors.ErrNonUniqueBrandItem - } - return nil } @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.17.2" +const OPENBILLS_VERSION = "v0.18.0" func init() { if !viper.GetBool("debug_mode") { |