aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 23:39:05 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.com>2023-09-03 23:39:05 +0530
commitcba9f19ba50c60576a5961e89c24caefca52d740 (patch)
tree5dd36d4903c640e11a022c5fbf0e55adc3cdd5a5
parent46083ff15e16b0b49132af9466bcab7c2ae71322 (diff)
deleting addresses upon customer deletionv0.0.5
-rw-r--r--customer/customer.go6
-rw-r--r--customer/hooks.go14
-rw-r--r--customer/service.go2
-rw-r--r--item/hooks.go5
-rw-r--r--main.go2
5 files changed, 24 insertions, 5 deletions
diff --git a/customer/customer.go b/customer/customer.go
index e411ad5..1a5d6f1 100644
--- a/customer/customer.go
+++ b/customer/customer.go
@@ -39,11 +39,13 @@ type Address struct {
}
type CustomerBillingAddress struct {
+ gorm.Model
Address
CustomerID uint
}
type CustomerShippingAddress struct {
+ gorm.Model
Address
CustomerID uint
}
@@ -58,6 +60,6 @@ type Customer struct {
Phone string
Email string
Website string
- //BillingAddress CustomerBillingAddress
- //ShippingAddresses []CustomerShippingAddress
+ BillingAddress CustomerBillingAddress
+ ShippingAddresses []CustomerShippingAddress
}
diff --git a/customer/hooks.go b/customer/hooks.go
index ac246f3..bef3308 100644
--- a/customer/hooks.go
+++ b/customer/hooks.go
@@ -39,5 +39,19 @@ func (c *Customer) BeforeDelete(tx *gorm.DB) error {
return errors.ErrNoWhereCondition
}
+ var err error
+
+ // delete billing address
+ err = db.Where("customer_id = ?", c.ID).Delete(&CustomerBillingAddress{}).Error
+ if err != nil {
+ return err
+ }
+
+ // delete shipping addresses
+ err = db.Where("customer_id = ?", c.ID).Delete(&CustomerShippingAddress{}).Error
+ if err != nil {
+ return err
+ }
+
return nil
}
diff --git a/customer/service.go b/customer/service.go
index f1108c6..a79e466 100644
--- a/customer/service.go
+++ b/customer/service.go
@@ -22,7 +22,7 @@ import (
)
func getCustomer(customer *Customer, id uint) error {
- res := db.Preload("Contact").Preload("BillingAddress").Preload("ShippingAddresses").Find(&customer, id)
+ res := db.Preload("BillingAddress").Preload("ShippingAddresses").Find(&customer, id)
// TODO: handle potential errors
if res.Error != nil {
diff --git a/item/hooks.go b/item/hooks.go
index c7dc498..74b6860 100644
--- a/item/hooks.go
+++ b/item/hooks.go
@@ -60,7 +60,10 @@ func (b *Brand) BeforeDelete(tx *gorm.DB) error {
}
// delete all items
- db.Where("brand_id = ? and user_id = ?", b.ID, b.UserID).Delete(&SavedItem{})
+ err := db.Where("brand_id = ? and user_id = ?", b.ID, b.UserID).Delete(&SavedItem{}).Error
+ if err != nil {
+ return err
+ }
return nil
}
diff --git a/main.go b/main.go
index 2ec24dc..5dda554 100644
--- a/main.go
+++ b/main.go
@@ -37,7 +37,7 @@ import (
"log"
)
-const OPENBILLS_VERSION = "v0.0.4"
+const OPENBILLS_VERSION = "v0.0.5"
func init() {
if viper.GetBool("production_mode") {