draft list

This commit is contained in:
Niko Abeler 2023-08-05 13:35:56 +02:00
parent 70902af7e0
commit 349aa0dd5d
11 changed files with 143 additions and 8 deletions

View File

@ -9,9 +9,10 @@
{{end}} {{end}}
</ul> </ul>
<h2>Files</h2> <h2>Content</h2>
<ul> <ul>
<li><a href="/admin/binaries/">Files</a></li> <li><a href="/admin/binaries/">Files</a></li>
<li><a href="/admin/drafts/">Drafts</a></li>
</ul> </ul>
<h2>Configurations</h2> <h2>Configurations</h2>

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<br> <br>
<br> <br>

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<br> <br>
<br> <br>

View File

@ -0,0 +1,44 @@
{{define "title"}}Index{{end}}
{{define "main"}}
<a href="/admin">&larr; Back</a>
<br>
<hr>
<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>
</hgroup>
</div>
<hr>
{{ end }}
</div>
<hr>
<nav class="row">
{{ if not .FirstPage }}
<div>
<a href="?page={{ .PrevPage }}">Prev</a>
</div>
{{ end }}
<div>Page {{.Page}}</div>
{{ if not .LastPage }}
<div>
<a href="?page={{ .NextPage }}">Next</a>
</div>
{{ end }}
</nav>
{{end}}

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<br> <br>
<br> <br>

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<h2>Site Settings</h2> <h2>Site Settings</h2>

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<h2>Create a List</h2> <h2>Create a List</h2>
<form action="/site-config/lists/create" method="post" enctype="multipart/form-data"> <form action="/site-config/lists/create" method="post" enctype="multipart/form-data">

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<h2>Create a Me Link</h2> <h2>Create a Me Link</h2>
<form action="/site-config/me/create" method="post" enctype="multipart/form-data"> <form action="/site-config/me/create" method="post" enctype="multipart/form-data">

View File

@ -2,7 +2,7 @@
{{define "main"}} {{define "main"}}
<a href="/admin">Back</a> <a href="/admin">&larr; Back</a>
<h2>Create a List</h2> <h2>Create a List</h2>
<form action="/site-config/menus/create" method="post" enctype="multipart/form-data"> <form action="/site-config/menus/create" method="post" enctype="multipart/form-data">

View File

@ -52,10 +52,12 @@ func NewWebApp(
// admin // admin
adminHandler := NewAdminHandler(configRepo, configRegister, typeRegistry) adminHandler := NewAdminHandler(configRepo, configRegister, typeRegistry)
draftHandler := NewDraftHandler(entryService, configRepo)
binaryManageHandler := NewBinaryManageHandler(configRepo, binService) binaryManageHandler := NewBinaryManageHandler(configRepo, binService)
admin := app.Group("/admin") admin := app.Group("/admin")
admin.Use(middleware.NewAuthMiddleware(authorService).Handle) admin.Use(middleware.NewAuthMiddleware(authorService).Handle)
admin.Get("/", adminHandler.Handle) admin.Get("/", adminHandler.Handle)
admin.Get("/drafts/", draftHandler.Handle)
admin.Get("/config/:config/", adminHandler.HandleConfigGet) admin.Get("/config/:config/", adminHandler.HandleConfigGet)
admin.Post("/config/:config/", adminHandler.HandleConfigPost) admin.Post("/config/:config/", adminHandler.HandleConfigPost)
admin.Get("/binaries/", binaryManageHandler.Handle) admin.Get("/binaries/", binaryManageHandler.Handle)

88
web/draft_handler.go Normal file
View File

@ -0,0 +1,88 @@
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 DraftHandler struct {
configRepo repository.ConfigRepository
entrySvc *app.EntryService
}
func NewDraftHandler(
entryService *app.EntryService,
configRepo repository.ConfigRepository,
) *DraftHandler {
return &DraftHandler{
entrySvc: entryService,
configRepo: configRepo,
}
}
type DraftRenderData struct {
Entries []model.Entry
Page int
NextPage int
PrevPage int
FirstPage bool
LastPage bool
}
func (h *DraftHandler) Handle(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)
siteConfig := getSiteConfig(h.configRepo)
entries, err := h.entrySvc.FindAllByType(&siteConfig.PrimaryListInclude, false, true)
if err != nil {
return err
}
// sort entries by date descending
sort.Slice(entries, func(i, j int) bool {
return entries[i].Title() < entries[j].Title()
})
// 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/draft_list", DraftRenderData{
Entries: entries,
Page: pageNum,
NextPage: pageNum + 1,
PrevPage: pageNum - 1,
FirstPage: pageNum == 1,
LastPage: lastPage,
})
}