package fertilizing import ( "wagfarm-api/assets/fertilizer" "wagfarm-api/database" "wagfarm-api/logs/generic" "github.com/shopspring/decimal" "gorm.io/gorm" ) type FertilizingLog struct { generic.BaseExtendedLog FertilizerID uint `gorm:"not null"` Fertilizer fertilizer.Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` Amount float64 `gorm:"not null"` // in units of the fertilizer } func (l *FertilizingLog) AfterSave(db *gorm.DB) (err error) { // Example: compute cost as Amount * UnitPrice if l.FertilizerID == 0 { if err := db.First(&l.Fertilizer, l.FertilizerID).Error; err != nil { return err } } unitPrice := l.Fertilizer.Cost totalCost := unitPrice.Mul(decimal.NewFromFloat(l.Amount)) l.UpdateCost(db, totalCost) return } func RegisterModels() { database.RegisterModel(&FertilizingLog{}) }