datamodel done without validators
This commit is contained in:
48
wagfarm-api/logs/base/model.go
Normal file
48
wagfarm-api/logs/base/model.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"time"
|
||||
"wagfarm-api/assets/machine"
|
||||
"wagfarm-api/database"
|
||||
"wagfarm-api/taxonomy/visibility"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BaseLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Name string `gorm:"not null"`
|
||||
Notes string `gorm:"not null"`
|
||||
LogType string `gorm:"not null"`
|
||||
Date time.Time `gorm:"not null"`
|
||||
Duration float64 `gorm:"not null"` // in hours
|
||||
VisibiltyID uint `gorm:"not null"`
|
||||
Visibility visibility.Visibility `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
Cost decimal.Decimal `gorm:"not null;type:decimal(10,2);"` // in euro
|
||||
}
|
||||
|
||||
func (l BaseLog) GetID() uint { return l.ID }
|
||||
|
||||
type ExtendedLog struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
BaseLogID uint `gorm:"not null"`
|
||||
BaseLog BaseLog `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
}
|
||||
|
||||
func (l ExtendedLog) GetID() uint { return l.ID }
|
||||
func (l *ExtendedLog) UpdateCost(db *gorm.DB, newCost decimal.Decimal) (err error) {
|
||||
err = db.Model(&BaseLog{}).
|
||||
Where("id = ?", l.BaseLogID).
|
||||
Update("cost", newCost).Error
|
||||
return
|
||||
}
|
||||
|
||||
type FieldWorkLog struct {
|
||||
ExtendedLog
|
||||
WorkedAreaPercentage float64 `gorm:"not null"`
|
||||
MachineID uint `gorm:"not null"`
|
||||
Machine machine.Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||
}
|
||||
|
||||
func RegisterModels() { database.RegisterModel(&BaseLog{}) }
|
||||
11
wagfarm-api/logs/base/route.go
Normal file
11
wagfarm-api/logs/base/route.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package base
|
||||
|
||||
import (
|
||||
"wagfarm-api/generic"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||
generic.RegisterCRUDRoutes(rg, generic.DefaultCRUDController[BaseLog]())
|
||||
}
|
||||
Reference in New Issue
Block a user