diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-08-11 20:39:48 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-08-11 20:39:48 +0900 |
| commit | 125e26b4d5c986f8531c098ecd5d291c1e1c7a76 (patch) | |
| tree | fee5d641a05dc972a6b80fb3620d156f13d9b137 /backend | |
| parent | e3502d9e649fe61bb0ba4046b2c23c0d78bc92e9 (diff) | |
| download | iosdc-japan-2024-albatross-125e26b4d5c986f8531c098ecd5d291c1e1c7a76.tar.gz iosdc-japan-2024-albatross-125e26b4d5c986f8531c098ecd5d291c1e1c7a76.tar.zst iosdc-japan-2024-albatross-125e26b4d5c986f8531c098ecd5d291c1e1c7a76.zip | |
feat(backend): make `games.problem_id` non-null
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/admin/handler.go | 21 | ||||
| -rw-r--r-- | backend/admin/templates/game_edit.html | 2 | ||||
| -rw-r--r-- | backend/api/handler.go | 29 | ||||
| -rw-r--r-- | backend/db/models.go | 2 | ||||
| -rw-r--r-- | backend/db/query.sql.go | 32 | ||||
| -rw-r--r-- | backend/game/hub.go | 14 | ||||
| -rw-r--r-- | backend/query.sql | 6 | ||||
| -rw-r--r-- | backend/schema.sql | 2 |
8 files changed, 41 insertions, 67 deletions
diff --git a/backend/admin/handler.go b/backend/admin/handler.go index 7239b08..d540f57 100644 --- a/backend/admin/handler.go +++ b/backend/admin/handler.go @@ -196,16 +196,14 @@ func (h *Handler) postGameEdit(c echo.Context) error { if err != nil { return echo.NewHTTPError(http.StatusBadRequest, "Invalid duration_seconds") } - var problemID *int + var problemID int { problemIDRaw := c.FormValue("problem_id") - if problemIDRaw != "" { - problemIDInt, err := strconv.Atoi(problemIDRaw) - if err != nil { - return echo.NewHTTPError(http.StatusBadRequest, "Invalid problem_id") - } - problemID = &problemIDInt + problemIDInt, err := strconv.Atoi(problemIDRaw) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Invalid problem_id") } + problemID = problemIDInt } var startedAt *time.Time { @@ -230,13 +228,6 @@ func (h *Handler) postGameEdit(c echo.Context) error { Valid: true, } } - var changedProblemID *int32 - if problemID == nil { - changedProblemID = nil - } else { - changedProblemID = new(int32) - *changedProblemID = int32(*problemID) - } { // TODO: @@ -255,7 +246,7 @@ func (h *Handler) postGameEdit(c echo.Context) error { DisplayName: displayName, DurationSeconds: int32(durationSeconds), StartedAt: changedStartedAt, - ProblemID: changedProblemID, + ProblemID: int32(problemID), }) if err != nil { return echo.NewHTTPError(http.StatusInternalServerError, err.Error()) diff --git a/backend/admin/templates/game_edit.html b/backend/admin/templates/game_edit.html index f40cc5d..c1e38a6 100644 --- a/backend/admin/templates/game_edit.html +++ b/backend/admin/templates/game_edit.html @@ -43,7 +43,7 @@ </div> <div> <label>Problem ID</label> - <input type="text" name="problem_id" value="{{ if .Game.ProblemID }}{{ .Game.ProblemID }}{{ end }}"> + <input type="text" name="problem_id" value="{{ .Game.ProblemID }}"> </div> <div> <button type="submit">Save</button> diff --git a/backend/api/handler.go b/backend/api/handler.go index 5b53791..e70f298 100644 --- a/backend/api/handler.go +++ b/backend/api/handler.go @@ -89,16 +89,10 @@ func (h *Handler) GetGames(ctx context.Context, _ GetGamesRequestObject, user *a startedAtTimestamp := int(row.StartedAt.Time.Unix()) startedAt = &startedAtTimestamp } - var problem *Problem - if row.ProblemID != nil { - if row.Title == nil || row.Description == nil { - panic("inconsistent data") - } - problem = &Problem{ - ProblemID: int(*row.ProblemID), - Title: *row.Title, - Description: *row.Description, - } + problem := &Problem{ + ProblemID: int(row.ProblemID), + Title: row.Title, + Description: row.Description, } games[i] = Game{ GameID: int(row.GameID), @@ -135,16 +129,11 @@ func (h *Handler) GetGame(ctx context.Context, request GetGameRequestObject, use startedAt = &startedAtTimestamp } var problem *Problem - if row.ProblemID != nil { - if row.Title == nil || row.Description == nil { - panic("inconsistent data") - } - if user.IsAdmin || (GameState(row.State) != Closed && GameState(row.State) != WaitingEntries) { - problem = &Problem{ - ProblemID: int(*row.ProblemID), - Title: *row.Title, - Description: *row.Description, - } + if user.IsAdmin || (GameState(row.State) != Closed && GameState(row.State) != WaitingEntries) { + problem = &Problem{ + ProblemID: int(row.ProblemID), + Title: row.Title, + Description: row.Description, } } playerRows, err := h.q.ListGamePlayers(ctx, int32(gameID)) diff --git a/backend/db/models.go b/backend/db/models.go index 800c183..f6e7ed1 100644 --- a/backend/db/models.go +++ b/backend/db/models.go @@ -16,7 +16,7 @@ type Game struct { DurationSeconds int32 CreatedAt pgtype.Timestamp StartedAt pgtype.Timestamp - ProblemID *int32 + ProblemID int32 } type GamePlayer struct { diff --git a/backend/db/query.sql.go b/backend/db/query.sql.go index cbef51d..583389e 100644 --- a/backend/db/query.sql.go +++ b/backend/db/query.sql.go @@ -136,7 +136,7 @@ func (q *Queries) CreateUserAuth(ctx context.Context, arg CreateUserAuthParams) const getGameByID = `-- name: GetGameByID :one SELECT game_id, game_type, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description FROM games -LEFT JOIN problems ON games.problem_id = problems.problem_id +JOIN problems ON games.problem_id = problems.problem_id WHERE games.game_id = $1 LIMIT 1 ` @@ -149,10 +149,10 @@ type GetGameByIDRow struct { DurationSeconds int32 CreatedAt pgtype.Timestamp StartedAt pgtype.Timestamp - ProblemID *int32 - ProblemID_2 *int32 - Title *string - Description *string + ProblemID int32 + ProblemID_2 int32 + Title string + Description string } func (q *Queries) GetGameByID(ctx context.Context, gameID int32) (GetGameByIDRow, error) { @@ -295,7 +295,7 @@ func (q *Queries) ListGamePlayers(ctx context.Context, gameID int32) ([]ListGame const listGames = `-- name: ListGames :many SELECT game_id, game_type, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description FROM games -LEFT JOIN problems ON games.problem_id = problems.problem_id +JOIN problems ON games.problem_id = problems.problem_id ORDER BY games.game_id ` @@ -307,10 +307,10 @@ type ListGamesRow struct { DurationSeconds int32 CreatedAt pgtype.Timestamp StartedAt pgtype.Timestamp - ProblemID *int32 - ProblemID_2 *int32 - Title *string - Description *string + ProblemID int32 + ProblemID_2 int32 + Title string + Description string } func (q *Queries) ListGames(ctx context.Context) ([]ListGamesRow, error) { @@ -347,7 +347,7 @@ func (q *Queries) ListGames(ctx context.Context) ([]ListGamesRow, error) { const listGamesForPlayer = `-- name: ListGamesForPlayer :many SELECT games.game_id, game_type, state, display_name, duration_seconds, created_at, started_at, games.problem_id, problems.problem_id, title, description, game_players.game_id, user_id FROM games -LEFT JOIN problems ON games.problem_id = problems.problem_id +JOIN problems ON games.problem_id = problems.problem_id JOIN game_players ON games.game_id = game_players.game_id WHERE game_players.user_id = $1 ORDER BY games.game_id @@ -361,10 +361,10 @@ type ListGamesForPlayerRow struct { DurationSeconds int32 CreatedAt pgtype.Timestamp StartedAt pgtype.Timestamp - ProblemID *int32 - ProblemID_2 *int32 - Title *string - Description *string + ProblemID int32 + ProblemID_2 int32 + Title string + Description string GameID_2 int32 UserID int32 } @@ -511,7 +511,7 @@ type UpdateGameParams struct { DisplayName string DurationSeconds int32 StartedAt pgtype.Timestamp - ProblemID *int32 + ProblemID int32 } func (q *Queries) UpdateGame(ctx context.Context, arg UpdateGameParams) error { diff --git a/backend/game/hub.go b/backend/game/hub.go index 54c559c..23fdd0d 100644 --- a/backend/game/hub.go +++ b/backend/game/hub.go @@ -693,16 +693,10 @@ func (hubs *Hubs) RestoreFromDB(ctx context.Context) error { if row.StartedAt.Valid { startedAt = &row.StartedAt.Time } - var pr *problem - if row.ProblemID != nil { - if row.Title == nil || row.Description == nil { - panic("inconsistent data") - } - pr = &problem{ - problemID: int(*row.ProblemID), - title: *row.Title, - description: *row.Description, - } + pr := &problem{ + problemID: int(row.ProblemID), + title: row.Title, + description: row.Description, } // TODO: N+1 playerRows, err := hubs.q.ListGamePlayers(ctx, int32(row.GameID)) diff --git a/backend/query.sql b/backend/query.sql index bcbee12..408bf2d 100644 --- a/backend/query.sql +++ b/backend/query.sql @@ -30,12 +30,12 @@ SELECT EXISTS ( -- name: ListGames :many SELECT * FROM games -LEFT JOIN problems ON games.problem_id = problems.problem_id +JOIN problems ON games.problem_id = problems.problem_id ORDER BY games.game_id; -- name: ListGamesForPlayer :many SELECT * FROM games -LEFT JOIN problems ON games.problem_id = problems.problem_id +JOIN problems ON games.problem_id = problems.problem_id JOIN game_players ON games.game_id = game_players.game_id WHERE game_players.user_id = $1 ORDER BY games.game_id; @@ -52,7 +52,7 @@ WHERE game_id = $1; -- name: GetGameByID :one SELECT * FROM games -LEFT JOIN problems ON games.problem_id = problems.problem_id +JOIN problems ON games.problem_id = problems.problem_id WHERE games.game_id = $1 LIMIT 1; diff --git a/backend/schema.sql b/backend/schema.sql index 2779eaf..dc49363 100644 --- a/backend/schema.sql +++ b/backend/schema.sql @@ -37,7 +37,7 @@ CREATE TABLE games ( duration_seconds INT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT NOW(), started_at TIMESTAMP, - problem_id INT, + problem_id INT NOT NULL, CONSTRAINT fk_problem_id FOREIGN KEY(problem_id) REFERENCES problems(problem_id) ); CREATE INDEX idx_games_problem_id ON games(problem_id); |
