mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 02:25:30 +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/pagemodels"
|
||||
"github.com/Theodor-Springmann-Stiftung/musenalm/templating"
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"github.com/pocketbase/pocketbase/tools/router"
|
||||
)
|
||||
|
||||
const (
|
||||
URL_ALMANACH = "/almanach/{id}/"
|
||||
TEMPLATE_ALMANACH = "/almanach/"
|
||||
URL_ALMANACH = "/almanach/{id}/"
|
||||
URL_ALMANACH_CONTENTS = "/almanach/{id}/contents/"
|
||||
TEMPLATE_ALMANACH = "/almanach/"
|
||||
TEMPLATE_ALMANACH_CONTENTS = "/almanach/contents/"
|
||||
)
|
||||
|
||||
// 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 {
|
||||
app := ia.Core()
|
||||
router.GET(p.URL, p.GET(engine, app))
|
||||
router.GET(URL_ALMANACH_CONTENTS, p.GETContents(engine, app))
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -91,7 +95,7 @@ func (p *AlmanachPage) GET(engine *templating.Engine, app core.App) HandleFunc {
|
||||
id := e.Request.PathValue("id")
|
||||
data := make(map[string]any)
|
||||
filters := NewBeitraegeFilterParameters(e)
|
||||
result, err := NewAlmanachResult(app, id, filters)
|
||||
result, err := NewAlmanachEntryResult(app, id)
|
||||
if 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 {
|
||||
Entry *dbmodels.Entry
|
||||
Places []*dbmodels.Place
|
||||
@@ -113,13 +133,181 @@ type AlmanachResult struct {
|
||||
EntriesAgents []*dbmodels.REntriesAgents
|
||||
ContentsAgents map[string][]*dbmodels.RContentsAgents // <- Key is content id
|
||||
|
||||
Types []string
|
||||
HasScans bool
|
||||
Types []string
|
||||
HasScans bool
|
||||
HasContents bool
|
||||
|
||||
PrevByTitle *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) {
|
||||
// INFO: what about sql.ErrNoRows?
|
||||
// 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 {
|
||||
return nil, err
|
||||
}
|
||||
hasContents := len(contents) > 0
|
||||
|
||||
items, err := dbmodels.Items_Entry(app, entry.Id)
|
||||
if err != nil {
|
||||
@@ -235,6 +424,7 @@ func NewAlmanachResult(app core.App, id string, params BeitraegeFilterParameters
|
||||
ContentsAgents: caMap,
|
||||
Types: types,
|
||||
HasScans: hs,
|
||||
HasContents: hasContents,
|
||||
PrevByTitle: prevByTitle,
|
||||
NextByTitle: nextByTitle,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user