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-07-13 19:20:00 +00:00
|
|
|
"owl-blogs/domain/model"
|
2023-07-09 17:51:49 +00:00
|
|
|
"owl-blogs/render"
|
2023-06-25 19:32:36 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type EntryHandler struct {
|
2023-07-19 18:25:42 +00:00
|
|
|
configRepo repository.ConfigRepository
|
2023-07-17 18:44:59 +00:00
|
|
|
entrySvc *app.EntryService
|
|
|
|
authorSvc *app.AuthorService
|
|
|
|
registry *app.EntryTypeRegistry
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 19:20:00 +00:00
|
|
|
type entryData struct {
|
2023-07-25 19:31:03 +00:00
|
|
|
Entry model.Entry
|
|
|
|
Author *model.Author
|
|
|
|
LoggedIn bool
|
2023-07-13 19:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 18:44:59 +00:00
|
|
|
func NewEntryHandler(
|
|
|
|
entryService *app.EntryService,
|
|
|
|
registry *app.EntryTypeRegistry,
|
|
|
|
authorService *app.AuthorService,
|
2023-07-19 18:25:42 +00:00
|
|
|
configRepo repository.ConfigRepository,
|
2023-07-17 18:44:59 +00:00
|
|
|
) *EntryHandler {
|
|
|
|
return &EntryHandler{
|
|
|
|
entrySvc: entryService,
|
|
|
|
authorSvc: authorService,
|
|
|
|
registry: registry,
|
|
|
|
configRepo: configRepo,
|
|
|
|
}
|
2023-07-07 19:04:25 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
2023-08-05 08:14:05 +00:00
|
|
|
loggedIn := c.Locals("author") != nil
|
|
|
|
|
2023-07-07 19:04:25 +00:00
|
|
|
entryId := c.Params("post")
|
|
|
|
entry, err := h.entrySvc.FindById(entryId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-05 08:14:05 +00:00
|
|
|
if !loggedIn {
|
|
|
|
if entry.PublishedAt() == nil || entry.PublishedAt().IsZero() {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, "Entry not found")
|
|
|
|
}
|
2023-07-30 09:29:30 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 19:55:08 +00:00
|
|
|
author, err := h.authorSvc.FindByName(entry.AuthorId())
|
2023-07-13 19:20:00 +00:00
|
|
|
if err != nil {
|
2023-07-13 19:55:08 +00:00
|
|
|
author = &model.Author{}
|
2023-07-13 19:20:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-25 19:31:03 +00:00
|
|
|
return render.RenderTemplateWithBase(
|
|
|
|
c,
|
|
|
|
getSiteConfig(h.configRepo),
|
|
|
|
"views/entry",
|
|
|
|
entryData{
|
|
|
|
Entry: entry,
|
|
|
|
Author: author,
|
2023-08-05 08:14:05 +00:00
|
|
|
LoggedIn: loggedIn,
|
2023-07-25 19:31:03 +00:00
|
|
|
},
|
|
|
|
)
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|