Files
wotra/internal/store/settings_store.go
Andreas Schneider 15bf3c3a18 feat: edit and delete settings history rows
Backend:
- SettingsStore: Add GetByID, Update, Delete, Count methods
- SettingsService: Add UpdateSettings (validates same rules as Upsert),
  DeleteSettings (guards against deleting the last row → 409)
- New sentinels: ErrSettingsNotFound, ErrLastSettingsRow
- Handler: PUT /api/settings/history/{id} → 200 updated row
           DELETE /api/settings/history/{id} → 204 / 404 / 409

Frontend:
- API client: settings.update(id, body) and settings.delete(id)
- Settings page: history table gains edit (pencil) and delete (×) buttons
- Inline edit form expands in place within the table row
- Delete button disabled and hint shown when only one row remains
- maskLabel() helper shows workday names instead of raw bitmask
- After save/delete: full reload to reflect changes in 'current' section
2026-04-30 19:50:27 +02:00

113 lines
3.5 KiB
Go

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
}
// Update overwrites an existing settings row by ID.
func (s *SettingsStore) Update(ctx context.Context, set *domain.Settings) error {
_, err := s.db.ExecContext(ctx,
`UPDATE settings_history
SET effective_from=?, hours_per_week=?, workdays_mask=?, timezone=?
WHERE id=?`,
set.EffectiveFrom, set.HoursPerWeek, set.WorkdaysMask, set.Timezone, set.ID)
return err
}
// Delete removes a settings row by ID.
func (s *SettingsStore) Delete(ctx context.Context, id int64) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM settings_history WHERE id=?`, id)
return err
}
// Count returns the total number of settings rows.
func (s *SettingsStore) Count(ctx context.Context) (int, error) {
var n int
err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM settings_history`).Scan(&n)
return n, err
}
// GetByID returns a single settings row by ID.
func (s *SettingsStore) GetByID(ctx context.Context, id int64) (*domain.Settings, error) {
row := s.db.QueryRowContext(ctx,
`SELECT id, effective_from, hours_per_week, workdays_mask, timezone, created_at
FROM settings_history WHERE id=?`, id)
return scanSettings(row)
}
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
}