add item adding/viewing

This commit is contained in:
2025-03-02 20:29:38 +01:00
parent ba5dbae5eb
commit 31f4422739
3 changed files with 77 additions and 4 deletions

View File

@@ -1,6 +1,8 @@
package models
import (
"fmt"
"strconv"
"gorm.io/gorm"
"github.com/gin-gonic/gin"
)
@@ -15,11 +17,25 @@ type ShopItem struct {
}
func NewShopItem(ctx *gin.Context) (ShopItem, error) {
var shopItem ShopItem
err := ctx.ShouldBindJSON(&shopItem)
name := ctx.PostForm("name")
description := ctx.PostForm("description")
priceStr := ctx.PostForm("price")
if err != nil {
return ShopItem{}, err
// 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