owl-blogs/web/editor_handler.go

49 lines
988 B
Go
Raw Normal View History

2023-06-25 19:32:36 +00:00
package web
import (
"owl-blogs/app"
2023-06-25 20:09:27 +00:00
"owl-blogs/domain/model"
"owl-blogs/web/editor"
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 {
entrySvc *app.EntryService
}
func NewEditorHandler(entryService *app.EntryService) *EditorHandler {
return &EditorHandler{entrySvc: entryService}
}
func (h *EditorHandler) HandleGet(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
form := editor.NewEntryForm(&model.ImageEntry{})
htmlForm, err := form.HtmlForm()
2023-07-06 16:56:43 +00:00
if err != nil {
return err
}
2023-07-06 17:36:24 +00:00
return c.SendString(htmlForm)
2023-06-25 19:32:36 +00:00
}
func (h *EditorHandler) HandlePost(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
form := editor.NewEntryForm(&model.ImageEntry{})
// get form data
metaData, err := form.Parse(c)
if err != nil {
return err
}
// create entry
now := time.Now()
err = h.entrySvc.Create(&model.ImageEntry{}, &now, metaData)
if err != nil {
return err
}
2023-06-25 19:32:36 +00:00
return c.SendString("Hello, Editor!")
}