2023-06-25 19:32:36 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"owl-blogs/app"
|
2023-07-19 18:45:42 +00:00
|
|
|
"owl-blogs/domain/model"
|
|
|
|
"owl-blogs/render"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
2023-06-25 19:32:36 +00:00
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ListHandler struct {
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *app.SiteConfigService
|
|
|
|
entrySvc *app.EntryService
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 18:45:42 +00:00
|
|
|
func NewListHandler(
|
|
|
|
entryService *app.EntryService,
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfigService *app.SiteConfigService,
|
2023-07-19 18:45:42 +00:00
|
|
|
) *ListHandler {
|
|
|
|
return &ListHandler{
|
2024-02-25 13:01:55 +00:00
|
|
|
entrySvc: entryService,
|
|
|
|
siteConfigService: siteConfigService,
|
2023-07-19 18:45:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type listRenderData struct {
|
|
|
|
List model.EntryList
|
|
|
|
Entries []model.Entry
|
|
|
|
Page int
|
|
|
|
NextPage int
|
|
|
|
PrevPage int
|
|
|
|
FirstPage bool
|
|
|
|
LastPage bool
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *ListHandler) Handle(c *fiber.Ctx) error {
|
2023-07-19 18:45:42 +00:00
|
|
|
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
siteConfig, err := h.siteConfigService.GetSiteConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-19 18:45:42 +00:00
|
|
|
listId := c.Params("list")
|
|
|
|
list := model.EntryList{}
|
|
|
|
for _, l := range siteConfig.Lists {
|
|
|
|
if l.Id == listId {
|
|
|
|
list = l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if list.Id == "" {
|
|
|
|
return c.SendStatus(404)
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := h.entrySvc.FindAllByType(&list.Include, true, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort entries by date descending
|
|
|
|
sort.Slice(entries, func(i, j int) bool {
|
|
|
|
return entries[i].PublishedAt().After(*entries[j].PublishedAt())
|
|
|
|
})
|
|
|
|
|
|
|
|
// pagination
|
|
|
|
page := c.Query("page")
|
|
|
|
if page == "" {
|
|
|
|
page = "1"
|
|
|
|
}
|
|
|
|
pageNum, err := strconv.Atoi(page)
|
|
|
|
if err != nil {
|
|
|
|
pageNum = 1
|
|
|
|
}
|
|
|
|
limit := 10
|
|
|
|
offset := (pageNum - 1) * limit
|
|
|
|
lastPage := false
|
|
|
|
if offset > len(entries) {
|
|
|
|
offset = len(entries)
|
|
|
|
lastPage = true
|
|
|
|
}
|
|
|
|
if offset+limit > len(entries) {
|
|
|
|
limit = len(entries) - offset
|
|
|
|
lastPage = true
|
|
|
|
}
|
|
|
|
entries = entries[offset : offset+limit]
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-25 13:01:55 +00:00
|
|
|
return render.RenderTemplateWithBase(c, "views/list", listRenderData{
|
2023-07-19 18:45:42 +00:00
|
|
|
List: list,
|
|
|
|
Entries: entries,
|
|
|
|
Page: pageNum,
|
|
|
|
NextPage: pageNum + 1,
|
|
|
|
PrevPage: pageNum - 1,
|
|
|
|
FirstPage: pageNum == 1,
|
|
|
|
LastPage: lastPage,
|
|
|
|
})
|
|
|
|
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|