feat(m1): backend scaffold - entries CRUD, start/stop, auth, migrations
This commit is contained in:
81
internal/store/settings_store.go
Normal file
81
internal/store/settings_store.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/wotra/wotra/internal/domain"
|
||||
)
|
||||
|
||||
// SettingsStore handles persistence for settings history.
|
||||
type SettingsStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewSettingsStore(db *sql.DB) *SettingsStore {
|
||||
return &SettingsStore{db: db}
|
||||
}
|
||||
|
||||
// Current returns the most recent settings effective on or before the given day key.
|
||||
func (s *SettingsStore) Current(ctx context.Context, asOfDayKey string) (*domain.Settings, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
FROM settings_history
|
||||
WHERE effective_from <= ?
|
||||
ORDER BY effective_from DESC, id DESC
|
||||
LIMIT 1`, asOfDayKey)
|
||||
return scanSettings(row)
|
||||
}
|
||||
|
||||
// Latest returns the most recently created settings row.
|
||||
func (s *SettingsStore) Latest(ctx context.Context) (*domain.Settings, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
FROM settings_history
|
||||
ORDER BY effective_from DESC, id DESC
|
||||
LIMIT 1`)
|
||||
return scanSettings(row)
|
||||
}
|
||||
|
||||
// History returns all settings rows ordered by effective_from DESC.
|
||||
func (s *SettingsStore) History(ctx context.Context) ([]*domain.Settings, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
|
||||
FROM settings_history ORDER BY effective_from DESC, id DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var result []*domain.Settings
|
||||
for rows.Next() {
|
||||
var s domain.Settings
|
||||
if err := rows.Scan(&s.ID, &s.EffectiveFrom, &s.HoursPerWeek, &s.WorkdaysMask, &s.Timezone, &s.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result = append(result, &s)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// Insert inserts a new settings row.
|
||||
func (s *SettingsStore) Insert(ctx context.Context, set *domain.Settings) error {
|
||||
res, err := s.db.ExecContext(ctx,
|
||||
`INSERT INTO settings_history (effective_from, hours_per_week, workdays_mask, timezone, created_at)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.CreatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, _ := res.LastInsertId()
|
||||
set.ID = id
|
||||
return nil
|
||||
}
|
||||
|
||||
func scanSettings(row *sql.Row) (*domain.Settings, error) {
|
||||
var s domain.Settings
|
||||
err := row.Scan(&s.ID, &s.EffectiveFrom, &s.HoursPerWeek, &s.WorkdaysMask, &s.Timezone, &s.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
Reference in New Issue
Block a user