owl-blogs/web/editor_handler.go

179 lines
3.8 KiB
Go
Raw Normal View History

2023-06-25 19:32:36 +00:00
package web
import (
"owl-blogs/app"
2023-07-17 18:44:59 +00:00
"owl-blogs/app/repository"
2023-06-25 20:09:27 +00:00
"owl-blogs/domain/model"
2023-07-16 19:48:39 +00:00
"owl-blogs/render"
2023-07-25 19:31:03 +00:00
"owl-blogs/web/forms"
2023-07-06 17:36:24 +00:00
"time"
2023-06-25 19:32:36 +00:00
"github.com/gofiber/fiber/v2"
)
type EditorHandler struct {
configRepo repository.ConfigRepository
2023-07-17 18:44:59 +00:00
entrySvc *app.EntryService
binSvc *app.BinaryService
registry *app.EntryTypeRegistry
2023-06-25 19:32:36 +00:00
}
2023-07-08 11:28:06 +00:00
func NewEditorHandler(
entryService *app.EntryService,
registry *app.EntryTypeRegistry,
binService *app.BinaryService,
configRepo repository.ConfigRepository,
2023-07-08 11:28:06 +00:00
) *EditorHandler {
return &EditorHandler{
2023-07-17 18:44:59 +00:00
entrySvc: entryService,
registry: registry,
binSvc: binService,
configRepo: configRepo,
2023-07-08 11:28:06 +00:00
}
2023-07-06 20:16:52 +00:00
}
func (h *EditorHandler) paramToEntry(c *fiber.Ctx) (model.Entry, error) {
typeName := c.Params("editor")
entryType, err := h.registry.Type(typeName)
if err != nil {
return nil, err
}
return entryType, nil
2023-06-25 19:32:36 +00:00
}
2023-07-25 19:31:03 +00:00
func (h *EditorHandler) HandleGetNew(c *fiber.Ctx) error {
2023-07-05 20:03:00 +00:00
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
2023-07-06 20:16:52 +00:00
entryType, err := h.paramToEntry(c)
if err != nil {
return err
}
2023-07-25 19:31:03 +00:00
form := forms.NewForm(entryType.MetaData(), h.binSvc)
2023-07-06 17:36:24 +00:00
htmlForm, err := form.HtmlForm()
2023-07-06 16:56:43 +00:00
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm)
2023-06-25 19:32:36 +00:00
}
2023-07-25 19:31:03 +00:00
func (h *EditorHandler) HandlePostNew(c *fiber.Ctx) error {
2023-07-05 20:03:00 +00:00
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
2023-07-06 17:36:24 +00:00
2023-07-25 19:31:03 +00:00
entry, err := h.paramToEntry(c)
2023-07-06 20:16:52 +00:00
if err != nil {
return err
}
2023-07-25 19:31:03 +00:00
form := forms.NewForm(entry.MetaData(), h.binSvc)
2023-07-06 17:36:24 +00:00
// get form data
2023-07-25 19:31:03 +00:00
entryMeta, err := form.Parse(c)
2023-07-06 17:36:24 +00:00
if err != nil {
return err
}
// create entry
now := time.Now()
2023-07-25 19:31:03 +00:00
entry.SetMetaData(entryMeta)
published := c.FormValue("action") == "Publish"
if published {
entry.SetPublishedAt(&now)
} else {
entry.SetPublishedAt(nil)
}
2023-07-13 19:55:08 +00:00
entry.SetAuthorId(c.Locals("author").(string))
2023-07-09 20:12:06 +00:00
err = h.entrySvc.Create(entry)
2023-07-06 17:36:24 +00:00
if err != nil {
return err
}
2023-07-07 19:04:25 +00:00
return c.Redirect("/posts/" + entry.ID() + "/")
2023-07-06 17:36:24 +00:00
2023-06-25 19:32:36 +00:00
}
2023-07-25 19:31:03 +00:00
func (h *EditorHandler) HandleGetEdit(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
id := c.Params("id")
entry, err := h.entrySvc.FindById(id)
if err != nil {
return err
}
form := forms.NewForm(entry.MetaData(), h.binSvc)
htmlForm, err := form.HtmlForm()
if err != nil {
return err
}
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/editor", htmlForm)
}
func (h *EditorHandler) HandlePostEdit(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
id := c.Params("id")
entry, err := h.entrySvc.FindById(id)
if err != nil {
return err
}
form := forms.NewForm(entry.MetaData(), h.binSvc)
// get form data
meta, err := form.Parse(c)
if err != nil {
return err
}
published := c.FormValue("action") == "Publish"
if !published {
entry.SetPublishedAt(nil)
2023-08-05 08:15:08 +00:00
} else if entry.PublishedAt() == nil || entry.PublishedAt().IsZero() {
now := time.Now()
entry.SetPublishedAt(&now)
}
2023-07-25 19:31:03 +00:00
// update entry
entry.SetMetaData(meta)
err = h.entrySvc.Update(entry)
if err != nil {
return err
}
return c.Redirect("/posts/" + entry.ID() + "/")
}
2023-07-25 19:49:09 +00:00
func (h *EditorHandler) HandlePostDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
id := c.Params("id")
entry, err := h.entrySvc.FindById(id)
if err != nil {
return err
}
confirm := c.FormValue("confirm")
if confirm != "on" {
return c.Redirect("/posts/" + entry.ID() + "/")
}
err = h.entrySvc.Delete(entry)
if err != nil {
return err
}
return c.Redirect("/")
}
2023-07-30 09:29:30 +00:00
func (h *EditorHandler) HandlePostUnpublish(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
id := c.Params("id")
entry, err := h.entrySvc.FindById(id)
if err != nil {
return err
}
entry.SetPublishedAt(nil)
err = h.entrySvc.Update(entry)
if err != nil {
return err
}
return c.Redirect("/posts/" + entry.ID() + "/")
}