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,36 @@
package assets
import (
"wagfarm-api/assets/fertilizer"
"wagfarm-api/assets/land"
"wagfarm-api/assets/machine"
"wagfarm-api/assets/pesticide"
"wagfarm-api/assets/plant"
"wagfarm-api/assets/product"
"wagfarm-api/assets/seed"
"github.com/gin-gonic/gin"
)
var Models = []any{}
func RegisterModels() {
fertilizer.RegisterModels()
land.RegisterModels()
machine.RegisterModels()
pesticide.RegisterModels()
plant.RegisterModels()
product.RegisterModels()
seed.RegisterModels()
}
func RegisterRoutes(rg *gin.RouterGroup) {
group := rg.Group("/assets")
fertilizer.RegisterRoutes(group)
land.RegisterRoutes(group)
machine.RegisterRoutes(group)
pesticide.RegisterRoutes(group)
plant.RegisterRoutes(group)
product.RegisterRoutes(group)
seed.RegisterRoutes(group)
}

View File

@@ -0,0 +1,39 @@
package base
import (
"time"
"wagfarm-api/taxonomy/customer"
"wagfarm-api/taxonomy/seller"
"wagfarm-api/taxonomy/unit"
"github.com/shopspring/decimal"
)
type Asset struct {
ID uint `gorm:"primaryKey`
Name string `gorm:"not null"`
Notes string
}
func (a Asset) GetID() uint { return a.ID }
type Resource struct {
Asset
UnitID uint `gorm:"not null"`
Unit unit.Unit `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Cost decimal.Decimal `gorm:"not null; type:decimal(10,2);"` // in euro/unit
}
type BoughtResource struct {
Resource
BuyDate time.Time `gorm:"not null"`
BoughtFromID uint `gorm:"not null"`
BoughtFrom seller.Seller `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
type SoldResource struct {
Resource
SaleDate time.Time `gorm:"not null"`
SoldToID uint `gorm:"not null"`
SoldTo customer.Customer `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}

View File

@@ -0,0 +1,30 @@
package fertilizer
import (
"wagfarm-api/assets/base"
"wagfarm-api/database"
"wagfarm-api/taxonomy/fertilizertype"
"wagfarm-api/taxonomy/ingredient"
)
type Fertilizer struct {
base.BoughtResource
FertilizerTypeID uint `gorm:"not null"`
FertilizerType fertilizertype.FertilizerType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
// Association to the ingredients. This represents the composition.
Ingredients []FertilizerIngredient `gorm:"not null; many2many:fertilizer_ingredient"`
}
type FertilizerIngredient struct {
FertilizerAssetID uint `gorm:"primaryKey"` // foreign key to the fertilizer assets
IngredientID uint `gorm:"primaryKey"` // foreign key to the ingredient
Amount float64 `gorm:"not null"` // amount of the ingredient
// Preload the ingredient details if needed.
Ingredient ingredient.Ingredient `gorm:"not null"`
}
func RegisterModels() {
database.RegisterModel(&Fertilizer{})
database.RegisterJoinTable(&Fertilizer{}, "Ingredients", &FertilizerIngredient{})
}

View File

@@ -0,0 +1,11 @@
package fertilizer
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/fertilizer"), generic.DefaultCRUDController[Fertilizer]())
}

View File

@@ -0,0 +1,16 @@
package land
import (
"wagfarm-api/assets/base"
"wagfarm-api/database"
)
type Land struct {
base.Asset
FID string `gorm:"not null"`
IsWaterProtectionArea bool `gorm:"not null"`
IsRedArea bool `gorm:"not null"`
Area float64 `gorm:"not null"`
}
func RegisterModels() { database.RegisterModel(&Land{}) }

View File

@@ -0,0 +1,11 @@
package land
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/land"), generic.DefaultCRUDController[Land]())
}

View File

@@ -0,0 +1,12 @@
package machine
import (
"wagfarm-api/assets/base"
"wagfarm-api/database"
)
type Machine struct {
base.Resource
}
func RegisterModels() { database.RegisterModel(&Machine{}) }

View File

@@ -0,0 +1,11 @@
package machine
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/machine"), generic.DefaultCRUDController[Machine]())
}

View File

@@ -0,0 +1,15 @@
package pesticide
import (
"wagfarm-api/assets/base"
"wagfarm-api/database"
"wagfarm-api/taxonomy/pesticidetype"
)
type Pesticide struct {
base.BoughtResource
PesticideTypeID uint `gorm:"not null"`
PesticideType pesticidetype.PesticideType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
func RegisterModels() { database.RegisterModel(&Pesticide{}) }

View File

@@ -0,0 +1,11 @@
package pesticide
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/pesticide"), generic.DefaultCRUDController[Pesticide]())
}

View File

@@ -0,0 +1,22 @@
package plant
import (
"wagfarm-api/assets/base"
"wagfarm-api/assets/land"
"wagfarm-api/database"
"wagfarm-api/taxonomy/crop"
)
type Plant struct {
base.Asset
LandId uint `gorm:"not null"`
Land land.Land `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CropID uint `gorm:"not null"`
Crop crop.Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
CoverCropID uint `gorm:"not null"`
CoverCrop crop.Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Year uint `gorm:"not null"`
AreaPercentage float64 `gorm:"not null"`
}
func RegisterModels() { database.RegisterModel(&Plant{}) }

View File

@@ -0,0 +1,11 @@
package plant
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/plant"), generic.DefaultCRUDController[Plant]())
}

View File

@@ -0,0 +1,15 @@
package product
import (
"wagfarm-api/assets/base"
"wagfarm-api/database"
"wagfarm-api/taxonomy/producttype"
)
type Product struct {
base.SoldResource
ProductTypeID uint `gorm:"not null"`
ProductType producttype.ProductType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
func RegisterModels() { database.RegisterModel(&Product{}) }

View File

@@ -0,0 +1,11 @@
package product
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/product"), generic.DefaultCRUDController[Product]())
}

View File

@@ -0,0 +1,15 @@
package seed
import (
"wagfarm-api/assets/base"
"wagfarm-api/database"
"wagfarm-api/taxonomy/seedtype"
)
type Seed struct {
base.BoughtResource
SeedTypeID uint `gorm:"not null"`
SeedType seedtype.SeedType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
}
func RegisterModels() { database.RegisterModel(&Seed{}) }

View File

@@ -0,0 +1,11 @@
package seed
import (
"wagfarm-api/generic"
"github.com/gin-gonic/gin"
)
func RegisterRoutes(rg *gin.RouterGroup) {
generic.RegisterCRUDRoutes(rg.Group("/seed"), generic.DefaultCRUDController[Seed]())
}