2023-07-18 18:09:45 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"owl-blogs/app/repository"
|
2023-07-19 18:25:42 +00:00
|
|
|
"owl-blogs/config"
|
2023-07-18 18:09:45 +00:00
|
|
|
"owl-blogs/domain/model"
|
|
|
|
"owl-blogs/render"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SiteConfigMenusHandler struct {
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfigRepo repository.ConfigRepository
|
2023-07-18 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type siteConfigMenusTemplateData struct {
|
|
|
|
HeaderMenu []model.MenuItem
|
|
|
|
FooterMenu []model.MenuItem
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
func NewSiteConfigMenusHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMenusHandler {
|
2023-07-18 18:09:45 +00:00
|
|
|
return &SiteConfigMenusHandler{
|
|
|
|
siteConfigRepo: siteConfigRepo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SiteConfigMenusHandler) HandleGet(c *fiber.Ctx) error {
|
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig := model.SiteConfig{}
|
|
|
|
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
|
|
|
|
|
2023-07-18 18:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return render.RenderTemplateWithBase(
|
2023-07-19 18:25:42 +00:00
|
|
|
c, getSiteConfig(h.siteConfigRepo), "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)
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig := model.SiteConfig{}
|
|
|
|
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
|
|
|
|
|
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" {
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.HeaderMenu = append(siteConfig.HeaderMenu, menuItem)
|
2023-07-18 18:09:45 +00:00
|
|
|
} else if c.FormValue("menu") == "footer" {
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.FooterMenu = append(siteConfig.FooterMenu, menuItem)
|
2023-07-18 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
err = h.siteConfigRepo.Update(config.SITE_CONFIG, 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)
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig := model.SiteConfig{}
|
|
|
|
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
|
|
|
|
|
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" {
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.HeaderMenu = append(siteConfig.HeaderMenu[:idx], siteConfig.HeaderMenu[idx+1:]...)
|
2023-07-18 18:09:45 +00:00
|
|
|
} else if menu == "footer" {
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.FooterMenu = append(siteConfig.FooterMenu[:idx], siteConfig.FooterMenu[idx+1:]...)
|
2023-07-18 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
|
2023-07-18 18:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect("/site-config/menus")
|
|
|
|
}
|