This commit is contained in:
2025-03-03 13:49:36 +01:00
parent 016b54f8dd
commit c7b10d24be
7 changed files with 147 additions and 71 deletions

View File

@@ -1,42 +1,17 @@
package models
import (
"fmt"
"strconv"
"gorm.io/gorm"
"github.com/gin-gonic/gin"
)
type ShopItem struct {
gorm.Model
Name string `json:"name" binding:"required" gorm:"unique;not null"`
Abstract string `json:"Abstract" binding:"required"`
Description string `json:"description" binding:"required"`
Price float64 `json:"price" binding:"required"`
IsPublic bool `json:"isPublic" gorm:"default:true"`
Tags []Tag `gorm:"many2many:item_tags;"`
//Images gin.multipart.FileHeader ``
}
func NewShopItem(ctx *gin.Context) (ShopItem, error) {
name := ctx.PostForm("name")
description := ctx.PostForm("description")
priceStr := ctx.PostForm("price")
// Convert the price string to float64
price, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
return ShopItem{}, fmt.Errorf("Could not parse price")
}
shopItem := ShopItem{
Name: name,
Description: description,
Price: price,
IsPublic: true,
}
if name == "" || description == "" {
return ShopItem{}, fmt.Errorf("Name or description empty")
}
return shopItem, nil
}