diff --git a/wagfarm-api/main.go b/wagfarm-api/main.go index 113e732..fa230ab 100644 --- a/wagfarm-api/main.go +++ b/wagfarm-api/main.go @@ -27,18 +27,26 @@ func initLogging() { gin.DefaultWriter = f // capture Gin logs too } +func dbMigration() { + 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"}) +} + func main() { initLogging() log.Println("Starting API server...") database.Connect() - database.DB.AutoMigrate( - &models.AssetType{}, - &models.Asset{}, - &models.LogType{}, - &models.Log{}, - ) r := gin.Default() routes.SetupRoutes(r) diff --git a/wagfarm-api/models/Schlagkartei52.xlsm b/wagfarm-api/models/Schlagkartei52.xlsm new file mode 100755 index 0000000..8401b7a Binary files /dev/null and b/wagfarm-api/models/Schlagkartei52.xlsm differ diff --git a/wagfarm-api/models/asset_models.go b/wagfarm-api/models/asset_models.go new file mode 100644 index 0000000..125f1a1 --- /dev/null +++ b/wagfarm-api/models/asset_models.go @@ -0,0 +1,66 @@ +package models + +type _Asset struct { + 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"` +} + +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"` +} + +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 +} + +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 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;"` +} + +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 diff --git a/wagfarm-api/models/log_models.go b/wagfarm-api/models/log_models.go new file mode 100644 index 0000000..693a3ad --- /dev/null +++ b/wagfarm-api/models/log_models.go @@ -0,0 +1,41 @@ +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"` +} + +type _LogMetadata struct { + 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"` +} + +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"` +} + +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 diff --git a/wagfarm-api/models/models.go b/wagfarm-api/models/models.go deleted file mode 100644 index 691a545..0000000 --- a/wagfarm-api/models/models.go +++ /dev/null @@ -1,28 +0,0 @@ -package models - -type AssetType struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"unique;not null" json:"name"` -} - -type Asset struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"not null" json:"name"` - AssetTypeID uint `json:"asset_type_id"` - AssetType AssetType `gorm:"foreignKey:AssetTypeID" json:"-"` -} - -type LogType struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"unique;not null" json:"name"` -} - -type Log struct { - ID uint `gorm:"primaryKey" json:"id"` - Name string `gorm:"not null" json:"name"` - LogTypeID uint `json:"log_type_id"` - LogType LogType `gorm:"foreignKey:LogTypeID" json:"-"` - Date string `gorm:"type:date;default:CURRENT_DATE" json:"date"` - Equipment []Asset `gorm:"many2many:log_equipment_asset;" json:"equipment"` - Substance []Asset `gorm:"many2many:log_substance_asset;" json:"substance"` -} diff --git a/wagfarm-api/models/taxonomy_models.go b/wagfarm-api/models/taxonomy_models.go new file mode 100644 index 0000000..650007a --- /dev/null +++ b/wagfarm-api/models/taxonomy_models.go @@ -0,0 +1,7 @@ +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