Files
zineshop/controllers/shopItemController.go
2025-03-03 13:49:36 +01:00

279 lines
5.9 KiB
Go

package controllers
import (
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"example.com/gin/test/models"
//"example.com/gin/test/services"
"example.com/gin/test/repositories"
)
type CRUDController interface {
Create(*gin.Context)
GetAll(*gin.Context)
GetById(*gin.Context)
Update(*gin.Context)
Delete(*gin.Context)
}
type ShopItemController interface {
CRUDController
ShopItemView(*gin.Context)
AddItemView(*gin.Context)
AddItemHandler(*gin.Context)
CreateTag(*gin.Context)
GetAllTags(*gin.Context)
}
type shopItemController struct {}
func NewShopItemController() ShopItemController {
return &shopItemController{}
}
func (rc *shopItemController) GetAll(c *gin.Context) {
shopItems, err := repositories.ShopItems.GetAll()
if err != nil {
ReplyError(c, fmt.Errorf("Could not query shopItems"))
return
}
c.JSON(http.StatusOK, shopItems)
}
func (rc *shopItemController) GetById(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("Could not query shopItem: %v", err))
return
}
ReplyOK(c, shopItem)
}
func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.ShopItem, error) {
name := ctx.PostForm("name")
abstract := ctx.PostForm("abstract")
description := ctx.PostForm("description")
priceStr := ctx.PostForm("price")
tagIds := ctx.PostFormArray("tags[]")
if name == "" || description == "" {
return models.ShopItem{}, fmt.Errorf("Name or description empty")
}
// Convert the price string to float64
price, err := strconv.ParseFloat(priceStr, 64)
if err != nil {
return models.ShopItem{}, fmt.Errorf("Could not parse price")
}
shopItem := models.ShopItem{
Name: name,
Abstract: abstract,
Description: description,
Price: price,
IsPublic: true,
}
for _, tagId := range tagIds {
tag, err := repositories.Tags.GetById(tagId)
if err != nil {
return models.ShopItem{}, fmt.Errorf("Could not get tag by id")
}
shopItem.Tags = append(shopItem.Tags, tag)
}
return shopItem, nil
//return services.ShopItems.NewShopItem(name, abstract, description, price, tagIds)
}
func (rc *shopItemController) Create(c *gin.Context) {
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
_, err = repositories.ShopItems.Create(shopItem)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
ReplyOK(c, "shopItem was created")
}
func (rc *shopItemController) Update(c *gin.Context) {
shopItemId, err := strconv.Atoi(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("shopItem with Id '%s' does not exist", c.Param("id")))
return
}
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
ReplyError(c, err)
return
}
shopItem.ID = uint(shopItemId)
_, err = repositories.ShopItems.Update(shopItem)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
ReplyOK(c, "shopItem was updated")
}
func (rc *shopItemController) Delete(c *gin.Context) {
err := repositories.ShopItems.DeleteById(c.Param("id"))
if err != nil {
ReplyError(c, fmt.Errorf("shopItem deletion failed: %s", err))
return
}
ReplyOK(c, "shopItem was deleted")
}
func (rc *shopItemController) ShopItemView(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
if err != nil {
c.HTML(http.StatusBadRequest, "shopitem.html", gin.H{ "data": gin.H{ "error": err } })
}
//TODO: get tags by item
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "shopitem.html", gin.H{ "data": gin.H{ "error": err } })
}
data := CreateSessionData(c, gin.H{
"shopItem": shopItem,
"tags": tags,
})
if err != nil {
c.HTML(http.StatusBadRequest, "shopitem.html", data)
}
c.HTML(http.StatusOK, "shopitem.html", data)
}
func (rc *shopItemController) AddItemView(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
}
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
}
func (rc *shopItemController) AddItemHandler(c *gin.Context) {
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
return
}
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
return
}
_, err = repositories.ShopItems.Create(shopItem)
if err != nil {
data := CreateSessionData(c, gin.H{
"error": err,
"success": "",
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
return
}
data := CreateSessionData(c, gin.H{
"error": "",
"success": fmt.Sprintf("Item '%s' Registered", shopItem.Name),
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
}
func (rc *shopItemController) CreateTag(c *gin.Context) {
tag, err := models.NewTagByJson(c)
if err != nil {
ReplyError(c, err)
}
_, err = repositories.Tags.Create(tag)
if err != nil {
ReplyError(c, fmt.Errorf("shopItem creation failed: %s", err))
return
}
//userID := user.(models.User).ID
//rc.DB.Model(&models.shopItem{}).Where("id = ?"), room.ID).Association("Admins").Append(&models.User{ID: userID})
//if result.Error != nil {
// ReplyError(c, fmt.Errorf("shopItem creation failed: %s", result.Error))
// return
//}
ReplyOK(c, fmt.Sprintf("tag '%s' was created", tag.Name))
}
func (rc *shopItemController) GetAllTags(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
ReplyError(c, fmt.Errorf("Could not query Tags"))
return
}
c.JSON(http.StatusOK, tags)
}
func ReplyError(ctx *gin.Context, err error) {
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
}
func ReplyOK(ctx *gin.Context, message any) {
ctx.JSON(http.StatusOK, message)
}