datamodel done without validators
This commit is contained in:
48
wagfarm-api/logs/base/model.go
Normal file
48
wagfarm-api/logs/base/model.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"time"
|
||||
"wagfarm-api/assets/machine"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/taxonomy/visibility"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BaseLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
Notes string `gorm:"not null"`
|
||||
LogType string `gorm:"not null"`
|
||||
Date time.Time `gorm:"not null"`
|
||||
Duration float64 `gorm:"not null"` // in hours
|
||||
VisibiltyID uint `gorm:"not null"`
|
||||
Visibility visibility.Visibility `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
Cost decimal.Decimal `gorm:"not null;type:decimal(10,2);"` // in euro
|
||||
}
|
||||
|
||||
func (l BaseLog) GetID() uint { return l.ID }
|
||||
|
||||
type ExtendedLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
BaseLogID uint `gorm:"not null"`
|
||||
BaseLog BaseLog `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
}
|
||||
|
||||
func (l ExtendedLog) GetID() uint { return l.ID }
|
||||
func (l *ExtendedLog) UpdateCost(db *gorm.DB, newCost decimal.Decimal) (err error) {
|
||||
err = db.Model(&BaseLog{}).
|
||||
Where("id = ?", l.BaseLogID).
|
||||
Update("cost", newCost).Error
|
||||
return
|
||||
}
|
||||
|
||||
type FieldWorkLog struct {
|
||||
ExtendedLog
|
||||
WorkedAreaPercentage float64 `gorm:"not null"`
|
||||
MachineID uint `gorm:"not null"`
|
||||
Machine machine.Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
}
|
||||
|
||||
func RegisterModels() { database.RegisterModel(&BaseLog{}) }
|
||||
11
wagfarm-api/logs/base/route.go
Normal file
11
wagfarm-api/logs/base/route.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"wagfarm-api/generic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
generic.RegisterCRUDRoutes(rg, generic.DefaultCRUDController[BaseLog]())
|
||||
}
|
||||
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{}) }
|
||||
11
wagfarm-api/logs/cropprotection/route.go
Normal file
11
wagfarm-api/logs/cropprotection/route.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package cropprotection
|
||||
|
||||
import (
|
||||
"wagfarm-api/generic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
generic.RegisterCRUDRoutes(rg.Group("/cropprotection"), generic.DefaultCRUDController[CropProtectionLog]())
|
||||
}
|
||||
34
wagfarm-api/logs/fertilizing/model.go
Normal file
34
wagfarm-api/logs/fertilizing/model.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package fertilizing
|
||||
|
||||
import (
|
||||
"wagfarm-api/assets/fertilizer"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/logs/base"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type FertilizingLog struct {
|
||||
base.ExtendedLog
|
||||
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{}) }
|
||||
11
wagfarm-api/logs/fertilizing/route.go
Normal file
11
wagfarm-api/logs/fertilizing/route.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package fertilizing
|
||||
|
||||
import (
|
||||
"wagfarm-api/generic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
generic.RegisterCRUDRoutes(rg.Group("/fertilizer"), generic.DefaultCRUDController[FertilizingLog]())
|
||||
}
|
||||
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]())
|
||||
}
|
||||
28
wagfarm-api/logs/logs.go
Normal file
28
wagfarm-api/logs/logs.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package logs
|
||||
|
||||
import (
|
||||
"wagfarm-api/logs/base"
|
||||
"wagfarm-api/logs/cropprotection"
|
||||
"wagfarm-api/logs/fertilizing"
|
||||
"wagfarm-api/logs/harvest"
|
||||
"wagfarm-api/logs/seeding"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterModels() {
|
||||
base.RegisterModels()
|
||||
cropprotection.RegisterModels()
|
||||
fertilizing.RegisterModels()
|
||||
harvest.RegisterModels()
|
||||
seeding.RegisterModels()
|
||||
}
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
group := rg.Group("/logs")
|
||||
base.RegisterRoutes(group)
|
||||
cropprotection.RegisterRoutes(group)
|
||||
fertilizing.RegisterRoutes(group)
|
||||
harvest.RegisterRoutes(group)
|
||||
seeding.RegisterRoutes(group)
|
||||
}
|
||||
40
wagfarm-api/logs/seeding/model.go
Normal file
40
wagfarm-api/logs/seeding/model.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package seeding
|
||||
|
||||
import (
|
||||
"wagfarm-api/assets/seed"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/logs/base"
|
||||
"wagfarm-api/taxonomy/seedingcondition"
|
||||
"wagfarm-api/taxonomy/seedingtechnique"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SeedingLog struct {
|
||||
base.FieldWorkLog
|
||||
SeedID uint `gorm:"not null"`
|
||||
Seed seed.Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
TechniqueID uint `gorm:"not null"`
|
||||
Technique seedingtechnique.SeedingTechnique `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
Amount float64 `gorm:"not null"` // in units/ha
|
||||
ConditionsID uint `gorm:"not null"`
|
||||
Conditions seedingcondition.SeedingCondition `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
}
|
||||
|
||||
func (l *SeedingLog) AfterSave(db *gorm.DB) (err error) {
|
||||
// Example: compute cost as Amount * UnitPrice
|
||||
if l.SeedID == 0 {
|
||||
if err := db.First(&l.Seed, l.SeedID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
unitPrice := l.Seed.Cost
|
||||
totalCost := unitPrice.Mul(decimal.NewFromFloat(l.Amount))
|
||||
l.UpdateCost(db, totalCost)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func RegisterModels() { database.RegisterModel(&SeedingLog{}) }
|
||||
11
wagfarm-api/logs/seeding/route.go
Normal file
11
wagfarm-api/logs/seeding/route.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package seeding
|
||||
|
||||
import (
|
||||
"wagfarm-api/generic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
generic.RegisterCRUDRoutes(rg.Group("/seeding"), generic.DefaultCRUDController[SeedingLog]())
|
||||
}
|
||||
Reference in New Issue
Block a user