2023-07-17 19:43:31 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"owl-blogs/app"
|
|
|
|
"owl-blogs/domain/model"
|
|
|
|
"owl-blogs/render"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SiteConfigListHandler struct {
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *app.SiteConfigService
|
|
|
|
typeRegistry *app.EntryTypeRegistry
|
2023-07-17 19:43:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type siteConfigListTemplateData struct {
|
|
|
|
Lists []model.EntryList
|
|
|
|
Types []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSiteConfigListHandler(
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *app.SiteConfigService,
|
2023-07-17 19:43:31 +00:00
|
|
|
typeRegistry *app.EntryTypeRegistry,
|
|
|
|
) *SiteConfigListHandler {
|
|
|
|
return &SiteConfigListHandler{
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService: siteConfigService,
|
|
|
|
typeRegistry: typeRegistry,
|
2023-07-17 19:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SiteConfigListHandler) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
types := make([]string, 0)
|
|
|
|
for _, t := range h.typeRegistry.Types() {
|
|
|
|
typeName, err := h.typeRegistry.TypeName(t)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
types = append(types, typeName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return render.RenderTemplateWithBase(
|
2024-02-25 13:01:55 +00:00
|
|
|
c, "views/site_config_list", siteConfigListTemplateData{
|
2023-07-19 18:25:42 +00:00
|
|
|
Lists: siteConfig.Lists,
|
2023-07-17 19:43:31 +00:00
|
|
|
Types: types,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SiteConfigListHandler) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
form, err := c.MultipartForm()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.Lists = append(siteConfig.Lists, model.EntryList{
|
2023-07-17 19:43:31 +00:00
|
|
|
Id: c.FormValue("Id"),
|
|
|
|
Title: c.FormValue("Title"),
|
|
|
|
Include: form.Value["Include"],
|
|
|
|
ListType: c.FormValue("ListType"),
|
|
|
|
})
|
|
|
|
|
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/lists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *SiteConfigListHandler) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(c.FormValue("idx"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:25:42 +00:00
|
|
|
siteConfig.Lists = append(siteConfig.Lists[:id], siteConfig.Lists[id+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/lists")
|
|
|
|
}
|