Added Lookup functions to template to do reverse lookups on works, issues and pieces

This commit is contained in:
Simon Martens
2025-01-08 03:04:12 +01:00
parent 6cfed94ff6
commit 2fa66f90e5
7 changed files with 35 additions and 51 deletions

View File

@@ -5,30 +5,18 @@ import (
"slices"
"strings"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/xmlmodels"
)
type AgentsListView struct {
Search string
AvailableLetters []string
Agents map[string]AgentView
Agents map[string]xmlmodels.Agent
Sorted []string
}
type AgentView struct {
xmlmodels.Agent
Works []WorkByAgent
Pieces []xmlprovider.Resolved[xmlmodels.Piece]
}
type WorkByAgent struct {
xmlprovider.Resolved[xmlmodels.Work]
Pieces []xmlprovider.Resolved[xmlmodels.Piece]
}
func AgentsView(letterorid string, lib *xmlmodels.Library) *AgentsListView {
res := AgentsListView{Search: letterorid, Agents: make(map[string]AgentView)}
res := AgentsListView{Search: letterorid, Agents: make(map[string]xmlmodels.Agent)}
av := make(map[string]bool)
if len(letterorid) == 1 {
@@ -37,7 +25,7 @@ func AgentsView(letterorid string, lib *xmlmodels.Library) *AgentsListView {
av[strings.ToUpper(a.ID[:1])] = true
if strings.HasPrefix(a.ID, letterorid) {
res.Sorted = append(res.Sorted, a.ID)
res.Agents[a.ID] = AgentView{Agent: a}
res.Agents[a.ID] = a
}
}
} else {
@@ -46,30 +34,12 @@ func AgentsView(letterorid string, lib *xmlmodels.Library) *AgentsListView {
av[strings.ToUpper(a.ID[:1])] = true
if a.ID == letterorid {
res.Sorted = append(res.Sorted, a.ID)
res.Agents[a.ID] = AgentView{Agent: a}
res.Agents[a.ID] = a
break
}
}
}
for _, a := range res.Agents {
if works, err := lib.Works.ReverseLookup(a); err == nil {
for _, w := range works {
if pieces, err := lib.Pieces.ReverseLookup(w.Item); err == nil {
// INFO: it makes no sense to append works that have no pieces attached
a.Works = append(a.Works, WorkByAgent{Resolved: w, Pieces: pieces})
}
}
}
if pieces, err := lib.Pieces.ReverseLookup(a.Agent); err == nil {
a.Pieces = pieces
}
// TODO: sort the things, also for works and pieces above
res.Agents[a.ID] = a
}
res.AvailableLetters = slices.Collect(maps.Keys(av))
slices.Sort(res.AvailableLetters)
slices.Sort(res.Sorted)