diff --git a/controllers/shopItemController.go b/controllers/shopItemController.go index 5f019ed..55295ec 100644 --- a/controllers/shopItemController.go +++ b/controllers/shopItemController.go @@ -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) } diff --git a/repositories/shopItemRepository.go b/repositories/shopItemRepository.go index a787efb..73e2377 100644 --- a/repositories/shopItemRepository.go +++ b/repositories/shopItemRepository.go @@ -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) diff --git a/static/output.css b/static/output.css index b0d07ea..4333cf7 100644 --- a/static/output.css +++ b/static/output.css @@ -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; } diff --git a/views/edititem.html b/views/edititem.html index 5a5fc37..e690eb0 100644 --- a/views/edititem.html +++ b/views/edititem.html @@ -1,5 +1,8 @@ {{ template "header.html" . }} + + +
@@ -12,11 +15,31 @@
{{ .data.success }}
-
+
+