owl-blogs/web/siteconfig_me_handler.go

78 lines
1.6 KiB
Go
Raw Permalink Normal View History

2023-07-17 19:43:31 +00:00
package web
import (
"owl-blogs/app"
2023-07-17 19:43:31 +00:00
"owl-blogs/domain/model"
"owl-blogs/render"
"strconv"
"github.com/gofiber/fiber/v2"
)
type SiteConfigMeHandler struct {
siteConfigService *app.SiteConfigService
2023-07-17 19:43:31 +00:00
}
func NewSiteConfigMeHandler(siteConfigService *app.SiteConfigService) *SiteConfigMeHandler {
2023-07-17 19:43:31 +00:00
return &SiteConfigMeHandler{
siteConfigService: siteConfigService,
2023-07-17 19:43:31 +00:00
}
}
func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig, err := h.siteConfigService.GetSiteConfig()
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
return render.RenderTemplateWithBase(
c, "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, err := h.siteConfigService.GetSiteConfig()
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.siteConfigService.UpdateSiteConfig(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, err := h.siteConfigService.GetSiteConfig()
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.siteConfigService.UpdateSiteConfig(siteConfig)
2023-07-17 19:43:31 +00:00
if err != nil {
return err
}
return c.Redirect("/site-config/me")
}