lists
This commit is contained in:
parent
b488f9b032
commit
b60980b368
|
@ -29,21 +29,26 @@ func (s *EntryService) FindById(id string) (model.Entry, error) {
|
||||||
return s.EntryRepository.FindById(id)
|
return s.EntryRepository.FindById(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EntryService) FindAllByType(types *[]string) ([]model.Entry, error) {
|
func (s *EntryService) filterEntries(entries []model.Entry, published bool, drafts bool) []model.Entry {
|
||||||
return s.EntryRepository.FindAll(types)
|
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) {
|
func (s *EntryService) FindAll() ([]model.Entry, error) {
|
||||||
entries, err := s.EntryRepository.FindAll(nil)
|
entries, err := s.EntryRepository.FindAll(nil)
|
||||||
if err != nil {
|
return s.filterEntries(entries, true, true), err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,7 +96,7 @@ var importCmd = &cobra.Command{
|
||||||
v2Config.AuthorName = v1Config.AuthorName
|
v2Config.AuthorName = v1Config.AuthorName
|
||||||
v2Config.Me = mes
|
v2Config.Me = mes
|
||||||
v2Config.Lists = lists
|
v2Config.Lists = lists
|
||||||
v2Config.PrimaryListInclude = v1Config.PrimaryListInclude
|
v2Config.PrimaryListInclude = importer.ConvertTypeList(v1Config.PrimaryListInclude, app.Registry)
|
||||||
v2Config.HeaderMenu = headerMenu
|
v2Config.HeaderMenu = headerMenu
|
||||||
v2Config.FooterMenu = footerMenu
|
v2Config.FooterMenu = footerMenu
|
||||||
|
|
||||||
|
|
|
@ -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}}
|
24
web/app.go
24
web/app.go
|
@ -28,18 +28,18 @@ func NewWebApp(
|
||||||
typeRegistry *app.EntryTypeRegistry,
|
typeRegistry *app.EntryTypeRegistry,
|
||||||
binService *app.BinaryService,
|
binService *app.BinaryService,
|
||||||
authorService *app.AuthorService,
|
authorService *app.AuthorService,
|
||||||
siteConfigRepo repository.ConfigRepository,
|
configRepo repository.ConfigRepository,
|
||||||
) *WebApp {
|
) *WebApp {
|
||||||
app := fiber.New()
|
app := fiber.New()
|
||||||
|
|
||||||
indexHandler := NewIndexHandler(entryService, siteConfigRepo)
|
indexHandler := NewIndexHandler(entryService, configRepo)
|
||||||
listHandler := NewListHandler(entryService)
|
listHandler := NewListHandler(entryService, configRepo)
|
||||||
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, siteConfigRepo)
|
entryHandler := NewEntryHandler(entryService, typeRegistry, authorService, configRepo)
|
||||||
mediaHandler := NewMediaHandler(binService)
|
mediaHandler := NewMediaHandler(binService)
|
||||||
rssHandler := NewRSSHandler(entryService)
|
rssHandler := NewRSSHandler(entryService)
|
||||||
loginHandler := NewLoginHandler(authorService, siteConfigRepo)
|
loginHandler := NewLoginHandler(authorService, configRepo)
|
||||||
editorListHandler := NewEditorListHandler(typeRegistry, siteConfigRepo)
|
editorListHandler := NewEditorListHandler(typeRegistry, configRepo)
|
||||||
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, siteConfigRepo)
|
editorHandler := NewEditorHandler(entryService, typeRegistry, binService, configRepo)
|
||||||
|
|
||||||
// Login
|
// Login
|
||||||
app.Get("/auth/login", loginHandler.HandleGet)
|
app.Get("/auth/login", loginHandler.HandleGet)
|
||||||
|
@ -56,21 +56,21 @@ func NewWebApp(
|
||||||
siteConfig := app.Group("/site-config")
|
siteConfig := app.Group("/site-config")
|
||||||
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
|
siteConfig.Use(middleware.NewAuthMiddleware(authorService).Handle)
|
||||||
|
|
||||||
siteConfigHandler := NewSiteConfigHandler(siteConfigRepo)
|
siteConfigHandler := NewSiteConfigHandler(configRepo)
|
||||||
siteConfig.Get("/", siteConfigHandler.HandleGet)
|
siteConfig.Get("/", siteConfigHandler.HandleGet)
|
||||||
siteConfig.Post("/", siteConfigHandler.HandlePost)
|
siteConfig.Post("/", siteConfigHandler.HandlePost)
|
||||||
|
|
||||||
siteConfigMeHandler := NewSiteConfigMeHandler(siteConfigRepo)
|
siteConfigMeHandler := NewSiteConfigMeHandler(configRepo)
|
||||||
siteConfig.Get("/me", siteConfigMeHandler.HandleGet)
|
siteConfig.Get("/me", siteConfigMeHandler.HandleGet)
|
||||||
siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate)
|
siteConfig.Post("/me/create/", siteConfigMeHandler.HandleCreate)
|
||||||
siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete)
|
siteConfig.Post("/me/delete/", siteConfigMeHandler.HandleDelete)
|
||||||
|
|
||||||
siteConfigListHandler := NewSiteConfigListHandler(siteConfigRepo, typeRegistry)
|
siteConfigListHandler := NewSiteConfigListHandler(configRepo, typeRegistry)
|
||||||
siteConfig.Get("/lists", siteConfigListHandler.HandleGet)
|
siteConfig.Get("/lists", siteConfigListHandler.HandleGet)
|
||||||
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
|
siteConfig.Post("/lists/create/", siteConfigListHandler.HandleCreate)
|
||||||
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
|
siteConfig.Post("/lists/delete/", siteConfigListHandler.HandleDelete)
|
||||||
|
|
||||||
siteConfigMenusHandler := NewSiteConfigMenusHandler(siteConfigRepo)
|
siteConfigMenusHandler := NewSiteConfigMenusHandler(configRepo)
|
||||||
siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet)
|
siteConfig.Get("/menus", siteConfigMenusHandler.HandleGet)
|
||||||
siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate)
|
siteConfig.Post("/menus/create/", siteConfigMenusHandler.HandleCreate)
|
||||||
siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete)
|
siteConfig.Post("/menus/delete/", siteConfigMenusHandler.HandleDelete)
|
||||||
|
@ -106,7 +106,7 @@ func NewWebApp(
|
||||||
Registry: typeRegistry,
|
Registry: typeRegistry,
|
||||||
BinaryService: binService,
|
BinaryService: binService,
|
||||||
AuthorService: authorService,
|
AuthorService: authorService,
|
||||||
SiteConfigRepo: siteConfigRepo,
|
SiteConfigRepo: configRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,7 +37,10 @@ type indexRenderData struct {
|
||||||
|
|
||||||
func (h *IndexHandler) Handle(c *fiber.Ctx) error {
|
func (h *IndexHandler) Handle(c *fiber.Ctx) error {
|
||||||
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -73,7 +76,7 @@ func (h *IndexHandler) Handle(c *fiber.Ctx) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return render.RenderTemplateWithBase(c, getSiteConfig(h.configRepo), "views/index", indexRenderData{
|
return render.RenderTemplateWithBase(c, siteConfig, "views/index", indexRenderData{
|
||||||
Entries: entries,
|
Entries: entries,
|
||||||
Page: pageNum,
|
Page: pageNum,
|
||||||
NextPage: pageNum + 1,
|
NextPage: pageNum + 1,
|
||||||
|
|
|
@ -2,18 +2,99 @@ package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"owl-blogs/app"
|
"owl-blogs/app"
|
||||||
|
"owl-blogs/app/repository"
|
||||||
|
"owl-blogs/domain/model"
|
||||||
|
"owl-blogs/render"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListHandler struct {
|
type ListHandler struct {
|
||||||
entrySvc *app.EntryService
|
configRepo repository.ConfigRepository
|
||||||
|
entrySvc *app.EntryService
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListHandler(entryService *app.EntryService) *ListHandler {
|
func NewListHandler(
|
||||||
return &ListHandler{entrySvc: entryService}
|
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 {
|
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,
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue