mirror of
https://github.com/IS1DI/x0gp.git
synced 2026-07-16 20:47:55 +00:00
Add leaderboard for all tracks and track calendar
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
-- 014_track_calendar.sql — schema for F1-style active track scheduling.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS track_calendar (
|
||||
id SERIAL PRIMARY KEY,
|
||||
track_id TEXT NOT NULL REFERENCES tracks(id) ON DELETE CASCADE,
|
||||
start_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
end_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
CHECK (start_at < end_at)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS track_calendar_dates_idx ON track_calendar (start_at, end_at);
|
||||
@@ -370,4 +370,52 @@ func (s *PgStore) UpdateCarStats(ctx context.Context, id string, fn func(*catalo
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// GetTrackCalendar returns all track calendar entries ordered by start_at.
|
||||
func (s *PgStore) GetTrackCalendar(ctx context.Context) ([]catalog.TrackCalendarEntry, error) {
|
||||
query := `SELECT id, track_id, start_at, end_at FROM track_calendar ORDER BY start_at ASC`
|
||||
rows, err := s.pool.Query(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var entries []catalog.TrackCalendarEntry
|
||||
for rows.Next() {
|
||||
var entry catalog.TrackCalendarEntry
|
||||
if err := rows.Scan(&entry.ID, &entry.TrackID, &entry.StartAt, &entry.EndAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
return entries, nil
|
||||
}
|
||||
|
||||
// CreateTrackCalendar inserts a new calendar entry.
|
||||
func (s *PgStore) CreateTrackCalendar(ctx context.Context, entry catalog.TrackCalendarEntry) (catalog.TrackCalendarEntry, error) {
|
||||
query := `
|
||||
INSERT INTO track_calendar (track_id, start_at, end_at)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, track_id, start_at, end_at`
|
||||
|
||||
row := s.pool.QueryRow(ctx, query, entry.TrackID, entry.StartAt, entry.EndAt)
|
||||
var res catalog.TrackCalendarEntry
|
||||
if err := row.Scan(&res.ID, &res.TrackID, &res.StartAt, &res.EndAt); err != nil {
|
||||
return catalog.TrackCalendarEntry{}, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// DeleteTrackCalendar deletes a calendar entry by ID.
|
||||
func (s *PgStore) DeleteTrackCalendar(ctx context.Context, id int) error {
|
||||
command := `DELETE FROM track_calendar WHERE id = $1`
|
||||
res, err := s.pool.Exec(ctx, command, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.RowsAffected() == 0 {
|
||||
return catalog.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user