Akteure beginning

This commit is contained in:
Simon Martens
2024-12-26 01:26:51 +01:00
parent 4de0eab443
commit 86152bd46d
47 changed files with 61845 additions and 23644 deletions

View File

@@ -1,58 +1,85 @@
package viewmodels
import (
"maps"
"slices"
"strings"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
type AgentView struct {
Agents []xmlprovider.Agent
Works map[string][]xmlprovider.Work
Pieces map[string][]xmlprovider.Piece
type AgentsListView struct {
Search string
AvailableLetters []string
Agents map[string]AgentView
Sorted []string
}
func AgentsView(letterorid string, lib *xmlprovider.Library) *AgentView {
res := AgentView{}
lib.Agents.Items.Range(func(key, value interface{}) bool {
k := key.(string)
if strings.HasPrefix(k, letterorid) {
agent := value.(xmlprovider.Agent)
res.Agents = append(res.Agents, agent)
}
return true
})
type AgentView struct {
xmlprovider.Agent
Works []WorkByAgent
Pieces []PieceByAgent
}
res.Works = make(map[string][]xmlprovider.Work)
res.Pieces = make(map[string][]xmlprovider.Piece)
type WorkByAgent struct {
xmlprovider.Work
Reference xmlprovider.AgentRef
}
lib.Works.Items.Range(func(key, value interface{}) bool {
w := value.(xmlprovider.Work)
for _, a := range res.Agents {
type PieceByAgent struct {
xmlprovider.Piece
Reference xmlprovider.AgentRef
}
func AgentsView(letterorid string, lib *xmlprovider.Library) *AgentsListView {
res := AgentsListView{Search: letterorid, Agents: make(map[string]AgentView)}
av := make(map[string]bool)
if len(letterorid) == 1 {
// INFO: This is all persons beginning with a letter
for _, a := range lib.Agents.Array {
av[strings.ToUpper(a.ID[:1])] = true
if strings.HasPrefix(a.ID, letterorid) {
_, ok := res.Works[a.ID]
if !ok {
res.Works[a.ID] = []xmlprovider.Work{}
}
res.Works[a.ID] = append(res.Works[a.ID], w)
res.Sorted = append(res.Sorted, a.ID)
res.Agents[a.ID] = AgentView{Agent: a}
}
}
return true
})
lib.Pieces.Items.Range(func(key, value interface{}) bool {
p := value.(xmlprovider.Piece)
for _, a := range res.Agents {
if strings.HasPrefix(a.ID, letterorid) {
_, ok := res.Pieces[a.ID]
if !ok {
res.Pieces[a.ID] = []xmlprovider.Piece{}
}
res.Pieces[a.ID] = append(res.Pieces[a.ID], p)
} else {
// INFO: This is a specific person lookup by ID
for _, a := range lib.Agents.Array {
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}
break
}
}
return true
})
}
// TODO: We won't need to lock the library if we take down all routes during parsing
lib.Works.Lock()
for _, w := range lib.Works.Array {
if ref, ok := w.ReferencesAgent(letterorid); ok {
if entry, ok := res.Agents[ref.Ref]; ok {
entry.Works = append(entry.Works, WorkByAgent{Work: w, Reference: *ref})
}
}
}
lib.Works.Unlock()
lib.Pieces.Lock()
for _, p := range lib.Pieces.Array {
if ref, ok := p.ReferencesAgent(letterorid); ok {
if entry, ok := res.Agents[ref.Ref]; ok {
entry.Pieces = append(entry.Pieces, PieceByAgent{Piece: p, Reference: *ref})
}
}
}
lib.Pieces.Unlock()
res.AvailableLetters = slices.Collect(maps.Keys(av))
slices.Sort(res.AvailableLetters)
slices.Sort(res.Sorted)
return &res
}

View File

@@ -2,7 +2,6 @@ package viewmodels
import (
"fmt"
"log/slog"
"maps"
"slices"
@@ -10,20 +9,20 @@ import (
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
type PieceListitemVM struct {
type PieceByIssue struct {
xmlprovider.Piece
// TODO: this is a bit hacky, but it refences the page number of the piece in the issue
Reference xmlprovider.IssueRef
}
type PiecesByPage struct {
Items map[int][]PieceListitemVM
Items map[int][]PieceByIssue
Pages []int
}
// TODO: Next & Prev
type IssueVM struct {
IssueListitemVM
xmlprovider.Issue
Pieces PiecesByPage
AdditionalPieces PiecesByPage
}
@@ -34,35 +33,34 @@ func NewSingleIssueView(y string, no string, lib *xmlprovider.Library) (*IssueVM
return nil, fmt.Errorf("No issue found for %v-%v", y, no)
}
ivm, err := ListitemFromIssue(*issue)
sivm := IssueVM{Issue: *issue}
ppi, ppa, err := PiecesForIsssue(lib, *issue)
if err != nil {
return nil, err
}
sivm := IssueVM{IssueListitemVM: *ivm}
ppi, ppa, err := PiecesForIsssue(lib, *issue)
slices.Sort(ppi.Pages)
slices.Sort(ppa.Pages)
sivm.Pieces = *ppi
sivm.AdditionalPieces = *ppa
sivm.Pieces = ppi
sivm.AdditionalPieces = ppa
return &sivm, nil
}
func PiecesForIsssue(lib *xmlprovider.Library, issue xmlprovider.Issue) (*PiecesByPage, *PiecesByPage, error) {
func PiecesForIsssue(lib *xmlprovider.Library, issue xmlprovider.Issue) (PiecesByPage, PiecesByPage, error) {
year := issue.Datum.When.Year
ppi := PiecesByPage{Items: make(map[int][]PieceListitemVM)}
ppa := PiecesByPage{Items: make(map[int][]PieceListitemVM)}
ppi := PiecesByPage{Items: make(map[int][]PieceByIssue)}
ppa := PiecesByPage{Items: make(map[int][]PieceByIssue)}
// TODO: will we have to lock this, if we shutdown the server while loading the library?
lib.Pieces.Lock()
defer lib.Pieces.Unlock()
slog.Debug(fmt.Sprintf("Checking piece for year %v, number %v", year, issue.Number.No))
for _, piece := range lib.Pieces.Array {
if d, ok := piece.ReferencesIssue(year, issue.Number.No); ok {
slog.Debug(fmt.Sprintf("Found piece %v in issue %v-%v", piece, year, issue.Number.No))
p := PieceListitemVM{Piece: piece, Reference: *d}
p := PieceByIssue{Piece: piece, Reference: *d}
if d.Beilage > 0 {
functions.MapArrayInsert(ppa.Items, d.Von, p)
} else {
@@ -74,5 +72,5 @@ func PiecesForIsssue(lib *xmlprovider.Library, issue xmlprovider.Issue) (*Pieces
ppi.Pages = slices.Collect(maps.Keys(ppi.Items))
ppa.Pages = slices.Collect(maps.Keys(ppa.Items))
return &ppi, &ppa, nil
return ppi, ppa, nil
}

View File

@@ -1,25 +0,0 @@
package viewmodels
import (
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
const TLAYOUT = "2006-01-02"
type IssueListitemVM struct {
xmlprovider.Issue
No int
Day int
Month int
Year int
}
func ListitemFromIssue(i xmlprovider.Issue) (*IssueListitemVM, error) {
return &IssueListitemVM{
No: i.Number.No,
Issue: i,
Day: i.Datum.When.Day,
Month: i.Datum.When.Month,
Year: i.Datum.When.Year,
}, nil
}

View File

@@ -6,10 +6,11 @@ import (
"slices"
"sort"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/functions"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
type IssuesByMonth map[int][]IssueListitemVM
type IssuesByMonth map[int][]xmlprovider.Issue
func (ibm *IssuesByMonth) Sort() {
for _, issues := range *ibm {
@@ -34,9 +35,7 @@ func YearView(year int, lib *xmlprovider.Library) (*YearVM, error) {
y := issue.Datum.When.Year
years[y] = true
if y == year {
if issuevm, err := ListitemFromIssue(issue); err == nil {
issues[issuevm.Month] = append(issues[issuevm.Month], *issuevm)
}
functions.MapArrayInsert(issues, issue.Datum.When.Month, issue)
}
}
lib.Issues.Unlock()