change log api to reflect data split into base and extension
This commit is contained in:
@@ -2,12 +2,10 @@ package base
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
"wagfarm-api/assets/machine"
|
|
||||||
"wagfarm-api/database"
|
"wagfarm-api/database"
|
||||||
"wagfarm-api/taxonomy/visibility"
|
"wagfarm-api/taxonomy/visibility"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type BaseLog struct {
|
type BaseLog struct {
|
||||||
@@ -23,26 +21,4 @@ type BaseLog struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l BaseLog) GetID() uint { return l.ID }
|
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{}) }
|
func RegisterModels() { database.RegisterModel(&BaseLog{}) }
|
||||||
|
|||||||
@@ -7,5 +7,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||||
generic.RegisterCRUDRoutes(rg, generic.DefaultCRUDController[BaseLog]())
|
generic.RegisterRRoutes(rg, generic.DefaultRController[BaseLog]())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package cropprotection
|
|||||||
import (
|
import (
|
||||||
"wagfarm-api/assets/pesticide"
|
"wagfarm-api/assets/pesticide"
|
||||||
"wagfarm-api/database"
|
"wagfarm-api/database"
|
||||||
"wagfarm-api/logs/base"
|
"wagfarm-api/logs/generic"
|
||||||
"wagfarm-api/taxonomy/worker"
|
"wagfarm-api/taxonomy/worker"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CropProtectionLog struct {
|
type CropProtectionLog struct {
|
||||||
base.FieldWorkLog
|
generic.FieldWorkLog
|
||||||
PesticideID uint `gorm:"not null"`
|
PesticideID uint `gorm:"not null"`
|
||||||
Pesticide pesticide.Pesticide `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
Pesticide pesticide.Pesticide `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||||
Amount float64 `gorm:"not null"` // in units of the pesticide
|
Amount float64 `gorm:"not null"` // in units of the pesticide
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package cropprotection
|
package cropprotection
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"wagfarm-api/generic"
|
"wagfarm-api/logs/generic"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||||
generic.RegisterCRUDRoutes(rg.Group("/cropprotection"), generic.DefaultCRUDController[CropProtectionLog]())
|
generic.RegisterCRUDRoutes(rg.Group("/cropprotection"), generic.LogCRUDController[CropProtectionLog]())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ package fertilizing
|
|||||||
import (
|
import (
|
||||||
"wagfarm-api/assets/fertilizer"
|
"wagfarm-api/assets/fertilizer"
|
||||||
"wagfarm-api/database"
|
"wagfarm-api/database"
|
||||||
"wagfarm-api/logs/base"
|
"wagfarm-api/logs/generic"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FertilizingLog struct {
|
type FertilizingLog struct {
|
||||||
base.ExtendedLog
|
generic.BaseExtendedLog
|
||||||
FertilizerID uint `gorm:"not null"`
|
FertilizerID uint `gorm:"not null"`
|
||||||
Fertilizer fertilizer.Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
Fertilizer fertilizer.Fertilizer `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||||
Amount float64 `gorm:"not null"` // in units of the fertilizer
|
Amount float64 `gorm:"not null"` // in units of the fertilizer
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package fertilizing
|
package fertilizing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"wagfarm-api/generic"
|
"wagfarm-api/logs/generic"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||||
generic.RegisterCRUDRoutes(rg.Group("/fertilizer"), generic.DefaultCRUDController[FertilizingLog]())
|
generic.RegisterCRUDRoutes(rg.Group("/fertilizer"), generic.LogCRUDController[FertilizingLog]())
|
||||||
}
|
}
|
||||||
|
|||||||
112
wagfarm-api/logs/generic/controller.go
Normal file
112
wagfarm-api/logs/generic/controller.go
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"wagfarm-api/database"
|
||||||
|
"wagfarm-api/generic"
|
||||||
|
"wagfarm-api/logs/base"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func LogCRUDController[T ExtendedLog]() generic.CRUDController {
|
||||||
|
return generic.CRUDController{
|
||||||
|
RController: generic.DefaultRController[T](),
|
||||||
|
Create: Create[T],
|
||||||
|
Update: Update[T],
|
||||||
|
Delete: Delete[T],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateLog handles POST /records and creates a new record record.
|
||||||
|
func Create[T ExtendedLog](c *gin.Context) {
|
||||||
|
var base base.BaseLog
|
||||||
|
var record T
|
||||||
|
if err := c.ShouldBindJSON(&base); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := database.DB.Create(&base).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create record: " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := c.ShouldBindJSON(&record); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := database.DB.Create(&record).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create record: " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Optionally preload visibility before returning.
|
||||||
|
database.Preload().First(&record, record.GetID())
|
||||||
|
c.JSON(http.StatusCreated, record)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateLog handles PUT /records/:id and updates an existing record record.
|
||||||
|
func Update[T ExtendedLog](c *gin.Context) {
|
||||||
|
idParam := c.Param("id")
|
||||||
|
id, err := strconv.Atoi(idParam)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var existing T
|
||||||
|
if err := database.DB.First(&existing, id).Error; err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var base base.BaseLog
|
||||||
|
if err := c.ShouldBindJSON(&base); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := database.DB.Model(&existing).Updates(base).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update record: " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var record T
|
||||||
|
if err := c.ShouldBindJSON(&record); err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := database.DB.Model(&existing).Updates(record).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update record: " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
database.Preload().First(&existing, record.GetID())
|
||||||
|
c.JSON(http.StatusOK, existing)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteLog handles DELETE /records/:id and deletes a record record.
|
||||||
|
func Delete[T ExtendedLog](c *gin.Context) {
|
||||||
|
idParam := c.Param("id")
|
||||||
|
id, err := strconv.Atoi(idParam)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid record ID"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var record T
|
||||||
|
if err := database.DB.First(&record, id).Error; err != nil {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "Record not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
base := record.GetBaseLog()
|
||||||
|
if err := database.DB.Delete(&base).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete record: " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := database.DB.Delete(&record).Error; err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete record: " + err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{"message": "Record deleted successfully"})
|
||||||
|
}
|
||||||
37
wagfarm-api/logs/generic/model.go
Normal file
37
wagfarm-api/logs/generic/model.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"wagfarm-api/assets/machine"
|
||||||
|
"wagfarm-api/generic"
|
||||||
|
"wagfarm-api/logs/base"
|
||||||
|
|
||||||
|
"github.com/shopspring/decimal"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ExtendedLog interface {
|
||||||
|
generic.Model
|
||||||
|
GetBaseLog() base.BaseLog
|
||||||
|
}
|
||||||
|
|
||||||
|
type BaseExtendedLog struct {
|
||||||
|
ID uint `gorm:"primaryKey"`
|
||||||
|
BaseLogID uint `gorm:"not null"`
|
||||||
|
BaseLog base.BaseLog `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l BaseExtendedLog) GetID() uint { return l.ID }
|
||||||
|
func (l BaseExtendedLog) GetBaseLog() base.BaseLog { return l.BaseLog }
|
||||||
|
func (l *BaseExtendedLog) UpdateCost(db *gorm.DB, newCost decimal.Decimal) (err error) {
|
||||||
|
err = db.Model(&base.BaseLog{}).
|
||||||
|
Where("id = ?", l.BaseLogID).
|
||||||
|
Update("cost", newCost).Error
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type FieldWorkLog struct {
|
||||||
|
BaseExtendedLog
|
||||||
|
WorkedAreaPercentage float64 `gorm:"not null"`
|
||||||
|
MachineID uint `gorm:"not null"`
|
||||||
|
Machine machine.Machine `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||||
|
}
|
||||||
15
wagfarm-api/logs/generic/route.go
Normal file
15
wagfarm-api/logs/generic/route.go
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
"wagfarm-api/generic"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func RegisterCRUDRoutes(rg *gin.RouterGroup, l generic.CRUDController) {
|
||||||
|
generic.RegisterCRUDRoutes(rg, l)
|
||||||
|
}
|
||||||
|
|
||||||
|
func RegisterRRoutes(rg *gin.RouterGroup, l generic.RController) {
|
||||||
|
generic.RegisterRRoutes(rg, l)
|
||||||
|
}
|
||||||
@@ -3,14 +3,14 @@ package harvest
|
|||||||
import (
|
import (
|
||||||
"wagfarm-api/assets/product"
|
"wagfarm-api/assets/product"
|
||||||
"wagfarm-api/database"
|
"wagfarm-api/database"
|
||||||
"wagfarm-api/logs/base"
|
"wagfarm-api/logs/generic"
|
||||||
|
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HarvestLog struct {
|
type HarvestLog struct {
|
||||||
base.FieldWorkLog
|
generic.FieldWorkLog
|
||||||
ProductID uint `gorm:"not null"`
|
ProductID uint `gorm:"not null"`
|
||||||
Product product.Product `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
Product product.Product `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||||
Amount float64 `gorm:"not null"` // in units of the product
|
Amount float64 `gorm:"not null"` // in units of the product
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package harvest
|
package harvest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"wagfarm-api/generic"
|
"wagfarm-api/logs/generic"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||||
generic.RegisterCRUDRoutes(rg.Group("/harvest"), generic.DefaultCRUDController[HarvestLog]())
|
generic.RegisterCRUDRoutes(rg.Group("/harvest"), generic.LogCRUDController[HarvestLog]())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package seeding
|
|||||||
import (
|
import (
|
||||||
"wagfarm-api/assets/seed"
|
"wagfarm-api/assets/seed"
|
||||||
"wagfarm-api/database"
|
"wagfarm-api/database"
|
||||||
"wagfarm-api/logs/base"
|
"wagfarm-api/logs/generic"
|
||||||
"wagfarm-api/taxonomy/seedingcondition"
|
"wagfarm-api/taxonomy/seedingcondition"
|
||||||
"wagfarm-api/taxonomy/seedingtechnique"
|
"wagfarm-api/taxonomy/seedingtechnique"
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type SeedingLog struct {
|
type SeedingLog struct {
|
||||||
base.FieldWorkLog
|
generic.FieldWorkLog
|
||||||
SeedID uint `gorm:"not null"`
|
SeedID uint `gorm:"not null"`
|
||||||
Seed seed.Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
Seed seed.Seed `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;not null"`
|
||||||
TechniqueID uint `gorm:"not null"`
|
TechniqueID uint `gorm:"not null"`
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package seeding
|
package seeding
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"wagfarm-api/generic"
|
"wagfarm-api/logs/generic"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterRoutes(rg *gin.RouterGroup) {
|
func RegisterRoutes(rg *gin.RouterGroup) {
|
||||||
generic.RegisterCRUDRoutes(rg.Group("/seeding"), generic.DefaultCRUDController[SeedingLog]())
|
generic.RegisterCRUDRoutes(rg.Group("/seeding"), generic.LogCRUDController[SeedingLog]())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user