add basic paper model/view/controller
All checks were successful
Go / build (push) Successful in 12m50s
All checks were successful
Go / build (push) Successful in 12m50s
paper weight is missing
This commit is contained in:
74
models/paper.go
Normal file
74
models/paper.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type PaperSize string
|
||||
|
||||
const (
|
||||
A3 PaperSize = "A3"
|
||||
A4 PaperSize = "A4"
|
||||
A5 PaperSize = "A5"
|
||||
SRA3 PaperSize = "SRA3"
|
||||
)
|
||||
|
||||
func ParseSize(s string) (c PaperSize, err error) {
|
||||
s = strings.ToUpper(s)
|
||||
if s == "A3" {
|
||||
return A3, nil
|
||||
} else if s == "A4" {
|
||||
return A4, nil
|
||||
} else if s == "A5" {
|
||||
return A5, nil
|
||||
} else if s == "SRA3" {
|
||||
return SRA3, nil
|
||||
}
|
||||
|
||||
return c, fmt.Errorf("Cannot parse category %s", s)
|
||||
}
|
||||
|
||||
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"`
|
||||
}
|
||||
|
||||
func NewPaper(ctx *gin.Context) (Paper, error) {
|
||||
name := ctx.PostForm("name")
|
||||
brand := ctx.PostForm("brand")
|
||||
sizeTmp := ctx.PostForm("size")
|
||||
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")
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
return tag, nil
|
||||
}
|
||||
Reference in New Issue
Block a user