add item adding/viewing

This commit is contained in:
2025-03-02 20:29:38 +01:00
parent ba5dbae5eb
commit 31f4422739
3 changed files with 77 additions and 4 deletions

View File

@@ -21,6 +21,9 @@ type CRUDController interface {
type ShopItemController interface {
CRUDController
ShopItemView(*gin.Context)
AddItemView(*gin.Context)
AddItemHandler(*gin.Context)
}
type shopItemController struct {}
@@ -113,6 +116,57 @@ func (rc *shopItemController) Delete(c *gin.Context) {
ReplyOK(c, "shopItem was deleted")
}
func (rc *shopItemController) ShopItemView(c *gin.Context) {
shopItem, err := repositories.ShopItems.GetById(c.Param("id"))
data := CreateSessionData(c, gin.H{
"shopItem": shopItem,
})
if err != nil {
c.HTML(http.StatusBadRequest, "shopitem.html", data)
}
c.HTML(http.StatusOK, "shopitem.html", data)
}
func (rc *shopItemController) AddItemView(c *gin.Context) {
data := CreateSessionData(c, gin.H{
"error": "",
"success": "",
})
c.HTML(http.StatusOK, "additem.html", data)
}
func (rc *shopItemController) AddItemHandler(c *gin.Context) {
shopItem, err := models.NewShopItem(c)
if err != nil {
ReplyError(c, err)
}
_, err = repositories.ShopItems.Create(shopItem)
if err != nil {
data := CreateSessionData(c, gin.H{
"error": err,
"success": "",
})
c.HTML(http.StatusOK, "additem.html", data)
return
}
data := CreateSessionData(c, gin.H{
"error": "",
"success": fmt.Sprintf("Item '%s' Registered", shopItem.Name),
})
c.HTML(http.StatusOK, "additem.html", data)
}
func ReplyError(ctx *gin.Context, err error) {
ctx.JSON(http.StatusBadRequest, gin.H{ "error": err.Error() })
}