2023-07-17 19:43:31 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2024-02-25 13:01:55 +00:00
|
|
|
"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 {
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *app.SiteConfigService
|
2023-07-17 19:43:31 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
func NewSiteConfigMeHandler(siteConfigService *app.SiteConfigService) *SiteConfigMeHandler {
|
2023-07-17 19:43:31 +00:00
|
|
|
return &SiteConfigMeHandler{
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService: siteConfigService,
|
2023-07-17 19:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SiteConfigMeHandler) HandleGet(c *fiber.Ctx) error {
|
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfig, err := h.siteConfigService.GetSiteConfig()
|
2023-07-17 19:43:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return render.RenderTemplateWithBase(
|
2024-02-25 13:01:55 +00:00
|
|
|
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)
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfig, err := h.siteConfigService.GetSiteConfig()
|
2023-07-19 18:25:42 +00:00
|
|
|
|
2023-07-17 19:43:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.Me = append(siteConfig.Me, model.MeLinks{
|
2023-07-17 19:43:31 +00:00
|
|
|
Name: c.FormValue("Name"),
|
|
|
|
Url: c.FormValue("Url"),
|
|
|
|
})
|
|
|
|
|
2024-02-25 13:01:55 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SiteConfigMeHandler) HandleDelete(c *fiber.Ctx) error {
|
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfig, err := h.siteConfigService.GetSiteConfig()
|
2023-07-19 18:25:42 +00:00
|
|
|
|
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
|
|
|
|
}
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.Me = append(siteConfig.Me[:idx], siteConfig.Me[idx+1:]...)
|
2023-07-17 19:43:31 +00:00
|
|
|
|
2024-02-25 13:01:55 +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")
|
|
|
|
}
|