Files
2026-11-12 20:00:33 +01:00

150 lines
4.7 KiB
Go

package store
import (
"context"
"database/sql"
"errors"
"time"
"lektorenplan/domain"
)
// LiturgicalDays returns persisted calendar days inside a range.
func (s *Store) LiturgicalDays(ctx context.Context, r domain.DayRange) ([]domain.LiturgicalDay, error) {
rows, err := s.db.QueryContext(ctx, `
SELECT date, title, rank, color, remark, first_reading, psalm, second_reading,
gospel, is_obligation, source, updated_at
FROM liturgical_days WHERE date >= ? AND date <= ? ORDER BY date`,
domain.Key(r.Start), domain.Key(r.End))
if err != nil {
return nil, err
}
defer rows.Close()
var out []domain.LiturgicalDay
for rows.Next() {
day, err := scanLiturgicalDay(rows)
if err != nil {
return nil, err
}
out = append(out, *day)
}
return out, rows.Err()
}
// LiturgicalDay loads one calendar day.
func (s *Store) LiturgicalDay(ctx context.Context, date time.Time) (*domain.LiturgicalDay, error) {
row := s.db.QueryRowContext(ctx, `
SELECT date, title, rank, color, remark, first_reading, psalm, second_reading,
gospel, is_obligation, source, updated_at
FROM liturgical_days WHERE date=?`, domain.Key(date))
day, err := scanLiturgicalDay(row)
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
return day, err
}
func scanLiturgicalDay(row rowScanner) (*domain.LiturgicalDay, error) {
var day domain.LiturgicalDay
var date, rank, color, updated string
var obligation int
if err := row.Scan(&date, &day.Title, &rank, &color, &day.Remark,
&day.Readings.First, &day.Readings.Psalm, &day.Readings.Second, &day.Readings.Gospel,
&obligation, &day.Source, &updated); err != nil {
return nil, err
}
day.Date = domain.MustDate(date)
day.Rank = domain.LiturgicalRank(rank)
day.Color = domain.LiturgicalColor(color)
day.IsObligation = obligation != 0
day.UpdatedAt = parseTime(updated)
return &day, nil
}
// LiturgicalDayCount returns the number of stored days.
func (s *Store) LiturgicalDayCount(ctx context.Context) (int, error) {
var count int
err := s.db.QueryRowContext(ctx, "SELECT COUNT(*) FROM liturgical_days").Scan(&count)
return count, err
}
// LiturgicalYears returns the years that have stored calendar data.
func (s *Store) LiturgicalYears(ctx context.Context) ([]int, error) {
rows, err := s.db.QueryContext(ctx,
"SELECT DISTINCT substr(date,1,4) FROM liturgical_days ORDER BY 1")
if err != nil {
return nil, err
}
defer rows.Close()
var out []int
for rows.Next() {
var value string
if err := rows.Scan(&value); err != nil {
return nil, err
}
year := 0
for _, r := range value {
if r < '0' || r > '9' {
break
}
year = year*10 + int(r-'0')
}
if year > 0 {
out = append(out, year)
}
}
return out, rows.Err()
}
// UpsertLiturgicalDays stores a batch of calendar days.
func (s *Store) UpsertLiturgicalDays(ctx context.Context, days []domain.LiturgicalDay) error {
return s.transaction(ctx, func(tx *sql.Tx) error {
for _, day := range days {
if err := upsertLiturgicalDayTx(ctx, tx, day); err != nil {
return err
}
}
return nil
})
}
// ReplaceLiturgicalYear replaces all stored days of one year.
func (s *Store) ReplaceLiturgicalYear(ctx context.Context, year int, days []domain.LiturgicalDay) error {
return s.transaction(ctx, func(tx *sql.Tx) error {
if _, err := tx.ExecContext(ctx,
"DELETE FROM liturgical_days WHERE date LIKE ?", time.Date(year, 1, 1, 0, 0, 0, 0, time.UTC).Format("2006")+"-%"); err != nil {
return err
}
for _, day := range days {
if err := upsertLiturgicalDayTx(ctx, tx, day); err != nil {
return err
}
}
return nil
})
}
func upsertLiturgicalDayTx(ctx context.Context, tx *sql.Tx, day domain.LiturgicalDay) error {
updated := day.UpdatedAt
if updated.IsZero() {
updated = time.Now().UTC()
}
_, err := tx.ExecContext(ctx, `
INSERT INTO liturgical_days(date, title, rank, color, remark, first_reading, psalm,
second_reading, gospel, is_obligation, source, updated_at)
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(date) DO UPDATE SET title=excluded.title, rank=excluded.rank,
color=excluded.color, remark=excluded.remark, first_reading=excluded.first_reading,
psalm=excluded.psalm, second_reading=excluded.second_reading, gospel=excluded.gospel,
is_obligation=excluded.is_obligation, source=excluded.source, updated_at=excluded.updated_at`,
domain.Key(day.Date), day.Title, string(day.Rank), string(day.Color), day.Remark,
day.Readings.First, day.Readings.Psalm, day.Readings.Second, day.Readings.Gospel,
boolInt(day.IsObligation), day.Source, updated.Format(time.RFC3339))
return err
}
// SetLiturgicalDay stores one day.
func (s *Store) SetLiturgicalDay(ctx context.Context, day domain.LiturgicalDay) error {
return s.UpsertLiturgicalDays(ctx, []domain.LiturgicalDay{day})
}