Files
wagfarm/wagfarm-api/logs/base/model.go

49 lines
1.5 KiB
Go

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{}) }