add item image

This commit is contained in:
2025-03-03 15:21:01 +01:00
parent cdfd77bc21
commit 803535daba
12 changed files with 401 additions and 112 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strconv"
"path/filepath"
"github.com/gin-gonic/gin"
@@ -27,6 +28,10 @@ type ShopItemController interface {
AddItemHandler(*gin.Context)
CreateTag(*gin.Context)
GetAllTags(*gin.Context)
EditItemView(*gin.Context)
EditItemHandler(*gin.Context)
DeleteItemView(*gin.Context)
DeleteItemHandler(*gin.Context)
}
type shopItemController struct {}
@@ -63,6 +68,15 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
description := ctx.PostForm("description")
priceStr := ctx.PostForm("price")
tagIds := ctx.PostFormArray("tags[]")
image, err := ctx.FormFile("image")
dst := "static/img/zine.jpg"
if err == nil {
dst = filepath.Join("static/uploads", image.Filename)
if err := ctx.SaveUploadedFile(image, dst); err != nil {
return models.ShopItem{}, fmt.Errorf("Could not save image")
}
}
if name == "" || description == "" {
return models.ShopItem{}, fmt.Errorf("Name or description empty")
@@ -80,6 +94,7 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
Description: description,
Price: price,
IsPublic: true,
Image: dst,
}
for _, tagId := range tagIds {
@@ -179,7 +194,6 @@ func (rc *shopItemController) ShopItemView(c *gin.Context) {
c.HTML(http.StatusOK, "shopitem.html", data)
}
func (rc *shopItemController) AddItemView(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
@@ -201,6 +215,7 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
fmt.Println(err)
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
return
}
@@ -208,6 +223,7 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
fmt.Println(err)
c.HTML(http.StatusBadRequest, "additem.html", gin.H{ "error": err })
return
}
@@ -233,6 +249,123 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
c.HTML(http.StatusOK, "additem.html", data)
}
func (rc *shopItemController) EditItemView(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{ "error": err })
}
fmt.Println(shopItem)
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
"shopItem": shopItem,
"tags": tags,
})
c.HTML(http.StatusOK, "edititem.html", data)
}
func (rc *shopItemController) EditItemHandler(c *gin.Context) {
shopItem, err := rc.NewShopItemFromForm(c)
if err != nil {
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{ "error": err })
return
}
newShopItem, err := repositories.ShopItems.GetById(c.Param("id"))
if err != nil {
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{ "error": err })
return
}
newShopItem.Name = shopItem.Name
newShopItem.Abstract = shopItem.Abstract
newShopItem.Description = shopItem.Description
newShopItem.Price = shopItem.Price
newShopItem.IsPublic = shopItem.IsPublic
newShopItem.Tags = shopItem.Tags
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{ "error": err })
return
}
_, err = repositories.ShopItems.Update(newShopItem)
if err != nil {
data := CreateSessionData(c, gin.H{
"error": err,
"success": "",
"tags": tags,
})
c.HTML(http.StatusOK, "edititem.html", data)
return
}
data := CreateSessionData(c, gin.H{
"error": "",
"success": fmt.Sprintf("Item '%s' Updated", newShopItem.Name),
"tags": tags,
})
c.HTML(http.StatusOK, "edititem.html", data)
}
func (rc *shopItemController) DeleteItemView(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "deleteitem.html", gin.H{ "error": err })
}
fmt.Println(shopItem)
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
"shopItem": shopItem,
"tags": tags,
})
c.HTML(http.StatusOK, "deleteitem.html", data)
}
func (rc *shopItemController) DeleteItemHandler(c *gin.Context) {
err := repositories.ShopItems.DeleteById(c.Param("id"))
if err != nil {
data := CreateSessionData(c, gin.H{
"error": err,
"success": "",
})
c.HTML(http.StatusOK, "deleteitem.html", data)
}
shopItems, _ := repositories.ShopItems.GetAll()
fmt.Println(len(shopItems))
data := CreateSessionData(c, gin.H{
"title": "shopItem Page",
"shopItems": shopItems,
})
fmt.Println(data)
c.HTML(http.StatusOK, "index.html", data)
}
func (rc *shopItemController) CreateTag(c *gin.Context) {
tag, err := models.NewTagByJson(c)