mirror of
https://github.com/Theodor-Springmann-Stiftung/kgpz_web.git
synced 2025-12-17 04:15:30 +00:00
added XSDTime datatype
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user