datamodel done without validators
This commit is contained in:
36
wagfarm-api/assets/assets.go
Normal file
36
wagfarm-api/assets/assets.go
Normal 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)
|
||||
}
|
||||
39
wagfarm-api/assets/base/model.go
Normal file
39
wagfarm-api/assets/base/model.go
Normal 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;"`
|
||||
}
|
||||
30
wagfarm-api/assets/fertilizer/model.go
Normal file
30
wagfarm-api/assets/fertilizer/model.go
Normal 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{})
|
||||
}
|
||||
11
wagfarm-api/assets/fertilizer/route.go
Normal file
11
wagfarm-api/assets/fertilizer/route.go
Normal 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]())
|
||||
}
|
||||
16
wagfarm-api/assets/land/model.go
Normal file
16
wagfarm-api/assets/land/model.go
Normal 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{}) }
|
||||
11
wagfarm-api/assets/land/route.go
Normal file
11
wagfarm-api/assets/land/route.go
Normal 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]())
|
||||
}
|
||||
12
wagfarm-api/assets/machine/model.go
Normal file
12
wagfarm-api/assets/machine/model.go
Normal 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{}) }
|
||||
11
wagfarm-api/assets/machine/route.go
Normal file
11
wagfarm-api/assets/machine/route.go
Normal 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]())
|
||||
}
|
||||
15
wagfarm-api/assets/pesticide/model.go
Normal file
15
wagfarm-api/assets/pesticide/model.go
Normal 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{}) }
|
||||
11
wagfarm-api/assets/pesticide/route.go
Normal file
11
wagfarm-api/assets/pesticide/route.go
Normal 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]())
|
||||
}
|
||||
22
wagfarm-api/assets/plant/model.go
Normal file
22
wagfarm-api/assets/plant/model.go
Normal 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{}) }
|
||||
11
wagfarm-api/assets/plant/route.go
Normal file
11
wagfarm-api/assets/plant/route.go
Normal 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]())
|
||||
}
|
||||
15
wagfarm-api/assets/product/model.go
Normal file
15
wagfarm-api/assets/product/model.go
Normal 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{}) }
|
||||
11
wagfarm-api/assets/product/route.go
Normal file
11
wagfarm-api/assets/product/route.go
Normal 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]())
|
||||
}
|
||||
15
wagfarm-api/assets/seed/model.go
Normal file
15
wagfarm-api/assets/seed/model.go
Normal 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{}) }
|
||||
11
wagfarm-api/assets/seed/route.go
Normal file
11
wagfarm-api/assets/seed/route.go
Normal 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]())
|
||||
}
|
||||
Reference in New Issue
Block a user