datamodel done without validators
This commit is contained in:
36
wagfarm-api/logs/cropprotection/model.go
Normal file
36
wagfarm-api/logs/cropprotection/model.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package cropprotection
|
||||
|
||||
import (
|
||||
"wagfarm-api/assets/pesticide"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/logs/base"
|
||||
"wagfarm-api/taxonomy/worker"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type CropProtectionLog struct {
|
||||
base.FieldWorkLog
|
||||
PesticideID uint `gorm:"not null"`
|
||||
Pesticide pesticide.Pesticide `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
Amount float64 `gorm:"not null"` // in units of the pesticide
|
||||
PerformedByID uint `gorm:"not null"`
|
||||
PerformedBy worker.Worker `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
}
|
||||
|
||||
func (l *CropProtectionLog) AfterSave(db *gorm.DB) (err error) {
|
||||
// Example: compute cost as Amount * UnitPrice
|
||||
if l.Pesticide.ID == 0 {
|
||||
if err := db.First(&l.Pesticide, l.PesticideID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
unitPrice := l.Pesticide.Cost
|
||||
totalCost := unitPrice.Mul(decimal.NewFromFloat(l.Amount))
|
||||
l.UpdateCost(db, totalCost)
|
||||
return
|
||||
}
|
||||
|
||||
func RegisterModels() { database.RegisterModel(&CropProtectionLog{}) }
|
||||
Reference in New Issue
Block a user