diff options
| author | nsfisis <nsfisis@gmail.com> | 2024-07-28 16:01:41 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2024-07-28 17:12:45 +0900 |
| commit | 2d5f913a431c4223a16c88551ffff4100ac483c4 (patch) | |
| tree | 4ea9f9db9dbe7cf1b7720205ae281a6b8bcca8e9 /backend/api/handlers.go | |
| parent | 0dd94cbea6e857896c46d17493725f97369d99f9 (diff) | |
| download | iosdc-japan-2025-albatross-2d5f913a431c4223a16c88551ffff4100ac483c4.tar.gz iosdc-japan-2025-albatross-2d5f913a431c4223a16c88551ffff4100ac483c4.tar.zst iosdc-japan-2025-albatross-2d5f913a431c4223a16c88551ffff4100ac483c4.zip | |
feat: implement game entry
Diffstat (limited to 'backend/api/handlers.go')
| -rw-r--r-- | backend/api/handlers.go | 93 |
1 files changed, 88 insertions, 5 deletions
diff --git a/backend/api/handlers.go b/backend/api/handlers.go index 162e30a..9856ce9 100644 --- a/backend/api/handlers.go +++ b/backend/api/handlers.go @@ -11,6 +11,8 @@ import ( "github.com/nsfisis/iosdc-2024-albatross-backend/db" ) +var _ StrictServerInterface = (*ApiHandler)(nil) + type ApiHandler struct { q *db.Queries } @@ -28,22 +30,20 @@ func (h *ApiHandler) PostLogin(ctx context.Context, request PostLoginRequestObje if err != nil { return PostLogin401JSONResponse{ Message: "Invalid username or password", - }, echo.NewHTTPError(http.StatusUnauthorized, "Invalid username or password") + }, nil } user, err := h.q.GetUserById(ctx, int32(userId)) if err != nil { return PostLogin401JSONResponse{ Message: "Invalid username or password", - }, echo.NewHTTPError(http.StatusUnauthorized, "Invalid username or password") + }, nil } jwt, err := auth.NewJWT(&user) if err != nil { // TODO - return PostLogin401JSONResponse{ - Message: "Internal Server Error", - }, echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error") + return nil, echo.NewHTTPError(http.StatusInternalServerError) } return PostLogin200JSONResponse{ @@ -51,6 +51,89 @@ func (h *ApiHandler) PostLogin(ctx context.Context, request PostLoginRequestObje }, nil } +func (h *ApiHandler) GetGames(ctx context.Context, request GetGamesRequestObject) (GetGamesResponseObject, error) { + user := ctx.Value("user").(*auth.JWTClaims) + playerId := request.Params.PlayerId + if !user.IsAdmin { + if playerId == nil || *playerId != user.UserID { + return GetGames403JSONResponse{ + Message: "Forbidden", + }, nil + } + } + if playerId == nil { + gameRows, err := h.q.ListGames(ctx) + if err != nil { + return nil, echo.NewHTTPError(http.StatusInternalServerError) + } + games := make([]Game, len(gameRows)) + for i, row := range gameRows { + var startedAt *int + if row.StartedAt.Valid { + startedAtTimestamp := int(row.StartedAt.Time.Unix()) + startedAt = &startedAtTimestamp + } + var problem *Problem + if row.ProblemID.Valid { + if !row.Title.Valid || !row.Description.Valid { + panic("inconsistent data") + } + problem = &Problem{ + ProblemId: int(row.ProblemID.Int32), + Title: row.Title.String, + Description: row.Description.String, + } + } + games[i] = Game{ + GameId: int(row.GameID), + State: GameState(row.State), + DisplayName: row.DisplayName, + DurationSeconds: int(row.DurationSeconds), + StartedAt: startedAt, + Problem: problem, + } + } + return GetGames200JSONResponse{ + Games: games, + }, nil + } else { + gameRows, err := h.q.ListGamesForPlayer(ctx, int32(*playerId)) + if err != nil { + return nil, echo.NewHTTPError(http.StatusInternalServerError) + } + games := make([]Game, len(gameRows)) + for i, row := range gameRows { + var startedAt *int + if row.StartedAt.Valid { + startedAtTimestamp := int(row.StartedAt.Time.Unix()) + startedAt = &startedAtTimestamp + } + var problem *Problem + if row.ProblemID.Valid { + if !row.Title.Valid || !row.Description.Valid { + panic("inconsistent data") + } + problem = &Problem{ + ProblemId: int(row.ProblemID.Int32), + Title: row.Title.String, + Description: row.Description.String, + } + } + games[i] = Game{ + GameId: int(row.GameID), + State: GameState(row.State), + DisplayName: row.DisplayName, + DurationSeconds: int(row.DurationSeconds), + StartedAt: startedAt, + Problem: problem, + } + } + return GetGames200JSONResponse{ + Games: games, + }, nil + } +} + func _assertJwtPayloadIsCompatibleWithJWTClaims() { var c auth.JWTClaims var p JwtPayload |
