add basic print controller + view

This commit is contained in:
2025-04-10 12:54:03 +02:00
parent 5a7565663d
commit f39b6205d1
6 changed files with 307 additions and 64 deletions

View File

@@ -0,0 +1,75 @@
package controllers
import (
"fmt"
"net/http"
"example.com/gin/test/models"
"example.com/gin/test/repositories"
"github.com/gin-gonic/gin"
)
type PrintController interface {
PrintVariantView(*gin.Context)
PrintVariantHandler(*gin.Context)
}
type printController struct{}
func NewPrintController() PrintController {
return &printController{}
}
func (rc *printController) PrintVariantView(c *gin.Context) {
variant, err := repositories.ShopItems.GetVariantById(c.Param("id"))
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
}
shopItem, err := repositories.ShopItems.GetById(fmt.Sprintf("%v", variant.ShopItemID))
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"data": gin.H{"error": err}})
}
type ShopItemVariantPair struct {
ShopItem models.ShopItem
ItemVariant models.ItemVariant
}
data := CreateSessionData(c, gin.H{
"itemVariants": []ShopItemVariantPair{
{ShopItem: shopItem, ItemVariant: variant},
},
})
fmt.Println(data)
c.HTML(http.StatusOK, "printvariant.html", data)
}
func (rc *printController) PrintVariantHandler(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)
}

View File

@@ -3,9 +3,9 @@ package controllers
import (
"fmt"
"net/http"
"strconv"
"path/filepath"
"os/exec"
"path/filepath"
"strconv"
"github.com/gin-gonic/gin"
@@ -16,7 +16,7 @@ import (
type CRUDController interface {
Create(*gin.Context)
GetAll(*gin.Context)
GetAll(*gin.Context)
GetById(*gin.Context)
Update(*gin.Context)
Delete(*gin.Context)
@@ -38,7 +38,7 @@ type ShopItemController interface {
AddTagHandler(*gin.Context)
}
type shopItemController struct {}
type shopItemController struct{}
func NewShopItemController() ShopItemController {
return &shopItemController{}
@@ -80,9 +80,9 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
if err == nil {
dstImage = filepath.Join("static/uploads", image.Filename)
if err := ctx.SaveUploadedFile(image, dstImage); err != nil {
if err := ctx.SaveUploadedFile(image, dstImage); err != nil {
return models.ShopItem{}, fmt.Errorf("Could not save image")
}
}
}
dstPdf := ""
@@ -90,13 +90,13 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
if err == nil {
dstPdf = filepath.Join("static/uploads", pdf.Filename)
if err := ctx.SaveUploadedFile(pdf, dstPdf); err != nil {
if err := ctx.SaveUploadedFile(pdf, dstPdf); err != nil {
return models.ShopItem{}, fmt.Errorf("Could not save PDF")
}
}
if dstImage == defaultImagePath {
dstImage = dstPdf + ".preview.png"
cmd := exec.Command("pdftoppm", "-png", "-singlefile", dstPdf, dstPdf + ".preview")
cmd := exec.Command("pdftoppm", "-png", "-singlefile", dstPdf, dstPdf+".preview")
_, err := cmd.Output()
if err != nil {
@@ -125,29 +125,29 @@ func (rc *shopItemController) NewShopItemFromForm(ctx *gin.Context) (models.Shop
for idx := range variantNames {
if variantValues[idx] == "" || variantNames[idx] == "" {
continue
}
}
price, err := strconv.ParseFloat(variantValues[idx], 64)
if err != nil {
if err != nil {
return models.ShopItem{}, fmt.Errorf("Could not variant parse price")
}
}
variants = append(variants, models.ItemVariant{
Name: variantNames[idx],
variants = append(variants, models.ItemVariant{
Name: variantNames[idx],
Price: price,
})
}
shopItem := models.ShopItem{
Name: name,
Abstract: abstract,
Name: name,
Abstract: abstract,
Description: description,
Category: category,
IsPublic: true,
BasePrice: rc.GetBasePrice(variants),
Image: dstImage,
Pdf: dstPdf,
Variants: variants,
Category: category,
IsPublic: true,
BasePrice: rc.GetBasePrice(variants),
Image: dstImage,
Pdf: dstPdf,
Variants: variants,
}
for _, tagId := range tagIds {
@@ -198,7 +198,6 @@ func (rc *shopItemController) Create(c *gin.Context) {
ReplyOK(c, "shopItem was created")
}
func (rc *shopItemController) Update(c *gin.Context) {
shopItemId, err := strconv.Atoi(c.Param("id"))
@@ -225,7 +224,7 @@ func (rc *shopItemController) Update(c *gin.Context) {
ReplyOK(c, "shopItem was updated")
}
//TODO: delete associated cartitems
// TODO: delete associated cartitems
func (rc *shopItemController) Delete(c *gin.Context) {
err := repositories.ShopItems.DeleteById(c.Param("id"))
@@ -241,19 +240,19 @@ 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 } })
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 } })
c.HTML(http.StatusBadRequest, "shopitem.html", gin.H{"data": gin.H{"error": err}})
}
data := CreateSessionData(c, gin.H{
"shopItem": shopItem,
"tags": tags,
"tags": tags,
})
if err != nil {
@@ -267,25 +266,24 @@ 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 })
c.HTML(http.StatusBadRequest, "additem.html", gin.H{"error": err})
}
data := CreateSessionData(c, gin.H{
"error": "",
"error": "",
"success": "",
"tags": tags,
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
}
func (rc *shopItemController) AddItemHandler(c *gin.Context) {
errorHandler := func(err error, tags []models.Tag) {
data := CreateSessionData(c, gin.H{
"error": err,
"error": err,
"success": "",
"tags": tags,
"tags": tags,
})
c.HTML(http.StatusOK, "additem.html", data)
@@ -310,48 +308,46 @@ func (rc *shopItemController) AddItemHandler(c *gin.Context) {
}
data := CreateSessionData(c, gin.H{
"error": "",
"error": "",
"success": fmt.Sprintf("Item '%s' Registered", shopItem.Name),
"tags": tags,
"tags": tags,
})
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 })
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{"error": err})
}
fmt.Println(shopItem)
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
"error": "",
"success": "",
"shopItem": shopItem,
"tags": tags,
"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 })
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 })
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{"error": err})
return
}
@@ -365,16 +361,16 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{ "error": err })
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,
"error": err,
"success": "",
"tags": tags,
"tags": tags,
})
c.HTML(http.StatusOK, "edititem.html", data)
@@ -382,9 +378,9 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) {
}
data := CreateSessionData(c, gin.H{
"error": "",
"error": "",
"success": fmt.Sprintf("Item '%s' Updated", newShopItem.Name),
"tags": tags,
"tags": tags,
})
c.HTML(http.StatusOK, "edititem.html", data)
@@ -395,28 +391,27 @@ func (rc *shopItemController) DeleteItemView(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "deleteitem.html", gin.H{ "error": err })
c.HTML(http.StatusBadRequest, "deleteitem.html", gin.H{"error": err})
}
fmt.Println(shopItem)
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
"error": "",
"success": "",
"shopItem": shopItem,
"tags": tags,
"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,
"error": err,
"success": "",
})
@@ -427,7 +422,7 @@ func (rc *shopItemController) DeleteItemHandler(c *gin.Context) {
fmt.Println(len(shopItems))
data := CreateSessionData(c, gin.H{
"title": "shopItem Page",
"title": "shopItem Page",
"shopItems": shopItems,
})
@@ -444,17 +439,17 @@ func (rc *shopItemController) TagHandler(ctx *gin.Context) {
if err != nil {
fmt.Println(err)
ctx.HTML(http.StatusBadRequest, "tagview.html", gin.H{ "error": err })
ctx.HTML(http.StatusBadRequest, "tagview.html", gin.H{"error": err})
return
}
if action == "update" {
tag.Name = name
tag, err = repositories.Tags.Update(tag)
if err != nil {
fmt.Println(err)
ctx.HTML(http.StatusBadRequest, "tagview.html", gin.H{ "error": err })
ctx.HTML(http.StatusBadRequest, "tagview.html", gin.H{"error": err})
return
}
}
@@ -471,14 +466,14 @@ func (rc *shopItemController) AddTagHandler(c *gin.Context) {
if err != nil {
fmt.Println(err)
c.HTML(http.StatusBadRequest, "tagview.html", gin.H{ "error": err })
c.HTML(http.StatusBadRequest, "tagview.html", gin.H{"error": err})
return
}
_, err = repositories.Tags.Create(tag)
if err != nil {
data := CreateSessionData(c, gin.H{
"error": err,
"error": err,
"success": "",
})
@@ -493,7 +488,7 @@ func (rc *shopItemController) TagView(c *gin.Context) {
tags, err := repositories.Tags.GetAll()
if err != nil {
c.HTML(http.StatusBadRequest, "tagview.html", gin.H{ "data": gin.H{ "error": err } })
c.HTML(http.StatusBadRequest, "tagview.html", gin.H{"data": gin.H{"error": err}})
}
data := CreateSessionData(c, gin.H{
@@ -544,7 +539,7 @@ func (rc *shopItemController) GetAllTags(c *gin.Context) {
}
func ReplyError(ctx *gin.Context, err error) {
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
}
func ReplyOK(ctx *gin.Context, message any) {