owl-blogs/web/siteconfig_me_handler.go

83 lines
1.8 KiB
Go
Raw Normal View History

2023-07-17 19:43:31 +00:00
package web
import (
"owl-blogs/app/repository"
"owl-blogs/config"
2023-07-17 19:43:31 +00:00
"owl-blogs/domain/model"
"owl-blogs/render"
"strconv"
"github.com/gofiber/fiber/v2"
)
type SiteConfigMeHandler struct {
siteConfigRepo repository.ConfigRepository
2023-07-17 19:43:31 +00:00
}
func NewSiteConfigMeHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigMeHandler {
2023-07-17 19:43:31 +00:00
return &SiteConfigMeHandler{
siteConfigRepo: siteConfigRepo,
}
}
func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
return render.RenderTemplateWithBase(
c, getSiteConfig(h.siteConfigRepo), "views/site_config_me", siteConfig.Me)
2023-07-17 19:43:31 +00:00
}
func (h *SiteConfigMeHandler) HandleCreate(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
siteConfig.Me = append(siteConfig.Me, model.MeLinks{
2023-07-17 19:43:31 +00:00
Name: c.FormValue("Name"),
Url: c.FormValue("Url"),
})
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
return c.Redirect("/site-config/me")
}
func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
idx, err := strconv.Atoi(c.FormValue("idx"))
if err != nil {
return err
}
siteConfig.Me = append(siteConfig.Me[:idx], siteConfig.Me[idx+1:]...)
2023-07-17 19:43:31 +00:00
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
return c.Redirect("/site-config/me")
}