mirror of
https://github.com/IS1DI/x0gp.git
synced 2026-07-16 20:47:55 +00:00
This commit captures the full server code accumulated across
several development sessions. It includes the following layers,
applied in roughly this order during development:
1. Base infrastructure
- cmd/poc-server HTTP+WebSocket server, /health, /stats
- internal/transport wire types (Envelope + all message types)
- internal/catalog, internal/realtime, internal/stats
- internal/storage/postgres with migrations 001-004
- .env, .env.example, docker-compose, Dockerfile, scripts/
2. Swagger documentation
- go get github.com/swaggo/http-swagger
- swag init produces docs/docs.go
- /swagger/index.html UI, /swagger/doc.json spec
- annotations on health/stats/ws and catalog handlers
3. Races API
- internal/races/{store,service,keyset,types}.go
- migration 005_races.sql: finished_races, race_plans, race_queue
- GET /api/races with keyset pagination, status filter
- GET /api/races/upcoming
- POST /api/races/queue/join, GET, DELETE
- POST/GET /api/races/plans, DELETE /{id}
- lobby.RaceMeta simplification (no host_id/host_name)
4. Races seeder
- internal/races/seed with deterministic generator
- --seed-races / --reset CLI flags in main.go
- 30 finished + 5 live + 5 plans + 4 queue entries
5. Drivers and clans
- internal/drivers, internal/clans (CRUD, validation)
- migration 008_drivers_clans.sql
- /api/clans and /api/drivers endpoints
- 3-letter uppercase nickname/tag validation
- lobby.DriverMeta: Nickname, AvatarURL, ClanID, ClanTag
6. Podium
- internal/transport.RacePodiumEntry
- lobby.SetDriverProfile for in-memory metadata sync
- migration 007_podium.sql (podium JSONB column)
- seeder populates top-3 per finished race
7. Live races persistence
- migration 009_live_persistence.sql: live_races,
live_race_drivers, lobby_drivers
- internal/races/live_store.go: LiveStore with write-side
mirror for lobby.Service mutations
- Service.collectLive and Upcoming read from Postgres
- main.go RestoreFromDB rehydrates lobby on startup
8. Unify live + finished into one races table
- migration 010_unify_races.sql: rename finished_races to
races, expand status CHECK, merge live rows
- PgStore now hosts both write paths; LiveStore is a
thin facade implementing lobby.Persistence
- seeder resetAll drops only finished/cancelled rows and
race_plans / race_queue / lobby_drivers
Each layer is consistent on its own; cross-layer changes are
visible in the file history. A future refactor may split this
commit into the per-stage boundaries listed above.
105 lines
2.4 KiB
Go
105 lines
2.4 KiB
Go
package clans
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
)
|
|
|
|
// Service composes validation on top of PgStore.
|
|
type Service struct {
|
|
pg *PgStore
|
|
now func() time.Time
|
|
}
|
|
|
|
// NewService wires a service.
|
|
func NewService(pg *PgStore) *Service {
|
|
return &Service{pg: pg, now: time.Now}
|
|
}
|
|
|
|
// CreateInput is what the HTTP handler hands to the service.
|
|
type CreateInput struct {
|
|
ID string
|
|
Tag string
|
|
Name string
|
|
AvatarURL string
|
|
}
|
|
|
|
// Create validates and inserts a clan.
|
|
func (s *Service) Create(ctx context.Context, in CreateInput) (Clan, error) {
|
|
tag := strings.ToUpper(strings.TrimSpace(in.Tag))
|
|
if !IsValidTag(tag) {
|
|
return Clan{}, fmt.Errorf("%w: tag must be 3 uppercase ASCII letters", ErrInvalidInput)
|
|
}
|
|
name := strings.TrimSpace(in.Name)
|
|
if name == "" {
|
|
return Clan{}, fmt.Errorf("%w: name required", ErrInvalidInput)
|
|
}
|
|
c := Clan{
|
|
ID: in.ID,
|
|
Tag: tag,
|
|
Name: name,
|
|
AvatarURL: in.AvatarURL,
|
|
}
|
|
if c.ID == "" {
|
|
c.ID = fmt.Sprintf("clan-%d-%d", s.now().UnixMilli(), time.Now().UnixNano()%1000)
|
|
}
|
|
if err := s.pg.Create(ctx, c); err != nil {
|
|
var pgErr *pgconn.PgError
|
|
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
|
return Clan{}, fmt.Errorf("%w: %s", ErrAlreadyExists, tag)
|
|
}
|
|
return Clan{}, err
|
|
}
|
|
return s.pg.Get(ctx, c.ID)
|
|
}
|
|
|
|
// Get loads a clan by id.
|
|
func (s *Service) Get(ctx context.Context, id string) (Clan, error) {
|
|
return s.pg.Get(ctx, id)
|
|
}
|
|
|
|
// GetByTag loads a clan by tag.
|
|
func (s *Service) GetByTag(ctx context.Context, tag string) (Clan, error) {
|
|
tag = strings.ToUpper(strings.TrimSpace(tag))
|
|
return s.pg.GetByTag(ctx, tag)
|
|
}
|
|
|
|
// List returns clans with limit/offset.
|
|
func (s *Service) List(ctx context.Context, limit, offset int) ([]Clan, error) {
|
|
return s.pg.List(ctx, limit, offset)
|
|
}
|
|
|
|
// UpdateInput is the patch payload.
|
|
type UpdateInput struct {
|
|
Name string
|
|
AvatarURL string
|
|
}
|
|
|
|
// Update applies a partial patch.
|
|
func (s *Service) Update(ctx context.Context, id string, in UpdateInput) (Clan, error) {
|
|
cur, err := s.pg.Get(ctx, id)
|
|
if err != nil {
|
|
return Clan{}, err
|
|
}
|
|
if in.Name != "" {
|
|
cur.Name = in.Name
|
|
}
|
|
if in.AvatarURL != "" {
|
|
cur.AvatarURL = in.AvatarURL
|
|
}
|
|
if err := s.pg.Update(ctx, cur); err != nil {
|
|
return Clan{}, err
|
|
}
|
|
return s.pg.Get(ctx, id)
|
|
}
|
|
|
|
// Delete removes a clan.
|
|
func (s *Service) Delete(ctx context.Context, id string) error {
|
|
return s.pg.Delete(ctx, id)
|
|
}
|