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)
}

View File

@@ -1,6 +1,7 @@
package repositories
import (
"fmt"
"gorm.io/gorm"
"strconv"
@@ -12,6 +13,8 @@ type ShopItemRepository interface {
GetAll() ([]models.ShopItem, error)
GetAllPublic() ([]models.ShopItem, error)
GetById(string) (models.ShopItem, error)
GetNextOfId(string) (models.ShopItem, error)
GetPreviousOfId(string) (models.ShopItem, error)
GetByTagId(string) ([]models.ShopItem, error)
GetVariantById(string) (models.ItemVariant, error)
Update(models.ShopItem) (models.ShopItem, error)
@@ -68,6 +71,30 @@ func (r *GORMShopItemRepository) GetById(id string) (models.ShopItem, error) {
return shopItem, nil
}
func (r *GORMShopItemRepository) GetNextOfId(id string) (models.ShopItem, error) {
var nextItem models.ShopItem
if err := r.DB.Where("id > ?", id).Order("id asc").First(&nextItem).Error; err != nil {
if err != gorm.ErrRecordNotFound {
return models.ShopItem{}, err
} else {
return models.ShopItem{}, fmt.Errorf("No Item found")
}
}
return nextItem, nil
}
func (r *GORMShopItemRepository) GetPreviousOfId(id string) (models.ShopItem, error) {
var previousItem models.ShopItem
if err := r.DB.Where("id < ?", id).Order("id desc").First(&previousItem).Error; err != nil {
if err != gorm.ErrRecordNotFound {
return models.ShopItem{}, err
} else {
return models.ShopItem{}, fmt.Errorf("No Item found")
}
}
return previousItem, nil
}
func (r *GORMShopItemRepository) GetByTagId(id string) ([]models.ShopItem, error) {
tagId, err := strconv.Atoi(id)

View File

@@ -587,6 +587,10 @@ video {
grid-column: span 12 / span 12;
}
.col-span-3 {
grid-column: span 3 / span 3;
}
.m-2 {
margin: 0.5rem;
}
@@ -806,6 +810,26 @@ video {
grid-template-columns: repeat(12, minmax(0, 1fr));
}
.grid-cols-2 {
grid-template-columns: repeat(2, minmax(0, 1fr));
}
.grid-cols-3 {
grid-template-columns: repeat(3, minmax(0, 1fr));
}
.grid-cols-6 {
grid-template-columns: repeat(6, minmax(0, 1fr));
}
.grid-cols-4 {
grid-template-columns: repeat(4, minmax(0, 1fr));
}
.grid-rows-6 {
grid-template-rows: repeat(6, minmax(0, 1fr));
}
.flex-col {
flex-direction: column;
}
@@ -1221,6 +1245,16 @@ video {
background-color: rgb(24 24 27 / var(--tw-bg-opacity, 1));
}
.bg-green-500 {
--tw-bg-opacity: 1;
background-color: rgb(34 197 94 / var(--tw-bg-opacity, 1));
}
.bg-gray-700 {
--tw-bg-opacity: 1;
background-color: rgb(55 65 81 / var(--tw-bg-opacity, 1));
}
.fill-red-50 {
fill: #fef2f2;
}

View File

@@ -1,5 +1,8 @@
{{ template "header.html" . }}
<div class="flex min-h-full flex-col justify-center px-6 py-12 lg:px-8">
<div class="sm:mx-auto sm:w-full sm:max-w-sm">
<img class="mx-auto h-10 w-auto" src="/static/img/logo-black.png" alt="Your Company">
@@ -12,11 +15,31 @@
{{ .data.success }}
</p>
<a href="/shopitems/{{ .data.shopItem.ID }}">
<a href="/{{ .data.shopItem.Pdf }}" target="_blank">
<img src="/{{ .data.shopItem.Image }}" alt="Product Image" class="aspect-4/5 mx-auto rounded-md bg-gray-200 object-cover group-hover:opacity-75 lg:aspect-auto lg:h-80">
</a>
<div class="grid grid-cols-6 gap-4 mb-4">
<div class="col-span-3">
{{ if .data.previousShopItem }}
<form action="/shopitems/{{ .data.previousShopItem.ID }}/edit">
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm/6 font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Previous</button>
</form>
{{ end }}
</div>
<div class="col-span-3">
{{ if .data.nextShopItem }}
<form action="/shopitems/{{ .data.nextShopItem.ID }}/edit">
<button type="submit" class="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm/6
font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2
focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Next</button>
</form>
{{ end }}
</div>
</div>
</div>