diff options
| author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-13 01:32:27 +0530 | 
|---|---|---|
| committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2025-10-13 01:32:27 +0530 | 
| commit | 0f435049cb3cc6900d881c5dce43bec3a1e60e2e (patch) | |
| tree | 374950fba5605ea8411da428ac94b1deef4e2809 /item/hooks.go | |
| parent | fb9ba155438100f295fdb563ad955151ee038ad3 (diff) | |
added item variantsv0.22.0
Diffstat (limited to 'item/hooks.go')
| -rw-r--r-- | item/hooks.go | 24 | 
1 files changed, 24 insertions, 0 deletions
diff --git a/item/hooks.go b/item/hooks.go index cd8d47c..e05a6cb 100644 --- a/item/hooks.go +++ b/item/hooks.go @@ -19,6 +19,7 @@ package item  import (  	"gorm.io/gorm" +	"vidhukant.com/openbills/errors"  )  func (i *SavedItem) BeforeSave(tx *gorm.DB) error { @@ -29,5 +30,28 @@ func (i *SavedItem) BeforeSave(tx *gorm.DB) error {  		return err  	} +	if i.ID != 0 { +		// delete all of this item's variants and save again +		err = db.Where("saved_item_id = ?", i.ID).Delete(&ItemVariant{}).Error +		if err != nil { +			return err +		} +	} + +	return nil +} + +func (i *SavedItem) BeforeDelete(tx *gorm.DB) error { +	// if ID is 0, item won't be deleted +	if i.ID == 0 { +		return errors.ErrNoWhereCondition +	} + +	// delete all of this item's variants before deleting this item +	err := db.Where("saved_item_id = ?", i.ID).Delete(&ItemVariant{}).Error +	if err != nil { +		return err +	} +  	return nil  }  |