aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/item/hooks.go
diff options
context:
space:
mode:
Diffstat (limited to 'item/hooks.go')
-rw-r--r--item/hooks.go24
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
}