mirror of
https://github.com/IS1DI/x0gp.git
synced 2026-07-19 05:47:02 +00:00
Add races results GET endpoints (by ID and last finished) and regenerate swagger specs
This commit is contained in:
@@ -696,6 +696,61 @@ func (s *PgStore) hydrateFinished(ctx context.Context, r *FinishedRace) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetFinished fetches a single finished race by id.
|
||||
func (s *PgStore) GetFinished(ctx context.Context, id string) (FinishedRace, error) {
|
||||
row := s.pool.QueryRow(ctx, `
|
||||
SELECT id, name, track_id, max_cars, laps, time_limit_s,
|
||||
status, created_ms, started_ms, finished_ms, duration_ms,
|
||||
total_laps, total_drivers
|
||||
FROM races
|
||||
WHERE id = $1 AND status IN ('finished', 'cancelled')`, id)
|
||||
var r FinishedRace
|
||||
if err := row.Scan(
|
||||
&r.ID, &r.Name, &r.TrackID,
|
||||
&r.MaxCars, &r.Laps, &r.TimeLimitS, &r.Status,
|
||||
&r.CreatedMs, &r.StartedMs, &r.FinishedMs, &r.DurationMs,
|
||||
&r.TotalLaps, &r.TotalDrivers,
|
||||
); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return FinishedRace{}, ErrNotFound
|
||||
}
|
||||
return FinishedRace{}, err
|
||||
}
|
||||
if err := s.hydrateFinished(ctx, &r); err != nil {
|
||||
return FinishedRace{}, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// GetLastFinished fetches the most recently finished race.
|
||||
func (s *PgStore) GetLastFinished(ctx context.Context) (FinishedRace, error) {
|
||||
row := s.pool.QueryRow(ctx, `
|
||||
SELECT id, name, track_id, max_cars, laps, time_limit_s,
|
||||
status, created_ms, started_ms, finished_ms, duration_ms,
|
||||
total_laps, total_drivers
|
||||
FROM races
|
||||
WHERE status IN ('finished', 'cancelled')
|
||||
ORDER BY finished_ms DESC, id DESC
|
||||
LIMIT 1`)
|
||||
var r FinishedRace
|
||||
if err := row.Scan(
|
||||
&r.ID, &r.Name, &r.TrackID,
|
||||
&r.MaxCars, &r.Laps, &r.TimeLimitS, &r.Status,
|
||||
&r.CreatedMs, &r.StartedMs, &r.FinishedMs, &r.DurationMs,
|
||||
&r.TotalLaps, &r.TotalDrivers,
|
||||
); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return FinishedRace{}, ErrNotFound
|
||||
}
|
||||
return FinishedRace{}, err
|
||||
}
|
||||
if err := s.hydrateFinished(ctx, &r); err != nil {
|
||||
return FinishedRace{}, err
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
||||
func joinAnd(parts []string) string {
|
||||
out := ""
|
||||
for i, p := range parts {
|
||||
|
||||
Reference in New Issue
Block a user