2023-06-25 19:32:36 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"owl-blogs/app"
|
2023-07-07 19:04:25 +00:00
|
|
|
"owl-blogs/domain/model"
|
|
|
|
"text/template"
|
2023-06-25 19:32:36 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EntryHandler struct {
|
|
|
|
entrySvc *app.EntryService
|
2023-07-07 19:04:25 +00:00
|
|
|
registry *app.EntryTypeRegistry
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 19:04:25 +00:00
|
|
|
func NewEntryHandler(entryService *app.EntryService, registry *app.EntryTypeRegistry) *EntryHandler {
|
|
|
|
return &EntryHandler{entrySvc: entryService, registry: registry}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *EntryHandler) getTemplate(entry model.Entry) (*template.Template, error) {
|
|
|
|
name, err := h.registry.TypeName(entry)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return template.ParseFS(
|
|
|
|
templates,
|
|
|
|
"templates/base.tmpl",
|
|
|
|
"templates/views/entry/"+name+".tmpl",
|
|
|
|
)
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *EntryHandler) Handle(c *fiber.Ctx) error {
|
2023-07-07 19:04:25 +00:00
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
|
|
|
|
|
|
|
entryId := c.Params("post")
|
|
|
|
entry, err := h.entrySvc.FindById(entryId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
template, err := h.getTemplate(entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return template.ExecuteTemplate(c, "base", entry)
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|