Add BeforeSave hook to db.Payment
This commit is contained in:
parent
994f623f23
commit
e6d53c4d17
|
@ -1,6 +1,7 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
@ -23,3 +24,26 @@ type Payment struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p Payment) __internalBelogingToPayment() {}
|
func (p Payment) __internalBelogingToPayment() {}
|
||||||
|
|
||||||
|
var (
|
||||||
|
ERROR_PAYMENT_INVALID_CARD_USERID = errors.New("Payment's `UserID` and Card's `UserID` are not equal")
|
||||||
|
ERROR_PAYMENT_INVALID_CATEGORY_USERID = errors.New("Payment's `UserID` and Category's `UserID` are not equal")
|
||||||
|
)
|
||||||
|
|
||||||
|
func (p *Payment) BeforeSave(tx *gorm.DB) error {
|
||||||
|
paymentCard := &Card{}
|
||||||
|
if err := tx.Find(paymentCard, p.CardID).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if paymentCard.UserID != p.UserID {
|
||||||
|
return ERROR_PAYMENT_INVALID_CARD_USERID
|
||||||
|
}
|
||||||
|
paymentCategory := &Category{}
|
||||||
|
if err := tx.Find(paymentCategory, p.CategoryID).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if paymentCategory.UserID != p.UserID {
|
||||||
|
return ERROR_PAYMENT_INVALID_CATEGORY_USERID
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user