owl-blogs/web/siteconfig_handler.go

60 lines
1.5 KiB
Go
Raw Normal View History

2023-07-17 18:44:59 +00:00
package web
import (
"owl-blogs/app/repository"
"owl-blogs/config"
"owl-blogs/domain/model"
2023-07-17 18:44:59 +00:00
"owl-blogs/render"
"github.com/gofiber/fiber/v2"
)
type SiteConfigHandler struct {
siteConfigRepo repository.ConfigRepository
2023-07-17 18:44:59 +00:00
}
func NewSiteConfigHandler(siteConfigRepo repository.ConfigRepository) *SiteConfigHandler {
2023-07-17 18:44:59 +00:00
return &SiteConfigHandler{
siteConfigRepo: siteConfigRepo,
}
}
func (h *SiteConfigHandler) 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 18:44:59 +00:00
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, getSiteConfig(h.siteConfigRepo), "views/site_config", siteConfig)
2023-07-17 18:44:59 +00:00
}
func (h *SiteConfigHandler) HandlePost(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := model.SiteConfig{}
err := h.siteConfigRepo.Get(config.SITE_CONFIG, &siteConfig)
2023-07-17 18:44:59 +00:00
if err != nil {
return err
}
siteConfig.Title = c.FormValue("Title")
siteConfig.SubTitle = c.FormValue("SubTitle")
siteConfig.HeaderColor = c.FormValue("HeaderColor")
siteConfig.AuthorName = c.FormValue("AuthorName")
siteConfig.AvatarUrl = c.FormValue("AvatarUrl")
2023-07-20 17:49:52 +00:00
siteConfig.FullUrl = c.FormValue("FullUrl")
siteConfig.HtmlHeadExtra = c.FormValue("HtmlHeadExtra")
siteConfig.FooterExtra = c.FormValue("FooterExtra")
2023-07-17 18:44:59 +00:00
err = h.siteConfigRepo.Update(config.SITE_CONFIG, siteConfig)
2023-07-17 18:44:59 +00:00
if err != nil {
return err
}
return c.Redirect("/site-config/")
}