mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 18:45:31 +00:00
Let's try separating content and alm info
This commit is contained in:
@@ -9,13 +9,16 @@ import (
|
|||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers/datatypes"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers/datatypes"
|
||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
|
||||||
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
||||||
|
"github.com/pocketbase/dbx"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
"github.com/pocketbase/pocketbase/tools/router"
|
"github.com/pocketbase/pocketbase/tools/router"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
URL_ALMANACH = "/almanach/{id}/"
|
URL_ALMANACH = "/almanach/{id}/"
|
||||||
TEMPLATE_ALMANACH = "/almanach/"
|
URL_ALMANACH_CONTENTS = "/almanach/{id}/contents/"
|
||||||
|
TEMPLATE_ALMANACH = "/almanach/"
|
||||||
|
TEMPLATE_ALMANACH_CONTENTS = "/almanach/contents/"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Simple in-memory cache for sorted entries
|
// Simple in-memory cache for sorted entries
|
||||||
@@ -83,6 +86,7 @@ type AlmanachPage struct {
|
|||||||
func (p *AlmanachPage) Setup(router *router.Router[*core.RequestEvent], ia pagemodels.IApp, engine *templating.Engine) error {
|
func (p *AlmanachPage) Setup(router *router.Router[*core.RequestEvent], ia pagemodels.IApp, engine *templating.Engine) error {
|
||||||
app := ia.Core()
|
app := ia.Core()
|
||||||
router.GET(p.URL, p.GET(engine, app))
|
router.GET(p.URL, p.GET(engine, app))
|
||||||
|
router.GET(URL_ALMANACH_CONTENTS, p.GETContents(engine, app))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,7 +95,7 @@ func (p *AlmanachPage) GET(engine *templating.Engine, app core.App) HandleFunc {
|
|||||||
id := e.Request.PathValue("id")
|
id := e.Request.PathValue("id")
|
||||||
data := make(map[string]any)
|
data := make(map[string]any)
|
||||||
filters := NewBeitraegeFilterParameters(e)
|
filters := NewBeitraegeFilterParameters(e)
|
||||||
result, err := NewAlmanachResult(app, id, filters)
|
result, err := NewAlmanachEntryResult(app, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
engine.Response404(e, err, nil)
|
engine.Response404(e, err, nil)
|
||||||
}
|
}
|
||||||
@@ -102,6 +106,22 @@ func (p *AlmanachPage) GET(engine *templating.Engine, app core.App) HandleFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *AlmanachPage) GETContents(engine *templating.Engine, app core.App) HandleFunc {
|
||||||
|
return func(e *core.RequestEvent) error {
|
||||||
|
id := e.Request.PathValue("id")
|
||||||
|
data := make(map[string]any)
|
||||||
|
filters := NewBeitraegeFilterParameters(e)
|
||||||
|
result, err := NewAlmanachContentsResult(app, id, filters)
|
||||||
|
if err != nil {
|
||||||
|
engine.Response404(e, err, nil)
|
||||||
|
}
|
||||||
|
data["result"] = result
|
||||||
|
data["filters"] = filters
|
||||||
|
|
||||||
|
return engine.Response200(e, TEMPLATE_ALMANACH_CONTENTS, data, "fragment")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type AlmanachResult struct {
|
type AlmanachResult struct {
|
||||||
Entry *dbmodels.Entry
|
Entry *dbmodels.Entry
|
||||||
Places []*dbmodels.Place
|
Places []*dbmodels.Place
|
||||||
@@ -113,13 +133,181 @@ type AlmanachResult struct {
|
|||||||
EntriesAgents []*dbmodels.REntriesAgents
|
EntriesAgents []*dbmodels.REntriesAgents
|
||||||
ContentsAgents map[string][]*dbmodels.RContentsAgents // <- Key is content id
|
ContentsAgents map[string][]*dbmodels.RContentsAgents // <- Key is content id
|
||||||
|
|
||||||
Types []string
|
Types []string
|
||||||
HasScans bool
|
HasScans bool
|
||||||
|
HasContents bool
|
||||||
|
|
||||||
PrevByTitle *dbmodels.Entry
|
PrevByTitle *dbmodels.Entry
|
||||||
NextByTitle *dbmodels.Entry
|
NextByTitle *dbmodels.Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func entryHasContents(app core.App, entryID string) (bool, error) {
|
||||||
|
if entryID == "" {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []struct {
|
||||||
|
Id string `db:"id"`
|
||||||
|
}
|
||||||
|
err := app.RecordQuery(dbmodels.CONTENTS_TABLE).
|
||||||
|
Select("id").
|
||||||
|
Where(dbx.HashExp{dbmodels.ENTRIES_TABLE: entryID}).
|
||||||
|
Limit(1).
|
||||||
|
All(&ret)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(ret) > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAlmanachEntryResult(app core.App, id string) (*AlmanachResult, error) {
|
||||||
|
entry, err := dbmodels.Entries_MusenalmID(app, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
places, err := dbmodels.Places_IDs(app, datatypes.ToAny(entry.Places()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
srelations, err := dbmodels.REntriesSeries_Entry(app, entry.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
sids := []any{}
|
||||||
|
srelationsMap := map[string]*dbmodels.REntriesSeries{}
|
||||||
|
for _, r := range srelations {
|
||||||
|
sids = append(sids, r.Series())
|
||||||
|
srelationsMap[r.Series()] = r
|
||||||
|
}
|
||||||
|
|
||||||
|
series, err := dbmodels.Series_IDs(app, sids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
entriesagents, err := dbmodels.REntriesAgents_Entry(app, entry.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
agentIDs := []any{}
|
||||||
|
for _, r := range entriesagents {
|
||||||
|
agentIDs = append(agentIDs, r.Agent())
|
||||||
|
}
|
||||||
|
|
||||||
|
agents, err := dbmodels.Agents_IDs(app, agentIDs)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
agentsMap := map[string]*dbmodels.Agent{}
|
||||||
|
for _, a := range agents {
|
||||||
|
agentsMap[a.Id] = a
|
||||||
|
}
|
||||||
|
|
||||||
|
prevByTitle, nextByTitle, err := entryNeighborsByPreferredTitle(app, entry.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
hasContents, err := entryHasContents(app, entry.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &AlmanachResult{
|
||||||
|
Entry: entry,
|
||||||
|
Places: places,
|
||||||
|
Series: series,
|
||||||
|
Agents: agentsMap,
|
||||||
|
EntriesSeries: srelationsMap,
|
||||||
|
EntriesAgents: entriesagents,
|
||||||
|
HasContents: hasContents,
|
||||||
|
PrevByTitle: prevByTitle,
|
||||||
|
NextByTitle: nextByTitle,
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewAlmanachContentsResult(app core.App, id string, params BeitraegeFilterParameters) (*AlmanachResult, error) {
|
||||||
|
entry, err := dbmodels.Entries_MusenalmID(app, id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
contents, err := dbmodels.Contents_Entry(app, entry.Id)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
types := Types_Contents(contents)
|
||||||
|
hs := HasScans(contents)
|
||||||
|
|
||||||
|
if params.OnlyScans {
|
||||||
|
cscans := []*dbmodels.Content{}
|
||||||
|
for _, c := range contents {
|
||||||
|
if len(c.Scans()) > 0 {
|
||||||
|
cscans = append(cscans, c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contents = cscans
|
||||||
|
}
|
||||||
|
|
||||||
|
if params.Type != "" {
|
||||||
|
cfiltered := []*dbmodels.Content{}
|
||||||
|
outer:
|
||||||
|
for _, c := range contents {
|
||||||
|
for _, t := range c.MusenalmType() {
|
||||||
|
if t == params.Type {
|
||||||
|
cfiltered = append(cfiltered, c)
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contents = cfiltered
|
||||||
|
}
|
||||||
|
|
||||||
|
dbmodels.Sort_Contents_Numbering(contents)
|
||||||
|
|
||||||
|
contentsagents, err := dbmodels.RContentsAgents_Contents(app, dbmodels.Ids(contents))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
caids := []any{}
|
||||||
|
caMap := map[string][]*dbmodels.RContentsAgents{}
|
||||||
|
for _, r := range contentsagents {
|
||||||
|
caids = append(caids, r.Agent())
|
||||||
|
caMap[r.Content()] = append(caMap[r.Content()], r)
|
||||||
|
}
|
||||||
|
|
||||||
|
agents, err := dbmodels.Agents_IDs(app, caids)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
agentsMap := map[string]*dbmodels.Agent{}
|
||||||
|
for _, a := range agents {
|
||||||
|
agentsMap[a.Id] = a
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &AlmanachResult{
|
||||||
|
Entry: entry,
|
||||||
|
Contents: contents,
|
||||||
|
Agents: agentsMap,
|
||||||
|
ContentsAgents: caMap,
|
||||||
|
Types: types,
|
||||||
|
HasScans: hs,
|
||||||
|
HasContents: len(contents) > 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewAlmanachResult(app core.App, id string, params BeitraegeFilterParameters) (*AlmanachResult, error) {
|
func NewAlmanachResult(app core.App, id string, params BeitraegeFilterParameters) (*AlmanachResult, error) {
|
||||||
// INFO: what about sql.ErrNoRows?
|
// INFO: what about sql.ErrNoRows?
|
||||||
// We don't get sql.ErrNoRows here, since dbx converts every empty slice or
|
// We don't get sql.ErrNoRows here, since dbx converts every empty slice or
|
||||||
@@ -155,6 +343,7 @@ func NewAlmanachResult(app core.App, id string, params BeitraegeFilterParameters
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
hasContents := len(contents) > 0
|
||||||
|
|
||||||
items, err := dbmodels.Items_Entry(app, entry.Id)
|
items, err := dbmodels.Items_Entry(app, entry.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -235,6 +424,7 @@ func NewAlmanachResult(app core.App, id string, params BeitraegeFilterParameters
|
|||||||
ContentsAgents: caMap,
|
ContentsAgents: caMap,
|
||||||
Types: types,
|
Types: types,
|
||||||
HasScans: hs,
|
HasScans: hs,
|
||||||
|
HasContents: hasContents,
|
||||||
PrevByTitle: prevByTitle,
|
PrevByTitle: prevByTitle,
|
||||||
NextByTitle: nextByTitle,
|
NextByTitle: nextByTitle,
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -60,6 +60,16 @@
|
|||||||
|
|
||||||
{{ template "entrydata" $model }}
|
{{ template "entrydata" $model }}
|
||||||
|
|
||||||
{{- if $model.result.Contents | len -}}
|
{{- if $model.result.HasContents -}}
|
||||||
{{ template "contents" $model }}
|
<div
|
||||||
|
id="almanachcontents"
|
||||||
|
hx-get="/almanach/{{ $model.result.Entry.MusenalmID }}/contents/{{- if $model.request.query -}}?{{- $model.request.query -}}{{- end -}}"
|
||||||
|
hx-trigger="load"
|
||||||
|
hx-target="this"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-indicator="body">
|
||||||
|
<div class="container-oversize mt-8 pt-0">
|
||||||
|
<div class="flex justify-center text-sm text-stone-600">Inhalt lädt…</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@
|
|||||||
<div class="flex flex-row items-center justify-center ml-2.5">
|
<div class="flex flex-row items-center justify-center ml-2.5">
|
||||||
<div class="block bg-stone-200 text-sm px-3 py-0.5 rounded mt-1">
|
<div class="block bg-stone-200 text-sm px-3 py-0.5 rounded mt-1">
|
||||||
<i class="ri-arrow-left-long-line"></i>
|
<i class="ri-arrow-left-long-line"></i>
|
||||||
<a href="./" hx-target="#almanachcontents" hx-select="#almanachcontents" hx-swap="outerHTML show:none" hx-indicator="body"> Alle Beiträge anzeigen </a>
|
<a href="/almanach/{{ $model.result.Entry.MusenalmID }}/contents/" hx-target="#almanachcontents" hx-select="#almanachcontents" hx-swap="outerHTML show:none" hx-indicator="body"> Alle Beiträge anzeigen </a>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{- end -}}
|
{{- end -}}
|
||||||
@@ -16,7 +16,7 @@
|
|||||||
{{ $isFra := false }}
|
{{ $isFra := false }}
|
||||||
{{ $isEng := false }}
|
{{ $isEng := false }}
|
||||||
|
|
||||||
{{- $hasContents := len $model.result.Contents -}}
|
{{- $hasContents := $model.result.HasContents -}}
|
||||||
|
|
||||||
|
|
||||||
<div class="container-oversize mt-12 pb-0 mb-0">
|
<div class="container-oversize mt-12 pb-0 mb-0">
|
||||||
|
|||||||
7
views/routes/almanach/contents/body.gohtml
Normal file
7
views/routes/almanach/contents/body.gohtml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{{ $model := . }}
|
||||||
|
|
||||||
|
{{- if $model.result.Contents | len -}}
|
||||||
|
{{ template "_contents" $model }}
|
||||||
|
{{- else -}}
|
||||||
|
<div id="almanachcontents"></div>
|
||||||
|
{{- end -}}
|
||||||
Reference in New Issue
Block a user