started datamodelling papas excel tables

This commit is contained in:
2025-04-13 01:24:44 +02:00
parent 96f2a3762f
commit 0564489b95
6 changed files with 128 additions and 34 deletions

View File

@@ -27,12 +27,7 @@ func initLogging() {
gin.DefaultWriter = f // capture Gin logs too gin.DefaultWriter = f // capture Gin logs too
} }
func main() { func dbMigration() {
initLogging()
log.Println("Starting API server...")
database.Connect()
database.DB.AutoMigrate( database.DB.AutoMigrate(
&models.AssetType{}, &models.AssetType{},
&models.Asset{}, &models.Asset{},
@@ -40,6 +35,19 @@ func main() {
&models.Log{}, &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()
r := gin.Default() r := gin.Default()
routes.SetupRoutes(r) routes.SetupRoutes(r)

Binary file not shown.

View File

@@ -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;"`
}

View File

@@ -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
}

View File

@@ -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"`
}

View File

@@ -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"
}