From 6330a990f5ed0871c837308f0cc891974c500c74 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sun, 29 Jun 2025 15:37:02 +0200 Subject: [PATCH] add paper weight --- controllers/configController.go | 1 + models/paper.go | 37 ++++++++++++++++++++------------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/controllers/configController.go b/controllers/configController.go index 5dfba1f..e9b8556 100644 --- a/controllers/configController.go +++ b/controllers/configController.go @@ -136,6 +136,7 @@ func (rc *configController) PaperHandler(ctx *gin.Context) { paper.Name = newPaper.Name paper.Brand = newPaper.Brand paper.Size = newPaper.Size + paper.Weight = newPaper.Weight paper.Price = newPaper.Price paper, err = repositories.Papers.Update(paper) diff --git a/models/paper.go b/models/paper.go index 5dc3b37..ab374c5 100644 --- a/models/paper.go +++ b/models/paper.go @@ -34,40 +34,49 @@ func ParseSize(s string) (c PaperSize, err error) { type Paper struct { gorm.Model - Name string `json:"name" binding:"required" gorm:"not null"` - Brand string `json:"brand" binding:"required"` - Size PaperSize `json:"size" binding:"required"` - Price float64 `json:"price" binding:"required"` + Name string `json:"name" binding:"required" gorm:"not null"` + Brand string `json:"brand" binding:"required"` + Size PaperSize `json:"size" binding:"required"` + Weight int `json:"weight" binding:"required"` + Price float64 `json:"price" binding:"required"` } func NewPaper(ctx *gin.Context) (Paper, error) { name := ctx.PostForm("name") brand := ctx.PostForm("brand") sizeTmp := ctx.PostForm("size") + weightTmp := ctx.PostForm("weight") priceTmp := ctx.PostForm("price") - price, err := strconv.ParseFloat(priceTmp, 64) - - if err != nil { - return Paper{}, fmt.Errorf("Couldnt parse Price") - } - size, err := ParseSize(sizeTmp) if err != nil { return Paper{}, fmt.Errorf("Couldnt parse Size") } + weight, err := strconv.Atoi(weightTmp) + + if err != nil { + return Paper{}, fmt.Errorf("Couldnt parse Weight") + } + + price, err := strconv.ParseFloat(priceTmp, 64) + + if err != nil { + return Paper{}, fmt.Errorf("Couldnt parse Price") + } + if name == "" || brand == "" { return Paper{}, fmt.Errorf("Name or brand empty") } // Convert the price string to float64 tag := Paper{ - Name: name, - Brand: brand, - Size: size, - Price: price, + Name: name, + Brand: brand, + Size: size, + Weight: weight, + Price: price, } return tag, nil