diff --git a/wagfarm-api/main.go b/wagfarm-api/main.go index fa230ab..ccbd7ca 100644 --- a/wagfarm-api/main.go +++ b/wagfarm-api/main.go @@ -28,17 +28,19 @@ func initLogging() { } func dbMigration() { - database.DB.AutoMigrate( + database.DB.AutoMigrate( &models.AssetType{}, &models.Asset{}, &models.LogType{}, &models.Log{}, ) - + db.Create(&models.AssetType{Name: "machine"}) db.Create(&models.AssetType{Name: "field"}) db.Create(&models.AssetType{Name: "animal"}) db.Create(&models.AssetType{Name: "substance"}) + + err := db.SetupJoinTable(&models.Fertilizer{}, "Ingredients", &models.FertilizerIngredient{}) } func main() { diff --git a/wagfarm-api/models/asset_models.go b/wagfarm-api/models/asset_models.go index 125f1a1..93f7e72 100644 --- a/wagfarm-api/models/asset_models.go +++ b/wagfarm-api/models/asset_models.go @@ -1,66 +1,87 @@ package models type _Asset struct { - ID uint `gorm:"primaryKey` - Name string `gorm:"not null"` - Notes string + ID uint `gorm:"primaryKey` + Name string `gorm:"not null"` + Notes string } type Land struct { _Asset - FID string `gorm:"not null"` - IsWaterProtectionArea bool `gorm:"not null"` - IsRedArea bool `gorm:"not null"` - Betriebsnummer? - Area float64 `gorm:"not null"` + FID string `gorm:"not null"` + IsWaterProtectionArea bool `gorm:"not null"` + IsRedArea bool `gorm:"not null"` + Area float64 `gorm:"not null"` } type Plant struct { _Asset - LandId uint `gorm:"not null"` - Land Land `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - CropID uint `gorm:"not null"` - Crop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - CoverCropID uint `gorm:"not null"` - CoverCrop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - Year uint `gorm:"not null"` - AreaPercentage float64 `gorm:"not null"` + LandId uint `gorm:"not null"` + Land Land `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + CropID uint `gorm:"not null"` + Crop Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + CoverCropID uint `gorm:"not null"` + CoverCrop Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Year uint `gorm:"not null"` + AreaPercentage float64 `gorm:"not null"` +} +type _Resource struct { + _Asset + UnitID uint `gorm:"not null"` + Unit Unit `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Cost decimal.Decimal `gorm:"not null; type:decimal(10,2);"` // in euro/unit } type Machine struct { - _Asset - UnitID uint `gorm:"not null"` - Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro + _Resource } -type SeedType struct { - ID uint `gorm:"primaryKey` - Name string `gorm:"not null"` - CropID uint `gorm:"not null"` - Crop Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` +type _BoughtResource struct { + _Resource + BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"` + BoughtFromID uint `gorm:"not null"` + BoughtFrom Seller `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` +} + +type _SoldResource struct { + _Resource + SaleDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"` + SoldToID uint `gorm:"not null"` + SoldTo Customer `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` } type Seed struct { - _Asset - BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"` - UnitID uint `gorm:"not null"` - Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro/unit - SourcedFromID uint `gorm:"not null"` - SourcedFrom Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - SeedTypeID uint `gorm:"not null"` - SeedType SeedType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + _BoughtResource + SeedTypeID uint `gorm:"not null"` + SeedType SeedType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` } type Fertilizer struct { _Asset - BuyDate Datetime.Time `gorm:"type:date;default:CURRENT_DATE"` - UnitID uint `gorm:"not null"` - Unit Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - Cost float64 `gorm:"not null; type:decimal(10,2);"` // in euro/unit - SourcedFromID uint `gorm:"not null"` - SourcedFrom Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` - FertilizerTypeID uint `gorm:"not null"` - FertilizerType Taxonomy `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` -} \ No newline at end of file + _BoughtResource + FertilizerTypeID uint `gorm:"not null"` + 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 asset + 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 `gorm:"not null"` +} + +type Pesticide struct { + _BoughtResource + PesticideTypeID uint `gorm:"not null"` + PesticideType PesticideType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` +} + +type Product struct { + _SoldResource + ProductTypeID uint `gorm:"not null"` + ProductType ProductType `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` +} diff --git a/wagfarm-api/models/log_models.go b/wagfarm-api/models/log_models.go index 693a3ad..3089630 100644 --- a/wagfarm-api/models/log_models.go +++ b/wagfarm-api/models/log_models.go @@ -1,41 +1,59 @@ +package models + type Log struct { - ID uint `gorm:"primaryKey"` - Name string `gorm:"not null"` - Notes string - LogType string `gorm:"not null"` - Date Datetime.Time `gorm:"type:date;default:CURRENT_DATE"not null` - Duration float64 `gorm:"not null"` // in hours - VisibiltyID uint `gorm:"not null"` - Visibility Taxonomy `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + ID uint `gorm:"primaryKey"` + Name string `gorm:"not null"` + Notes string `gorm:"not null"` + LogType string `gorm:"not null"` + Date Datetime.Time `gorm:"type:date;default:CURRENT_DATE"not null` + Duration float64 `gorm:"not null"` // in hours + VisibiltyID uint `gorm:"not null"` + Visibility Visibility `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` } type _LogMetadata struct { - ID uint `gorm:"primaryKey"` - LogID uint `gorm:"not null"` - Log Log `gorm:"foreignKey:LogID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + ID uint `gorm:"primaryKey"` + LogID uint `gorm:"not null"` + Log Log `gorm:"foreignKey:LogID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` } type _FieldWorkLog struct { _LogMetadata - WorkedAreaPercentage float64 `gorm:"not null"` - MachineID uint `gorm:"not null"` - Machine Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + WorkedAreaPercentage float64 `gorm:"not null"` + MachineID uint `gorm:"not null"` + Machine Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` } type SeedingLog struct { _FieldWorkLog - SeedID uint `gorm:"not null"` - Seed Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` - TechniqueID uint `gorm:"not null"` - Technique Taxonomy `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` - Amount float64 `gorm:"not null"` // in units/ha - ConditionsID uint `gorm:"not null"` - Conditions Taxonomy `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + SeedID uint `gorm:"not null"` + Seed Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + TechniqueID uint `gorm:"not null"` + Technique SeedingTechnique `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + Amount float64 `gorm:"not null"` // in units/ha + ConditionsID uint `gorm:"not null"` + Conditions SeedingCondition `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` } type FertilizingLog struct { _FieldWorkLog - FertilizerID uint `gorm:"not null"` - Fertilizer Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` - Amount float64 `gorm:"not null"` // in units/ha -} \ No newline at end of file + FertilizerID uint `gorm:"not null"` + Fertilizer Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + Amount float64 `gorm:"not null"` // in units of the fertilizer +} + +type CropProtectionLog struct { + _FieldWorkLog + PesticideID uint `gorm:"not null"` + 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 `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` +} + +type HarvestLog struct { + _FieldWorkLog + ProductID uint `gorm:"not null"` + Product Product `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"` + Amount float64 `gorm:"not null"` // in units of the product +} diff --git a/wagfarm-api/models/taxonomy_models.go b/wagfarm-api/models/taxonomy_models.go index 650007a..c88657e 100644 --- a/wagfarm-api/models/taxonomy_models.go +++ b/wagfarm-api/models/taxonomy_models.go @@ -1,7 +1,28 @@ package models -type Taxonomy struct { - ID uint `gorm:"primaryKey" json:"id"` - Type string `gorm:"not null;index" json:"type"` // e.g., "unit", "color", etc. - Name string `gorm:"not null;index" json:"name"` // e.g., "kg", "lb" -} \ No newline at end of file +type _Taxonomy struct { + ID uint `gorm:"primaryKey" json:"id"` + Name string `gorm:"not null;index" json:"name"` // e.g., "kg", "lb" +} + +type Unit struct{ _Taxonomy } +type Visibility struct{ _Taxonomy } +type SeedingTechnique struct{ _Taxonomy } +type SeedingCondition struct{ _Taxonomy } +type Worker struct{ _Taxonomy } +type Crop struct{ _Taxonomy } +type Seller struct{ _Taxonomy } +type Customer struct{ _Taxonomy } +type SeedType struct { + _Taxonomy + CropID uint `gorm:"not null"` + Crop Crop `gorm:"not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` +} +type Ingredient struct { + _Taxonomy + UnitID uint `gorm:"not null"` + Unit Unit `gorm:"not null"` +} +type FertilizerType struct{ _Taxonomy } +type PesticideType struct{ _Taxonomy } +type ProductType struct{ _Taxonomy }