datamodel done without validators
This commit is contained in:
34
wagfarm-api/logs/harvest/model.go
Normal file
34
wagfarm-api/logs/harvest/model.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package harvest
|
||||
|
||||
import (
|
||||
"wagfarm-api/assets/product"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/logs/base"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type HarvestLog struct {
|
||||
base.FieldWorkLog
|
||||
ProductID uint `gorm:"not null"`
|
||||
Product product.Product `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
Amount float64 `gorm:"not null"` // in units of the product
|
||||
}
|
||||
|
||||
func (l *HarvestLog) AfterSave(db *gorm.DB) (err error) {
|
||||
// Example: compute cost as Amount * UnitPrice
|
||||
if l.ProductID == 0 {
|
||||
if err := db.First(&l.Product, l.ProductID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
unitPrice := l.Product.Cost
|
||||
totalCost := unitPrice.Mul(decimal.NewFromFloat(l.Amount))
|
||||
l.UpdateCost(db, totalCost)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func RegisterModels() { database.RegisterModel(&HarvestLog{}) }
|
||||
11
wagfarm-api/logs/harvest/route.go
Normal file
11
wagfarm-api/logs/harvest/route.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package harvest
|
||||
|
||||
import (
|
||||
"wagfarm-api/generic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
generic.RegisterCRUDRoutes(rg.Group("/harvest"), generic.DefaultCRUDController[HarvestLog]())
|
||||
}
|
||||
Reference in New Issue
Block a user