This commit is contained in:
Niko Abeler 2023-07-19 20:45:42 +02:00
parent b488f9b032
commit b60980b368
6 changed files with 165 additions and 32 deletions

View File

@ -29,21 +29,26 @@ func (s *EntryService) FindById(id string) (model.Entry, error) {
return s.EntryRepository.FindById(id)
}
func (s *EntryService) FindAllByType(types *[]string) ([]model.Entry, error) {
return s.EntryRepository.FindAll(types)
func (s *EntryService) filterEntries(entries []model.Entry, published bool, drafts bool) []model.Entry {
filteredEntries := make([]model.Entry, 0)
for _, entry := range entries {
if published && entry.PublishedAt() != nil && !entry.PublishedAt().IsZero() {
filteredEntries = append(filteredEntries, entry)
}
if drafts && (entry.PublishedAt() == nil || entry.PublishedAt().IsZero()) {
filteredEntries = append(filteredEntries, entry)
}
}
return filteredEntries
}
func (s *EntryService) FindAllByType(types *[]string, published bool, drafts bool) ([]model.Entry, error) {
entries, err := s.EntryRepository.FindAll(types)
return s.filterEntries(entries, published, drafts), err
}
func (s *EntryService) FindAll() ([]model.Entry, error) {
entries, err := s.EntryRepository.FindAll(nil)
if err != nil {
return nil, err
}
// filter unpublished entries
publishedEntries := make([]model.Entry, 0)
for _, entry := range entries {
if entry.PublishedAt() != nil && !entry.PublishedAt().IsZero() {
publishedEntries = append(publishedEntries, entry)
}
}
return publishedEntries, nil
return s.filterEntries(entries, true, true), err
}

View File

@ -96,7 +96,7 @@ var importCmd = &cobra.Command{
v2Config.AuthorName = v1Config.AuthorName
v2Config.Me = mes
v2Config.Lists = lists
v2Config.PrimaryListInclude = v1Config.PrimaryListInclude
v2Config.PrimaryListInclude = importer.ConvertTypeList(v1Config.PrimaryListInclude, app.Registry)
v2Config.HeaderMenu = headerMenu
v2Config.FooterMenu = footerMenu

View File

@ -0,0 +1,44 @@
{{define "title"}}Index{{end}}
{{define "main"}}
<div class="h-feed">
{{ range .Entries }}
<div class="h-entry">
<hgroup>
<h3>
<a class="u-url" href="/posts/{{ .ID }}">
{{if .Title}}
{{ .Title }}
{{else}}
#
{{end}}
</a>
</h3>
<small style="font-size: 0.75em;">
<time class="dt-published" datetime="{{ .PublishedAt }}">{{ .PublishedAt }}</time>
</small>
</hgroup>
{{ .Content }}
</div>
<hr>
{{ end }}
</div>
<hr>
<nav class="row">
{{ if not .FirstPage }}
<div>
<a href="/lists/{{.List.Id}}/?page={{ .PrevPage }}">Prev</a>
</div>
{{ end }}
<div>Page {{.Page}}</div>
{{ if not .LastPage }}
<div>
<a href="/lists/{{.List.Id}}/?page={{ .NextPage }}">Next</a>
</div>
{{ end }}
</nav>
{{end}}

View File

@ -28,18 +28,18 @@ func NewWebApp(
typeRegistry *app.EntryTypeRegistry,
binService *app.BinaryService,
authorService *app.AuthorService,
siteConfigRepo repository.ConfigRepository,
configRepo repository.ConfigRepository,
) *WebApp {
app := fiber.New()
indexHandler := NewIndexHandler(entryService, siteConfigRepo)
listHandler := NewListHandler(entryService)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, siteConfigRepo)
indexHandler := NewIndexHandler(entryService, configRepo)
listHandler := NewListHandler(entryService, configRepo)
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo)
mediaHandler := NewMediaHandler(binService)
rssHandler := NewRSSHandler(entryService)
loginHandler := NewLoginHandler(authorService, siteConfigRepo)
editorListHandler := NewEditorListHandler(typeRegistry, siteConfigRepo)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, siteConfigRepo)
loginHandler := NewLoginHandler(authorService, configRepo)
editorListHandler := NewEditorListHandler(typeRegistry, configRepo)
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, configRepo)
// Login
app.Get("/auth/login", loginHandler.HandleGet)
@ -56,21 +56,21 @@ func NewWebApp(
siteConfig := app.Group("/site-config")
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
siteConfigHandler := NewSiteConfigHandler(siteConfigRepo)
siteConfigHandler := NewSiteConfigHandler(configRepo)
siteConfig.Get("/", siteConfigHandler.HandleGet)
siteConfig.Post("/", siteConfigHandler.HandlePost)
siteConfigMeHandler := NewSiteConfigMeHandler(siteConfigRepo)
siteConfigMeHandler := NewSiteConfigMeHandler(configRepo)
siteConfig.Get("/me", siteConfigMeHandler.HandleGet)
siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate)
siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete)
siteConfigListHandler := NewSiteConfigListHandler(siteConfigRepo, typeRegistry)
siteConfigListHandler := NewSiteConfigListHandler(configRepo, typeRegistry)
siteConfig.Get("/lists", siteConfigListHandler.HandleGet)
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
siteConfigMenusHandler := NewSiteConfigMenusHandler(siteConfigRepo)
siteConfigMenusHandler := NewSiteConfigMenusHandler(configRepo)
siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet)
siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate)
siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete)
@ -106,7 +106,7 @@ func NewWebApp(
Registry: typeRegistry,
BinaryService: binService,
AuthorService: authorService,
SiteConfigRepo: siteConfigRepo,
SiteConfigRepo: configRepo,
}
}

View File

@ -37,7 +37,10 @@ type indexRenderData struct {
func (h *IndexHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
entries, err := h.entrySvc.FindAll()
siteConfig := getSiteConfig(h.configRepo)
entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, true, false)
if err != nil {
return err
}
@ -73,7 +76,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
return err
}
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/index", indexRenderData{
return render.RenderTemplateWithBase(c, siteConfig, "views/index", indexRenderData{
Entries: entries,
Page: pageNum,
NextPage: pageNum + 1,

View File

@ -2,18 +2,99 @@ package web
import (
"owl-blogs/app"
"owl-blogs/app/repository"
"owl-blogs/domain/model"
"owl-blogs/render"
"sort"
"strconv"
"github.com/gofiber/fiber/v2"
)
type ListHandler struct {
entrySvc *app.EntryService
configRepo repository.ConfigRepository
entrySvc *app.EntryService
}
func NewListHandler(entryService *app.EntryService) *ListHandler {
return &ListHandler{entrySvc: entryService}
func NewListHandler(
entryService *app.EntryService,
configRepo repository.ConfigRepository,
) *ListHandler {
return &ListHandler{
entrySvc: entryService,
configRepo: configRepo,
}
}
type listRenderData struct {
List model.EntryList
Entries []model.Entry
Page int
NextPage int
PrevPage int
FirstPage bool
LastPage bool
}
func (h *ListHandler) Handle(c *fiber.Ctx) error {
return c.SendString("Hello, List!")
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := getSiteConfig(h.configRepo)
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
}
return render.RenderTemplateWithBase(c, siteConfig, "views/list", listRenderData{
List: list,
Entries: entries,
Page: pageNum,
NextPage: pageNum + 1,
PrevPage: pageNum - 1,
FirstPage: pageNum == 1,
LastPage: lastPage,
})
}