add item adding/viewing
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user