mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 09:15:33 +00:00
Alle Abfragen für Reihen-Seite
This commit is contained in:
17
dbmodels/agents.go
Normal file
17
dbmodels/agents.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
func AgentForId(app core.App, id string) (*Agent, error) {
|
||||
agent := &Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: id}).
|
||||
One(agent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return agent, nil
|
||||
}
|
||||
@@ -423,6 +423,7 @@ const (
|
||||
CONTENTS_TABLE = "contents"
|
||||
ITEMS_TABLE = "items"
|
||||
|
||||
ID_FIELD = "id"
|
||||
ANNOTATION_FIELD = "annotation"
|
||||
|
||||
MUSENALMID_FIELD = "musenalm_id"
|
||||
|
||||
90
dbmodels/entries.go
Normal file
90
dbmodels/entries.go
Normal file
@@ -0,0 +1,90 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
type EntriesAgents map[string][]*REntriesAgents
|
||||
|
||||
func YearsForEntries(app core.App) ([]int, error) {
|
||||
rec := []core.Record{}
|
||||
err := app.RecordQuery(ENTRIES_TABLE).
|
||||
Select(YEAR_FIELD + " AS id").
|
||||
Distinct(true).
|
||||
OrderBy("id").
|
||||
All(&rec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
years := []int{}
|
||||
for _, r := range rec {
|
||||
years = append(years, r.GetInt("id"))
|
||||
}
|
||||
|
||||
return years, nil
|
||||
}
|
||||
|
||||
func EntriesForYear(app core.App, year int) ([]*Entry, error) {
|
||||
entries := []*Entry{}
|
||||
err := app.RecordQuery(ENTRIES_TABLE).
|
||||
Where(dbx.HashExp{YEAR_FIELD: year}).
|
||||
All(&entries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func EntriesForAgent(app core.App, agentId string) ([]*Entry, EntriesAgents, error) {
|
||||
relations := []*core.Record{}
|
||||
err := app.RecordQuery(RelationTableName(ENTRIES_TABLE, AGENTS_TABLE)).
|
||||
Where(dbx.HashExp{AGENTS_TABLE: agentId}).
|
||||
All(&relations)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
app.ExpandRecords(relations, []string{ENTRIES_TABLE}, nil)
|
||||
entries := []*Entry{}
|
||||
for _, r := range relations {
|
||||
record := r.ExpandedOne(ENTRIES_TABLE)
|
||||
if record == nil {
|
||||
continue
|
||||
}
|
||||
entries = append(entries, NewEntry(record))
|
||||
}
|
||||
|
||||
agents := map[string][]*REntriesAgents{}
|
||||
for _, r := range relations {
|
||||
agent := NewREntriesAgents(r)
|
||||
agents[agent.Entry()] = append(agents[agent.Entry()], agent)
|
||||
}
|
||||
|
||||
return entries, agents, nil
|
||||
}
|
||||
|
||||
func EntriesForPlace(app core.App, placeId string) ([]*Entry, error) {
|
||||
entries := []*Entry{}
|
||||
err := app.RecordQuery(ENTRIES_TABLE).
|
||||
Where(dbx.Like(PLACES_TABLE, placeId).Match(true, true)).
|
||||
All(&entries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
func EntryForId(app core.App, id string) (*Entry, error) {
|
||||
entry := &Entry{}
|
||||
err := app.RecordQuery(ENTRIES_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: id}).
|
||||
One(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
@@ -82,14 +82,18 @@ func BasicRelationCollection(app core.App, sourcetablename, targettablename stri
|
||||
return collection, nil
|
||||
}
|
||||
|
||||
type IDable interface {
|
||||
ID() string
|
||||
}
|
||||
|
||||
func GetIDs(records []IDable) []string {
|
||||
ids := []string{}
|
||||
func GetIDs(records []*core.Record) []any {
|
||||
ids := []any{}
|
||||
for _, r := range records {
|
||||
ids = append(ids, r.ID())
|
||||
ids = append(ids, r.Id)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func GetFields(records []*core.Record, field string) []any {
|
||||
fields := []any{}
|
||||
for _, r := range records {
|
||||
fields = append(fields, r.GetString(field))
|
||||
}
|
||||
return fields
|
||||
}
|
||||
|
||||
40
dbmodels/places.go
Normal file
40
dbmodels/places.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"golang.org/x/text/collate"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func AllPlaces(app core.App) ([]*Place, error) {
|
||||
places := []*Place{}
|
||||
err := app.RecordQuery(PLACES_TABLE).
|
||||
OrderBy(PLACES_NAME_FIELD).
|
||||
All(&places)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return places, nil
|
||||
}
|
||||
|
||||
func SortPlacesByName(places []*Place) {
|
||||
collator := collate.New(language.German)
|
||||
slices.SortFunc(places, func(i, j *Place) int {
|
||||
return collator.CompareString(i.Name(), j.Name())
|
||||
})
|
||||
}
|
||||
|
||||
func PlaceForId(app core.App, id string) (*Place, error) {
|
||||
place := &Place{}
|
||||
err := app.RecordQuery(PLACES_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: id}).
|
||||
One(place)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return place, nil
|
||||
}
|
||||
@@ -1,11 +1,7 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"golang.org/x/text/collate"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
var _ core.RecordProxy = (*Series)(nil)
|
||||
@@ -91,10 +87,3 @@ func (s *Series) Frequency() string {
|
||||
func (s *Series) SetFrequency(frequency string) {
|
||||
s.Set(SERIES_FREQUENCY_FIELD, frequency)
|
||||
}
|
||||
|
||||
func SortSeriesByTitle(series []*Series) {
|
||||
collator := collate.New(language.German)
|
||||
slices.SortFunc(series, func(i, j *Series) int {
|
||||
return collator.CompareString(i.Title(), j.Title())
|
||||
})
|
||||
}
|
||||
|
||||
248
dbmodels/seriesses.go
Normal file
248
dbmodels/seriesses.go
Normal file
@@ -0,0 +1,248 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"golang.org/x/text/collate"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type SeriesEntries map[string][]*REntriesSeries
|
||||
|
||||
func SortSeriessesByTitle(series []*Series) {
|
||||
collator := collate.New(language.German)
|
||||
slices.SortFunc(series, func(i, j *Series) int {
|
||||
return collator.CompareString(i.Title(), j.Title())
|
||||
})
|
||||
}
|
||||
|
||||
func BasicSearchSeries(app core.App, query string) ([]*Series, []*Series, error) {
|
||||
series, err := TitleSearchSeries(app, query)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
altseries, err := AltSearchSeries(app, query)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return series, altseries, nil
|
||||
}
|
||||
|
||||
func TitleSearchSeries(app core.App, query string) ([]*Series, error) {
|
||||
series := []*Series{}
|
||||
err := app.RecordQuery(SERIES_TABLE).
|
||||
Where(dbx.Like(SERIES_TITLE_FIELD, query).Match(true, true)).
|
||||
OrderBy(SERIES_TITLE_FIELD).
|
||||
All(&series)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return series, nil
|
||||
}
|
||||
|
||||
func AltSearchSeries(app core.App, query string) ([]*Series, error) {
|
||||
series := []*Series{}
|
||||
err := app.RecordQuery(SERIES_TABLE).
|
||||
Where(dbx.Like(ANNOTATION_FIELD, query).Match(true, true)).
|
||||
OrderBy(SERIES_TITLE_FIELD).
|
||||
All(&series)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return series, nil
|
||||
}
|
||||
|
||||
func IDsForSeriesses(series []*Series) []any {
|
||||
ids := []any{}
|
||||
for _, s := range series {
|
||||
ids = append(ids, s.Id)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func makeMapForEnrySeries(relations []*REntriesSeries, entries map[string]*Entry) SeriesEntries {
|
||||
m := map[string][]*REntriesSeries{}
|
||||
for _, r := range relations {
|
||||
m[r.Id] = append(m[r.Id], r)
|
||||
}
|
||||
|
||||
for _, rel := range m {
|
||||
slices.SortFunc(rel, func(i, j *REntriesSeries) int {
|
||||
ientry := entries[i.Entry()]
|
||||
jentry := entries[j.Entry()]
|
||||
return ientry.Year() - jentry.Year()
|
||||
})
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func EntriesForSeriesses(app core.App, series []*Series) (
|
||||
SeriesEntries,
|
||||
map[string]*Entry,
|
||||
error) {
|
||||
ids := IDsForSeriesses(series)
|
||||
relations := []*core.Record{}
|
||||
|
||||
err := app.RecordQuery(RelationTableName(ENTRIES_TABLE, SERIES_TABLE)).
|
||||
Where(dbx.HashExp{
|
||||
SERIES_TABLE: ids,
|
||||
}).
|
||||
All(&relations)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
app.ExpandRecords(relations, []string{ENTRIES_TABLE}, nil)
|
||||
bmap := map[string]*Entry{}
|
||||
for _, r := range relations {
|
||||
record := r.ExpandedOne(ENTRIES_TABLE)
|
||||
if record == nil {
|
||||
continue
|
||||
}
|
||||
entry := NewEntry(record)
|
||||
bmap[entry.Id] = entry
|
||||
}
|
||||
|
||||
smap := map[string][]*REntriesSeries{}
|
||||
for _, r := range relations {
|
||||
series := NewREntriesSeries(r)
|
||||
smap[series.Id] = append(smap[series.Id], series)
|
||||
}
|
||||
|
||||
for _, rel := range smap {
|
||||
slices.SortFunc(rel, func(i, j *REntriesSeries) int {
|
||||
ientry := bmap[i.Entry()]
|
||||
jentry := bmap[j.Entry()]
|
||||
return ientry.Year() - jentry.Year()
|
||||
})
|
||||
}
|
||||
|
||||
return smap, bmap, nil
|
||||
}
|
||||
|
||||
func LettersForSeries(app core.App) ([]string, error) {
|
||||
letters := []core.Record{}
|
||||
ids := []string{}
|
||||
|
||||
err := app.RecordQuery(SERIES_TABLE).
|
||||
Select("upper(substr(" + SERIES_TITLE_FIELD + ", 1, 1)) AS id").
|
||||
Distinct(true).
|
||||
All(&letters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, l := range letters {
|
||||
ids = append(ids, l.GetString("id"))
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func AllAgentsForSeries(app core.App) ([]*Agent, error) {
|
||||
rels := []*core.Record{}
|
||||
// INFO: we could just fetch all relations here
|
||||
err := app.RecordQuery(RelationTableName(ENTRIES_TABLE, AGENTS_TABLE)).
|
||||
GroupBy(AGENTS_TABLE).
|
||||
All(&rels)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
app.ExpandRecords(rels, []string{AGENTS_TABLE}, nil)
|
||||
agents := []*Agent{}
|
||||
for _, r := range rels {
|
||||
record := r.ExpandedOne(AGENTS_TABLE)
|
||||
if record == nil {
|
||||
continue
|
||||
}
|
||||
agent := NewAgent(record)
|
||||
agents = append(agents, agent)
|
||||
}
|
||||
|
||||
SortAgentsByName(agents)
|
||||
|
||||
return agents, err
|
||||
}
|
||||
|
||||
func SeriesForLetter(app core.App, letter string) ([]*Series, error) {
|
||||
series := []*Series{}
|
||||
err := app.RecordQuery(SERIES_TABLE).
|
||||
Where(dbx.Like(SERIES_TITLE_FIELD, letter).Match(false, true)).
|
||||
OrderBy(SERIES_TITLE_FIELD).
|
||||
All(&series)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return series, nil
|
||||
}
|
||||
|
||||
func SeriesForAgent(app core.App, id string) ([]*Series, SeriesEntries, map[string]*Entry, error) {
|
||||
entries, _, err := EntriesForAgent(app, id)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
return SeriesForEntries(app, entries)
|
||||
}
|
||||
|
||||
func SeriesForPlace(app core.App, id string) ([]*Series, SeriesEntries, map[string]*Entry, error) {
|
||||
entries, err := EntriesForPlace(app, id)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
return SeriesForEntries(app, entries)
|
||||
}
|
||||
|
||||
func SeriesForEntries(app core.App, entries []*Entry) ([]*Series, SeriesEntries, map[string]*Entry, error) {
|
||||
bids := make([]any, 0, len(entries))
|
||||
for _, e := range entries {
|
||||
bids = append(bids, e.Id)
|
||||
}
|
||||
|
||||
srels := []*REntriesSeries{}
|
||||
err := app.RecordQuery(RelationTableName(ENTRIES_TABLE, SERIES_TABLE)).
|
||||
Where(dbx.HashExp{ENTRIES_TABLE: bids}).
|
||||
All(&srels)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
sids := []any{}
|
||||
for _, s := range srels {
|
||||
sids = append(sids, s.Series())
|
||||
}
|
||||
|
||||
series := []*Series{}
|
||||
err = app.RecordQuery(SERIES_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: sids}).
|
||||
All(&series)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
bmap := make(map[string]*Entry, len(entries))
|
||||
for _, e := range entries {
|
||||
bmap[e.Id] = e
|
||||
}
|
||||
|
||||
smap := makeMapForEnrySeries(srels, bmap)
|
||||
|
||||
return series, smap, bmap, nil
|
||||
}
|
||||
|
||||
func SeriesForYear(app core.App, year int) ([]*Series, SeriesEntries, map[string]*Entry, error) {
|
||||
series, err := EntriesForYear(app, year)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
return SeriesForEntries(app, series)
|
||||
}
|
||||
Reference in New Issue
Block a user