owl-blogs/web/siteconfig_menus_handler.go

101 lines
2.3 KiB
Go
Raw Normal View History

2023-07-18 18:09:45 +00:00
package web
import (
"owl-blogs/app"
2023-07-18 18:09:45 +00:00
"owl-blogs/domain/model"
"owl-blogs/render"
"strconv"
"github.com/gofiber/fiber/v2"
)
type SiteConfigMenusHandler struct {
siteConfigService *app.SiteConfigService
2023-07-18 18:09:45 +00:00
}
type siteConfigMenusTemplateData struct {
HeaderMenu []model.MenuItem
FooterMenu []model.MenuItem
}
func NewSiteConfigMenusHandler(siteConfigService *app.SiteConfigService) *SiteConfigMenusHandler {
2023-07-18 18:09:45 +00:00
return &SiteConfigMenusHandler{
siteConfigService: siteConfigService,
2023-07-18 18:09:45 +00:00
}
}
func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig, err := h.siteConfigService.GetSiteConfig()
2023-07-18 18:09:45 +00:00
if err != nil {
return err
}
return render.RenderTemplateWithBase(
c, "views/site_config_menus", siteConfigMenusTemplateData{
HeaderMenu: siteConfig.HeaderMenu,
FooterMenu: siteConfig.FooterMenu,
2023-07-18 18:09:45 +00:00
})
}
func (h *SiteConfigMenusHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig, err := h.siteConfigService.GetSiteConfig()
2023-07-18 18:09:45 +00:00
if err != nil {
return err
}
menuItem := model.MenuItem{
Title: c.FormValue("Title"),
List: c.FormValue("List"),
Url: c.FormValue("Url"),
Post: c.FormValue("Post"),
}
if c.FormValue("menu") == "header" {
siteConfig.HeaderMenu = append(siteConfig.HeaderMenu, menuItem)
2023-07-18 18:09:45 +00:00
} else if c.FormValue("menu") == "footer" {
siteConfig.FooterMenu = append(siteConfig.FooterMenu, menuItem)
2023-07-18 18:09:45 +00:00
}
err = h.siteConfigService.UpdateSiteConfig(siteConfig)
2023-07-18 18:09:45 +00:00
if err != nil {
return err
}
return c.Redirect("/site-config/menus")
}
func (h *SiteConfigMenusHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig, err := h.siteConfigService.GetSiteConfig()
2023-07-18 18:09:45 +00:00
if err != nil {
return err
}
menu := c.FormValue("menu")
idx, err := strconv.Atoi(c.FormValue("idx"))
if err != nil {
return err
}
if menu == "header" {
siteConfig.HeaderMenu = append(siteConfig.HeaderMenu[:idx], siteConfig.HeaderMenu[idx+1:]...)
2023-07-18 18:09:45 +00:00
} else if menu == "footer" {
siteConfig.FooterMenu = append(siteConfig.FooterMenu[:idx], siteConfig.FooterMenu[idx+1:]...)
2023-07-18 18:09:45 +00:00
}
err = h.siteConfigService.UpdateSiteConfig(siteConfig)
2023-07-18 18:09:45 +00:00
if err != nil {
return err
}
return c.Redirect("/site-config/menus")
}