aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/api/handlers.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/api/handlers.go')
-rw-r--r--backend/api/handlers.go189
1 files changed, 1 insertions, 188 deletions
diff --git a/backend/api/handlers.go b/backend/api/handlers.go
index 8dfc11c..a824f17 100644
--- a/backend/api/handlers.go
+++ b/backend/api/handlers.go
@@ -4,10 +4,8 @@ import (
"context"
"errors"
"net/http"
- "time"
"github.com/jackc/pgx/v5"
- "github.com/jackc/pgx/v5/pgtype"
"github.com/labstack/echo/v4"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/auth"
@@ -23,191 +21,6 @@ type GameHubsInterface interface {
StartGame(gameID int) error
}
-func (h *ApiHandler) AdminGetGames(ctx context.Context, request AdminGetGamesRequestObject, user *auth.JWTClaims) (AdminGetGamesResponseObject, error) {
- gameRows, err := h.q.ListGames(ctx)
- if err != nil {
- return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
- }
- 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 != nil {
- if row.Title == nil || row.Description == nil {
- panic("inconsistent data")
- }
- problem = &Problem{
- ProblemID: int(*row.ProblemID),
- Title: *row.Title,
- Description: *row.Description,
- }
- }
- games[i] = Game{
- GameID: int(row.GameID),
- State: GameState(row.State),
- DisplayName: row.DisplayName,
- DurationSeconds: int(row.DurationSeconds),
- StartedAt: startedAt,
- Problem: problem,
- }
- }
- return AdminGetGames200JSONResponse{
- Games: games,
- }, nil
-}
-
-func (h *ApiHandler) AdminGetGame(ctx context.Context, request AdminGetGameRequestObject, user *auth.JWTClaims) (AdminGetGameResponseObject, error) {
- gameID := request.GameID
- row, err := h.q.GetGameByID(ctx, int32(gameID))
- if err != nil {
- if errors.Is(err, pgx.ErrNoRows) {
- return AdminGetGame404JSONResponse{
- NotFoundJSONResponse: NotFoundJSONResponse{
- Message: "Game not found",
- },
- }, nil
- } else {
- return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
- }
- }
- var startedAt *int
- if row.StartedAt.Valid {
- 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,
- }
- }
- game := Game{
- GameID: int(row.GameID),
- State: GameState(row.State),
- DisplayName: row.DisplayName,
- DurationSeconds: int(row.DurationSeconds),
- StartedAt: startedAt,
- Problem: problem,
- }
- return AdminGetGame200JSONResponse{
- Game: game,
- }, nil
-}
-
-func (h *ApiHandler) AdminPutGame(ctx context.Context, request AdminPutGameRequestObject, user *auth.JWTClaims) (AdminPutGameResponseObject, error) {
- gameID := request.GameID
- displayName := request.Body.DisplayName
- durationSeconds := request.Body.DurationSeconds
- problemID := request.Body.ProblemID
- startedAt := request.Body.StartedAt
- state := request.Body.State
-
- game, err := h.q.GetGameByID(ctx, int32(gameID))
- if err != nil {
- if err == pgx.ErrNoRows {
- return AdminPutGame404JSONResponse{
- NotFoundJSONResponse: NotFoundJSONResponse{
- Message: "Game not found",
- },
- }, nil
- } else {
- return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
- }
- }
-
- var changedState string
- if state != nil {
- changedState = string(*state)
- // TODO:
- if changedState != game.State && changedState == "prepare" {
- h.hubs.StartGame(int(gameID))
- }
- } else {
- changedState = game.State
- }
- var changedDisplayName string
- if displayName != nil {
- changedDisplayName = *displayName
- } else {
- changedDisplayName = game.DisplayName
- }
- var changedDurationSeconds int32
- if durationSeconds != nil {
- changedDurationSeconds = int32(*durationSeconds)
- } else {
- changedDurationSeconds = game.DurationSeconds
- }
- var changedStartedAt pgtype.Timestamp
- if startedAt != nil {
- startedAtValue, err := startedAt.Get()
- if err == nil {
- changedStartedAt = pgtype.Timestamp{
- Time: time.Unix(int64(startedAtValue), 0),
- Valid: true,
- }
- }
- } else {
- changedStartedAt = game.StartedAt
- }
- var changedProblemID *int32
- if problemID != nil {
- problemIDValue, err := problemID.Get()
- if err == nil {
- changedProblemID = new(int32)
- *changedProblemID = int32(problemIDValue)
- }
- } else {
- changedProblemID = game.ProblemID
- }
-
- err = h.q.UpdateGame(ctx, db.UpdateGameParams{
- GameID: int32(gameID),
- State: changedState,
- DisplayName: changedDisplayName,
- DurationSeconds: changedDurationSeconds,
- StartedAt: changedStartedAt,
- ProblemID: changedProblemID,
- })
- if err != nil {
- return AdminPutGame400JSONResponse{
- BadRequestJSONResponse: BadRequestJSONResponse{
- Message: err.Error(),
- },
- }, nil
- }
-
- return AdminPutGame204Response{}, nil
-}
-
-func (h *ApiHandler) AdminGetUsers(ctx context.Context, request AdminGetUsersRequestObject, user *auth.JWTClaims) (AdminGetUsersResponseObject, error) {
- users, err := h.q.ListUsers(ctx)
- if err != nil {
- return nil, echo.NewHTTPError(http.StatusInternalServerError, err.Error())
- }
- responseUsers := make([]User, len(users))
- for i, u := range users {
- responseUsers[i] = User{
- UserID: int(u.UserID),
- Username: u.Username,
- DisplayName: u.DisplayName,
- IconPath: u.IconPath,
- IsAdmin: u.IsAdmin,
- }
- }
- return AdminGetUsers200JSONResponse{
- Users: responseUsers,
- }, nil
-}
-
func (h *ApiHandler) PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) {
username := request.Body.Username
password := request.Body.Password
@@ -311,7 +124,7 @@ func (h *ApiHandler) GetGame(ctx context.Context, request GetGameRequestObject,
if row.Title == nil || row.Description == nil {
panic("inconsistent data")
}
- if user.IsAdmin || (GameState(row.State) != GameStateClosed && GameState(row.State) != GameStateWaitingEntries) {
+ if user.IsAdmin || (GameState(row.State) != Closed && GameState(row.State) != WaitingEntries) {
problem = &Problem{
ProblemID: int(*row.ProblemID),
Title: *row.Title,