mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 02:25:30 +00:00
BUGFIX: annoyances when editing a almanach
This commit is contained in:
@@ -12,6 +12,11 @@ const (
|
||||
maxPlacesSearchLimit = 50
|
||||
)
|
||||
|
||||
type PlaceCount struct {
|
||||
ID string `db:"id"`
|
||||
Count int `db:"count"`
|
||||
}
|
||||
|
||||
// SearchPlaces performs a lightweight search against the places table using the provided term.
|
||||
// It matches against the place name and pseudonyms fields.
|
||||
func SearchPlaces(app core.App, term string, limit int) ([]*Place, error) {
|
||||
@@ -41,3 +46,47 @@ func SearchPlaces(app core.App, term string, limit int) ([]*Place, error) {
|
||||
|
||||
return places, nil
|
||||
}
|
||||
|
||||
// CountPlacesBaende counts the number of Bände (entries) linked to each place.
|
||||
// Returns a map of place ID -> count.
|
||||
func CountPlacesBaende(app core.App, ids []any) (map[string]int, error) {
|
||||
if len(ids) == 0 {
|
||||
return map[string]int{}, nil
|
||||
}
|
||||
|
||||
counts := []PlaceCount{}
|
||||
|
||||
// For each place ID, count how many entries have this place in their places field
|
||||
for _, id := range ids {
|
||||
var count int64
|
||||
|
||||
// Query counts entries where the places field contains this place ID
|
||||
// PocketBase stores relation fields as JSON arrays, even for single values
|
||||
err := app.DB().
|
||||
Select("COUNT(*)").
|
||||
From(ENTRIES_TABLE).
|
||||
Where(dbx.NewExp(
|
||||
"json_valid("+PLACES_TABLE+") = 1 AND EXISTS (SELECT 1 FROM json_each("+PLACES_TABLE+") WHERE value = {:id})",
|
||||
dbx.Params{"id": id},
|
||||
)).
|
||||
Row(&count)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
counts = append(counts, PlaceCount{
|
||||
ID: id.(string),
|
||||
Count: int(count),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
ret := make(map[string]int, len(counts))
|
||||
for _, c := range counts {
|
||||
ret[c.ID] = c.Count
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user