datamodel done without validators

This commit is contained in:
2025-04-17 12:33:53 +02:00
parent 284a40fac2
commit 5337da219e
58 changed files with 575 additions and 227 deletions

View 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{}) }

View 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]())
}

View 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{}) }

View 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]())
}

View 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{}) }

View 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]())
}

View 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{}) }

View 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
View 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)
}

View 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{}) }

View 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]())
}