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-06 17:36:24 +00:00
|
|
|
"time"
|
2023-06-25 19:32:36 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EditorHandler struct {
|
2023-07-19 18:25:42 +00:00
|
|
|
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,
|
2023-07-19 18:25:42 +00:00
|
|
|
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
|
|
|
|
}
|
2024-02-14 18:53:49 +00:00
|
|
|
htmlForm := entryType.MetaData().Form(h.binSvc)
|
2024-02-25 13:01:55 +00:00
|
|
|
return render.RenderTemplateWithBase(c, "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
|
|
|
|
}
|
|
|
|
|
2024-02-21 19:24:18 +00:00
|
|
|
entryMeta := entry.MetaData()
|
|
|
|
err = entryMeta.ParseFormData(c, h.binSvc)
|
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)
|
2023-08-05 08:14:05 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-14 18:53:49 +00:00
|
|
|
htmlForm := entry.MetaData().Form(h.binSvc)
|
2024-02-25 13:01:55 +00:00
|
|
|
return render.RenderTemplateWithBase(c, "views/editor", htmlForm)
|
2023-07-25 19:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// get form data
|
2024-02-21 19:24:18 +00:00
|
|
|
meta := entry.MetaData()
|
|
|
|
err = meta.ParseFormData(c, h.binSvc)
|
2023-07-25 19:31:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-05 08:14:05 +00:00
|
|
|
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-08-05 08:14:05 +00:00
|
|
|
}
|
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() + "/")
|
|
|
|
}
|