mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2025-10-29 17:25:32 +00:00
Personen DB Abfragen + Seiten
This commit is contained in:
@@ -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 = (*Agent)(nil)
|
||||
@@ -115,10 +111,3 @@ func (a *Agent) SetEditState(editState string) {
|
||||
func (a *Agent) Comment() string {
|
||||
return a.GetString(COMMENT_FIELD)
|
||||
}
|
||||
|
||||
func SortAgentsByName(series []*Agent) {
|
||||
collator := collate.New(language.German)
|
||||
slices.SortFunc(series, func(i, j *Agent) int {
|
||||
return collator.CompareString(i.Name(), j.Name())
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,10 +1,17 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"slices"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
"golang.org/x/text/collate"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
type AgentsEntries map[string][]*REntriesAgents
|
||||
type AgentsContents map[string][]*RContentsAgents
|
||||
|
||||
func AgentForId(app core.App, id string) (*Agent, error) {
|
||||
agent := &Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
@@ -15,3 +22,193 @@ func AgentForId(app core.App, id string) (*Agent, error) {
|
||||
}
|
||||
return agent, nil
|
||||
}
|
||||
|
||||
func AgentsForEntries(app core.App, entries []*Entry) (map[string]*Agent, AgentsEntries, error) {
|
||||
eids := []any{}
|
||||
for _, e := range entries {
|
||||
eids = append(eids, e.Id)
|
||||
}
|
||||
|
||||
relations := []*REntriesAgents{}
|
||||
err := app.RecordQuery(RelationTableName(ENTRIES_TABLE, AGENTS_TABLE)).
|
||||
Where(dbx.HashExp{ENTRIES_TABLE: eids}).
|
||||
All(&relations)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
agentIds := []any{}
|
||||
for _, r := range relations {
|
||||
agentIds = append(agentIds, r.Agent())
|
||||
}
|
||||
|
||||
agents := []*Agent{}
|
||||
err = app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: agentIds}).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
agentsMap := make(map[string]*Agent, len(agents))
|
||||
for _, a := range agents {
|
||||
agentsMap[a.Id] = a
|
||||
}
|
||||
|
||||
relationMap := make(map[string][]*REntriesAgents, len(entries))
|
||||
for _, r := range relations {
|
||||
relationMap[r.Entry()] = append(relationMap[r.Entry()], r)
|
||||
}
|
||||
|
||||
return agentsMap, relationMap, nil
|
||||
}
|
||||
|
||||
func AgentsForContents(app core.App, contents []*Content) (map[string]*Agent, AgentsContents, error) {
|
||||
cids := []any{}
|
||||
for _, c := range contents {
|
||||
cids = append(cids, c.Id)
|
||||
}
|
||||
|
||||
relations := []*RContentsAgents{}
|
||||
err := app.RecordQuery(RelationTableName(CONTENTS_TABLE, AGENTS_TABLE)).
|
||||
Where(dbx.HashExp{CONTENTS_TABLE: cids}).
|
||||
All(&relations)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
agentIds := []any{}
|
||||
for _, r := range relations {
|
||||
agentIds = append(agentIds, r.Agent())
|
||||
}
|
||||
|
||||
agents := []*Agent{}
|
||||
err = app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: agentIds}).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
agentsMap := make(map[string]*Agent, len(agents))
|
||||
for _, a := range agents {
|
||||
agentsMap[a.Id] = a
|
||||
}
|
||||
|
||||
relationMap := make(map[string][]*RContentsAgents, len(contents))
|
||||
for _, r := range relations {
|
||||
relationMap[r.Content()] = append(relationMap[r.Content()], r)
|
||||
}
|
||||
|
||||
return agentsMap, relationMap, nil
|
||||
}
|
||||
|
||||
func LettersForAgents(app core.App) ([]string, error) {
|
||||
letters := []core.Record{}
|
||||
ids := []string{}
|
||||
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Select("upper(substr(" + AGENTS_NAME_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"))
|
||||
}
|
||||
|
||||
collator := collate.New(language.German, collate.Loose)
|
||||
collator.SortStrings(ids)
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func AgentsForLetter(app core.App, letter string) ([]*Agent, error) {
|
||||
agents := []*Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.Like(AGENTS_NAME_FIELD, letter).Match(false, true)).
|
||||
OrderBy(AGENTS_NAME_FIELD).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return agents, nil
|
||||
}
|
||||
|
||||
func SortAgentsByName(series []*Agent) {
|
||||
collator := collate.New(language.German, collate.Loose)
|
||||
slices.SortFunc(series, func(i, j *Agent) int {
|
||||
return collator.CompareString(i.Name(), j.Name())
|
||||
})
|
||||
}
|
||||
|
||||
func BasicSearchAgents(app core.App, query string) ([]*Agent, []*Agent, error) {
|
||||
agents, err := TitleSearchAgents(app, query)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
altagents, err := AltSearchAgents(app, query)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return agents, altagents, nil
|
||||
}
|
||||
|
||||
func TitleSearchAgents(app core.App, query string) ([]*Agent, error) {
|
||||
agents := []*Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.Like(AGENTS_NAME_FIELD, query).Match(true, true)).
|
||||
OrWhere(dbx.Like(AGENTS_PSEUDONYMS_FIELD, query).Match(true, true)).
|
||||
OrderBy(AGENTS_NAME_FIELD).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return agents, nil
|
||||
}
|
||||
|
||||
func AltSearchAgents(app core.App, query string) ([]*Agent, error) {
|
||||
agents := []*Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.Like(ANNOTATION_FIELD, query).Match(true, true)).
|
||||
OrderBy(AGENTS_NAME_FIELD).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return agents, nil
|
||||
}
|
||||
|
||||
func AgentsForProfession(app core.App, profession string, letter string) ([]*Agent, error) {
|
||||
agents := []*Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.Like(AGENTS_NAME_FIELD, letter).Match(false, true)).
|
||||
AndWhere(dbx.Like(AGENTS_PROFESSION_FIELD, profession).Match(true, true)).
|
||||
OrderBy(AGENTS_NAME_FIELD).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return agents, nil
|
||||
}
|
||||
|
||||
func AgentsForOrg(app core.App, org bool, letter string) ([]*Agent, error) {
|
||||
agents := []*Agent{}
|
||||
err := app.RecordQuery(AGENTS_TABLE).
|
||||
Where(dbx.Like(AGENTS_NAME_FIELD, letter).Match(false, true)).
|
||||
AndWhere(dbx.HashExp{AGENTS_CORP_FIELD: org}).
|
||||
OrderBy(AGENTS_NAME_FIELD).
|
||||
All(&agents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return agents, nil
|
||||
}
|
||||
|
||||
86
dbmodels/contents.go
Normal file
86
dbmodels/contents.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package dbmodels
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/core"
|
||||
)
|
||||
|
||||
type ContentsAgents map[string][]*RContentsAgents
|
||||
|
||||
func ContentsForEntry(app core.App, entry *Entry) ([]*Content, error) {
|
||||
contents := []*Content{}
|
||||
err := app.RecordQuery(CONTENTS_TABLE).
|
||||
Where(dbx.HashExp{ENTRIES_TABLE: entry.Id}).
|
||||
All(&contents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
slices.SortFunc(contents, func(i, j *Content) int {
|
||||
r := i.Numbering() - j.Numbering()
|
||||
if r == 0 {
|
||||
return 0
|
||||
}
|
||||
if r < 0 {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
})
|
||||
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
func ContentsForAgent(app core.App, agentId string) ([]*Content, error) {
|
||||
relations := []*RContentsAgents{}
|
||||
err := app.RecordQuery(RelationTableName(CONTENTS_TABLE, AGENTS_TABLE)).
|
||||
Where(dbx.HashExp{AGENTS_TABLE: agentId}).
|
||||
All(&relations)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cids := []any{}
|
||||
for _, r := range relations {
|
||||
cids = append(cids, r.Content())
|
||||
}
|
||||
|
||||
contents := []*Content{}
|
||||
err = app.RecordQuery(CONTENTS_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: cids}).
|
||||
All(&contents)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return contents, nil
|
||||
}
|
||||
|
||||
func SortContentsByEntryNumbering(contents []*Content, entries map[string]*Entry) {
|
||||
slices.SortFunc(contents, func(i, j *Content) int {
|
||||
ii, iok := entries[i.Entry()]
|
||||
ij, jok := entries[j.Entry()]
|
||||
if iok && jok {
|
||||
ret := ii.Year() - ij.Year()
|
||||
if ret != 0 {
|
||||
return ret
|
||||
}
|
||||
|
||||
ret = strings.Compare(ii.PreferredTitle(), ij.PreferredTitle())
|
||||
if ret != 0 {
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
r := i.Numbering() - j.Numbering()
|
||||
if r == 0 {
|
||||
return 0
|
||||
}
|
||||
if r < 0 {
|
||||
return -1
|
||||
}
|
||||
return 1
|
||||
})
|
||||
}
|
||||
@@ -88,3 +88,36 @@ func EntryForId(app core.App, id string) (*Entry, error) {
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func EntryForMusenalmID(app core.App, id string) (*Entry, error) {
|
||||
entry := &Entry{}
|
||||
err := app.RecordQuery(ENTRIES_TABLE).
|
||||
Where(dbx.HashExp{MUSENALMID_FIELD: id}).
|
||||
One(entry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
func EntriesForContents(app core.App, contents []*Content) (map[string]*Entry, error) {
|
||||
cids := []any{}
|
||||
for _, c := range contents {
|
||||
cids = append(cids, c.Entry())
|
||||
}
|
||||
|
||||
entries := []*Entry{}
|
||||
err := app.RecordQuery(ENTRIES_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: cids}).
|
||||
All(&entries)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
entriesMap := make(map[string]*Entry, len(entries))
|
||||
for _, e := range entries {
|
||||
entriesMap[e.Id] = e
|
||||
}
|
||||
|
||||
return entriesMap, nil
|
||||
}
|
||||
|
||||
@@ -38,3 +38,29 @@ func PlaceForId(app core.App, id string) (*Place, error) {
|
||||
}
|
||||
return place, nil
|
||||
}
|
||||
|
||||
func PlacesForEntry(app core.App, entry *Entry) (map[string]*Place, error) {
|
||||
ids := []any{}
|
||||
places := map[string]*Place{}
|
||||
|
||||
for _, r := range entry.Places() {
|
||||
ids = append(ids, r)
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return places, nil
|
||||
}
|
||||
|
||||
p := []*Place{}
|
||||
err := app.RecordQuery(PLACES_TABLE).
|
||||
Where(dbx.HashExp{ID_FIELD: ids}).
|
||||
All(&p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, place := range p {
|
||||
places[place.Id] = place
|
||||
}
|
||||
|
||||
return places, nil
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func IDsForSeriesses(series []*Series) []any {
|
||||
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)
|
||||
m[r.Series()] = append(m[r.Series()], r)
|
||||
}
|
||||
|
||||
for _, rel := range m {
|
||||
@@ -111,8 +111,8 @@ func EntriesForSeriesses(app core.App, series []*Series) (
|
||||
|
||||
smap := map[string][]*REntriesSeries{}
|
||||
for _, r := range relations {
|
||||
series := NewREntriesSeries(r)
|
||||
smap[series.Id] = append(smap[series.Id], series)
|
||||
rel := NewREntriesSeries(r)
|
||||
smap[rel.Series()] = append(smap[rel.Series()], rel)
|
||||
}
|
||||
|
||||
for _, rel := range smap {
|
||||
|
||||
Reference in New Issue
Block a user