added XSDTime datatype

This commit is contained in:
Simon Martens
2024-12-22 20:40:58 +01:00
parent ff3ed74b5e
commit 0ad9e0122f
13 changed files with 552 additions and 268 deletions

View File

@@ -1,44 +1,84 @@
package viewmodels
import (
"errors"
"time"
"fmt"
"log/slog"
"maps"
"slices"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/functions"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
const TLAYOUT = "2006-01-02"
type IssueViewModel struct {
*xmlprovider.Issue
Day int
Month int
Year int
type PieceListitemVM struct {
xmlprovider.Piece
// TODO: this is a bit hacky, but it refences the page number of the piece in the issue
Reference xmlprovider.IssueRef
}
func IssueView(y string, No string, lib *xmlprovider.Library) (*IssueViewModel, error) {
issue := lib.Issues.Item(y + "-" + No)
type PiecesByPage struct {
Items map[int][]PieceListitemVM
Pages []int
}
// TODO: Next & Prev
type IssueVM struct {
IssueListitemVM
Pieces PiecesByPage
AdditionalPieces PiecesByPage
}
func NewSingleIssueView(y string, no string, lib *xmlprovider.Library) (*IssueVM, error) {
issue := lib.Issues.Item(y + "-" + no)
if issue == nil {
return nil, errors.New("Issue not found")
return nil, fmt.Errorf("No issue found for %v-%v", y, no)
}
return FromIssue(issue)
}
func FromIssue(i *xmlprovider.Issue) (*IssueViewModel, error) {
t, err := time.Parse(TLAYOUT, i.Datum.When)
ivm, err := ListitemFromIssue(*issue)
if err != nil {
return nil, err
}
ivm := IssueViewModel{
Issue: i,
Day: t.Day(),
Month: int(t.Month()),
Year: t.Year(),
sivm := IssueVM{IssueListitemVM: *ivm}
ppi, ppa, err := PiecesForIsssue(lib, *issue)
slices.Sort(ppi.Pages)
slices.Sort(ppa.Pages)
sivm.Pieces = *ppi
sivm.AdditionalPieces = *ppa
return &sivm, nil
}
func PiecesForIsssue(lib *xmlprovider.Library, issue xmlprovider.Issue) (*PiecesByPage, *PiecesByPage, error) {
date := issue.Datum.Date()
if date == nil {
return nil, nil, fmt.Errorf("Issue has no date")
}
return &ivm, nil
year := date.Year()
ppi := PiecesByPage{Items: make(map[int][]PieceListitemVM)}
ppa := PiecesByPage{Items: make(map[int][]PieceListitemVM)}
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}
if d.Beilage > 0 {
functions.MapArrayInsert(ppa.Items, d.Von, p)
} else {
functions.MapArrayInsert(ppi.Items, d.Von, p)
}
}
}
ppi.Pages = slices.Collect(maps.Keys(ppi.Items))
ppa.Pages = slices.Collect(maps.Keys(ppa.Items))
return &ppi, &ppa, nil
}

View File

@@ -0,0 +1,32 @@
package viewmodels
import (
"time"
"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) {
t, err := time.Parse(TLAYOUT, i.Datum.When)
if err != nil {
return nil, err
}
return &IssueListitemVM{
No: i.Number.No,
Issue: i,
Day: t.Day(),
Month: int(t.Month()),
Year: t.Year(),
}, nil
}

View File

@@ -1,16 +0,0 @@
package viewmodels
import (
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
type PieceViewModel struct {
*xmlprovider.Piece
// TODO: this is a bit hacky, but it refences the page number of the piece in the issue
Von int
Bis int
}
func NewPieceView(p *xmlprovider.Piece) (PieceViewModel, error) {
return PieceViewModel{Piece: p}, nil
}

View File

@@ -1,98 +0,0 @@
package viewmodels
import (
"slices"
"strconv"
"strings"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/functions"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/helpers/logging"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
type SingleIssueViewModel struct {
IssueViewModel
No int
Year string
Pieces map[int][]PieceViewModel
Pages []int
AdditionalPieces map[int][]PieceViewModel
AdditionalPages []int
Next IssueViewModel
Prev IssueViewModel
}
func NewSingleIssueView(y string, No string, lib *xmlprovider.Library) (*SingleIssueViewModel, error) {
ivm, err := IssueView(y, No, lib)
if err != nil {
return nil, err
}
no, err := strconv.Atoi(No)
if err != nil {
return nil, err
}
sivm := SingleIssueViewModel{IssueViewModel: *ivm, No: no, Year: y}
if err := sivm.PiecesForIsssue(lib); err != nil {
return nil, err
}
slices.Sort(sivm.Pages)
slices.Sort(sivm.AdditionalPages)
return &sivm, nil
}
func (issue *SingleIssueViewModel) PiecesForIsssue(lib *xmlprovider.Library) error {
nostr := strconv.Itoa(issue.No)
lookfor := issue.Year + "-" + nostr + "-"
n := issue.No
y := issue.Year
adp := make(map[int][]PieceViewModel)
ip := make(map[int][]PieceViewModel)
lib.Pieces.Items.Range(func(key, value interface{}) bool {
k := key.(string)
if strings.HasPrefix(k, lookfor) {
a := value.(*xmlprovider.Piece)
p, err := NewPieceView(a)
if err != nil {
logging.ObjErr(&a, err)
return true
}
if strings.HasPrefix(k, lookfor+"b-") {
for _, i := range p.AdditionalRef {
// INFO: Here we find the page number the piece has in THIS issue, same below
if i.Datum == y && i.Nr == n {
p.Von = i.Von
p.Bis = i.Bis
}
}
functions.MapArrayInsert(adp, p.Von, p)
} else {
for _, i := range p.IssueRefs {
if i.Datum == y && i.Nr == n {
p.Von = i.Von
p.Bis = i.Bis
}
}
functions.MapArrayInsert(ip, p.Von, p)
}
}
return true
})
issue.Pieces = ip
issue.Pages = functions.Keys(ip)
issue.AdditionalPieces = adp
issue.AdditionalPages = functions.Keys(adp)
return nil
}

View File

@@ -1,100 +1,15 @@
package viewmodels
import (
"errors"
"fmt"
"maps"
"slices"
"sort"
"strconv"
"github.com/Theodor-Springmann-Stiftung/kgpz_web/providers/xmlprovider"
)
type IssuesByMonth map[int][]IssueViewModel
type YearViewModel struct {
Year string
AvailableYears []string
Issues IssuesByMonth
}
func YearView(year string, lib *xmlprovider.Library) (*YearViewModel, error) {
res := YearViewModel{Year: year}
res.Issues = make(IssuesByMonth, 12)
last := ""
lib.Issues.Items.Range(func(key, value interface{}) bool {
k := key.(string)
if len(k) < 4 {
return true
}
date := k[0:4]
if date != last {
res.PushAvailable(date)
last = date
}
if date == year {
issue := value.(*xmlprovider.Issue)
res.PushIssue(issue)
}
return true
})
if len(res.Issues) == 0 {
return nil, errors.New("No issues found for year " + year)
}
res.Sort()
return &res, nil
}
func (y *YearViewModel) Sort() {
y.SortAvailableYears()
y.Issues.Sort()
}
func (y *YearViewModel) PushIssue(i *xmlprovider.Issue) {
iv, err := FromIssue(i)
if err != nil {
return
}
list, ok := y.Issues[iv.Month]
if !ok {
list = []IssueViewModel{}
} else {
for _, issue := range list {
if issue.Number.No == iv.Number.No {
return
}
}
}
y.Issues[iv.Month] = append(list, *iv)
}
func (y *YearViewModel) PushAvailable(s string) {
if !slices.Contains(y.AvailableYears, s) {
y.AvailableYears = append(y.AvailableYears, s)
}
}
func (y *YearViewModel) SortAvailableYears() {
sort.Slice(y.AvailableYears, func(i, j int) bool {
iint, err := strconv.Atoi(y.AvailableYears[i])
if err != nil {
return true
}
jint, err := strconv.Atoi(y.AvailableYears[j])
if err != nil {
return false
}
return iint < jint
})
}
type IssuesByMonth map[int][]IssueListitemVM
func (ibm *IssuesByMonth) Sort() {
for _, issues := range *ibm {
@@ -103,3 +18,41 @@ func (ibm *IssuesByMonth) Sort() {
})
}
}
type YearVM struct {
Year int
AvailableYears []int
Issues IssuesByMonth
}
func YearView(year int, lib *xmlprovider.Library) (*YearVM, error) {
issues := make(IssuesByMonth, 12)
years := make(map[int]bool)
lib.Issues.Lock()
for _, issue := range lib.Issues.Array {
if y, err := issue.Year(); err == nil {
years[y] = true
if y == year {
if issuevm, err := ListitemFromIssue(issue); err == nil {
issues[issuevm.Month] = append(issues[issuevm.Month], *issuevm)
}
}
}
}
lib.Issues.Unlock()
if len(issues) == 0 {
return nil, fmt.Errorf("No issues found for year %v", year)
}
availableyears := slices.Collect(maps.Keys(years))
slices.Sort(availableyears)
issues.Sort()
return &YearVM{
Year: year,
AvailableYears: availableyears,
Issues: issues,
}, nil
}