add next/previous button to edititemhtml

This commit is contained in:
2025-04-16 13:59:43 +02:00
parent 861343b338
commit 1688e61ccb
4 changed files with 128 additions and 45 deletions

View File

@@ -459,17 +459,11 @@ func GetCheckedTags(item models.ShopItem) ([]models.CheckedTag, error) {
return tags, nil
}
func (rc *shopItemController) EditItemView(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
}
func (rc *shopItemController) getEditTemplatData(shopItem models.ShopItem) (gin.H, error) {
tags, err := GetCheckedTags(shopItem)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
return gin.H{}, err
}
priceBW := ""
@@ -484,15 +478,47 @@ func (rc *shopItemController) EditItemView(c *gin.Context) {
}
}
data := CreateSessionData(c, gin.H{
templateData := gin.H{
"error": "",
"success": "",
"shopItem": shopItem,
"tags": tags,
"priceBW": priceBW,
"priceColored": priceColored,
})
}
id := fmt.Sprintf("%d", shopItem.ID)
nextShopItem, err := repositories.ShopItems.GetNextOfId(id)
if err == nil {
fmt.Println("Setting nextitem")
fmt.Println(nextShopItem)
templateData["nextShopItem"] = nextShopItem
}
previousShopItem, err := repositories.ShopItems.GetPreviousOfId(id)
if err == nil {
templateData["previousShopItem"] = previousShopItem
}
return templateData, nil
}
func (rc *shopItemController) EditItemView(c *gin.Context) {
id := c.Param("id")
shopItem, err := repositories.ShopItems.GetById(id)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
}
templateData, err := rc.getEditTemplatData(shopItem)
if err != nil {
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
}
data := CreateSessionData(c, templateData)
c.HTML(http.StatusOK, "edititem.html", data)
}
@@ -533,49 +559,22 @@ func (rc *shopItemController) EditItemHandler(c *gin.Context) {
newShopItem.PrintMode = shopItem.PrintMode
tags, err := GetCheckedTags(newShopItem)
templateData, err := rc.getEditTemplatData(newShopItem)
if err != nil {
c.HTML(http.StatusBadRequest, "edititem.html", gin.H{"error": err})
return
}
priceBW := ""
priceColored := ""
for _, variant := range newShopItem.Variants {
if variant.Name == "B/W" {
priceBW = fmt.Sprintf("%.2f", variant.Price)
}
if variant.Name == "Colored" {
priceColored = fmt.Sprintf("%.2f", variant.Price)
}
c.HTML(http.StatusBadRequest, "error.html", gin.H{"error": err})
}
_, err = repositories.ShopItems.Update(newShopItem)
if err != nil {
data := CreateSessionData(c, gin.H{
"error": err,
"success": "",
"shopItem": newShopItem,
"tags": tags,
"priceBW": priceBW,
"priceColored": priceColored,
})
templateData["error"] = err
data := CreateSessionData(c, templateData)
c.HTML(http.StatusOK, "edititem.html", data)
return
}
data := CreateSessionData(c, gin.H{
"error": "",
"success": fmt.Sprintf("Item '%s' Updated", newShopItem.Name),
"shopItem": newShopItem,
"tags": tags,
"priceBW": priceBW,
"priceColored": priceColored,
})
templateData["success"] = fmt.Sprintf("Item '%s' Updated", newShopItem.Name)
data := CreateSessionData(c, templateData)
c.HTML(http.StatusOK, "edititem.html", data)
}