diff options
Diffstat (limited to 'backend/api')
| -rw-r--r-- | backend/api/generated.go | 159 | ||||
| -rw-r--r-- | backend/api/handlers.go | 93 |
2 files changed, 239 insertions, 13 deletions
diff --git a/backend/api/generated.go b/backend/api/generated.go index 4455723..921f4b3 100644 --- a/backend/api/generated.go +++ b/backend/api/generated.go @@ -17,9 +17,34 @@ import ( "github.com/getkin/kin-openapi/openapi3" "github.com/labstack/echo/v4" + "github.com/oapi-codegen/runtime" strictecho "github.com/oapi-codegen/runtime/strictmiddleware/echo" ) +// Defines values for GameState. +const ( + Closed GameState = "closed" + Finished GameState = "finished" + Gaming GameState = "gaming" + Prepare GameState = "prepare" + Starting GameState = "starting" + WaitingEntries GameState = "waiting_entries" + WaitingStart GameState = "waiting_start" +) + +// Game defines model for Game. +type Game struct { + DisplayName string `json:"display_name"` + DurationSeconds int `json:"duration_seconds"` + GameId int `json:"game_id"` + Problem *Problem `json:"problem,omitempty"` + StartedAt *int `json:"started_at,omitempty"` + State GameState `json:"state"` +} + +// GameState defines model for Game.State. +type GameState string + // JwtPayload defines model for JwtPayload. type JwtPayload struct { DisplayName string `json:"display_name"` @@ -29,6 +54,19 @@ type JwtPayload struct { Username string `json:"username"` } +// Problem defines model for Problem. +type Problem struct { + Description string `json:"description"` + ProblemId int `json:"problem_id"` + Title string `json:"title"` +} + +// GetGamesParams defines parameters for GetGames. +type GetGamesParams struct { + PlayerId *int `form:"player_id,omitempty" json:"player_id,omitempty"` + Authorization string `json:"Authorization"` +} + // PostLoginJSONBody defines parameters for PostLogin. type PostLoginJSONBody struct { Password string `json:"password"` @@ -40,6 +78,9 @@ type PostLoginJSONRequestBody PostLoginJSONBody // ServerInterface represents all server handlers. type ServerInterface interface { + // List games + // (GET /games) + GetGames(ctx echo.Context, params GetGamesParams) error // User login // (POST /login) PostLogin(ctx echo.Context) error @@ -50,6 +91,43 @@ type ServerInterfaceWrapper struct { Handler ServerInterface } +// GetGames converts echo context to params. +func (w *ServerInterfaceWrapper) GetGames(ctx echo.Context) error { + var err error + + // Parameter object where we will unmarshal all parameters from the context + var params GetGamesParams + // ------------- Optional query parameter "player_id" ------------- + + err = runtime.BindQueryParameter("form", true, false, "player_id", ctx.QueryParams(), ¶ms.PlayerId) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter player_id: %s", err)) + } + + headers := ctx.Request().Header + // ------------- Required header parameter "Authorization" ------------- + if valueList, found := headers[http.CanonicalHeaderKey("Authorization")]; found { + var Authorization string + n := len(valueList) + if n != 1 { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Expected one value for Authorization, got %d", n)) + } + + err = runtime.BindStyledParameterWithOptions("simple", "Authorization", valueList[0], &Authorization, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationHeader, Explode: false, Required: true}) + if err != nil { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter Authorization: %s", err)) + } + + params.Authorization = Authorization + } else { + return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Header parameter Authorization is required, but not found")) + } + + // Invoke the callback with all the unmarshaled arguments + err = w.Handler.GetGames(ctx, params) + return err +} + // PostLogin converts echo context to params. func (w *ServerInterfaceWrapper) PostLogin(ctx echo.Context) error { var err error @@ -87,10 +165,41 @@ func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL Handler: si, } + router.GET(baseURL+"/games", wrapper.GetGames) router.POST(baseURL+"/login", wrapper.PostLogin) } +type GetGamesRequestObject struct { + Params GetGamesParams +} + +type GetGamesResponseObject interface { + VisitGetGamesResponse(w http.ResponseWriter) error +} + +type GetGames200JSONResponse struct { + Games []Game `json:"games"` +} + +func (response GetGames200JSONResponse) VisitGetGamesResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(200) + + return json.NewEncoder(w).Encode(response) +} + +type GetGames403JSONResponse struct { + Message string `json:"message"` +} + +func (response GetGames403JSONResponse) VisitGetGamesResponse(w http.ResponseWriter) error { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(403) + + return json.NewEncoder(w).Encode(response) +} + type PostLoginRequestObject struct { Body *PostLoginJSONRequestBody } @@ -123,6 +232,9 @@ func (response PostLogin401JSONResponse) VisitPostLoginResponse(w http.ResponseW // StrictServerInterface represents all server handlers. type StrictServerInterface interface { + // List games + // (GET /games) + GetGames(ctx context.Context, request GetGamesRequestObject) (GetGamesResponseObject, error) // User login // (POST /login) PostLogin(ctx context.Context, request PostLoginRequestObject) (PostLoginResponseObject, error) @@ -140,6 +252,31 @@ type strictHandler struct { middlewares []StrictMiddlewareFunc } +// GetGames operation middleware +func (sh *strictHandler) GetGames(ctx echo.Context, params GetGamesParams) error { + var request GetGamesRequestObject + + request.Params = params + + handler := func(ctx echo.Context, request interface{}) (interface{}, error) { + return sh.ssi.GetGames(ctx.Request().Context(), request.(GetGamesRequestObject)) + } + for _, middleware := range sh.middlewares { + handler = middleware(handler, "GetGames") + } + + response, err := handler(ctx, request) + + if err != nil { + return err + } else if validResponse, ok := response.(GetGamesResponseObject); ok { + return validResponse.VisitGetGamesResponse(ctx.Response()) + } else if response != nil { + return fmt.Errorf("unexpected response type: %T", response) + } + return nil +} + // PostLogin operation middleware func (sh *strictHandler) PostLogin(ctx echo.Context) error { var request PostLoginRequestObject @@ -172,14 +309,20 @@ func (sh *strictHandler) PostLogin(ctx echo.Context) error { // Base64 encoded, gzipped, json marshaled Swagger object var swaggerSpec = []string{ - "H4sIAAAAAAAC/6RSzY7aQAx+lcjnKAToKbetemHVA1LVU1Uhk5gwdDKejp1lo1XevZqkZElB2kpwIJnJ", - "Z/v78RuU3Hh25FSgeAMpj9Tg8Pp81i12lrGKJx/YU1BDw7fKiLfY7Rw2FM/0io23BAU889ElX5ggBe18", - "vBENxtXQp2BKdjuPepyXLEyDNcnixEeXnXx9t1R2WDXGzSoPaIUm8J7ZErqIboXCzlQz8HK1nqDGKdUU", - "LtBbFZHKLY0+hUC/WxOoguLHNOWqSTp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97geAs+N6QP0AKnR8gzHGYcLcwIuozDdu0zy4JFflj+l0jpfX6n9ge", + "TpUT39BMyVHlhxDMzhruuFxnWXjk1giZSAudq3QeKy8fuXPJsd7UTKMkWqjm1zZi3O/tODf0Hg+zC4b/", + "Y7oT78JXzZLYfdJltCncZKt3cKmJGYszw/5u/U4rRSYZp/2qdYdCb+Ew1o9VuKlr9IeBW0+sTWFZ2aJb", + "UM7yjPk2luVrDOmgEMuvVh3eoYZD5mfrp9d8PL26Xs1th3cuvH6vja3nBZx6vf1QP4v9m87W4o/wtzj5", + "/yqVrshbpv9Hk+fEvG+q6pBgIyUZCVBJdXa++mg735snrLRKck8q9MKKP9TOQ/1hmon1yTjOqcP/YvJJ", + "Z+u2bdt/AwAA//+RpToKFwoAAA==", } // GetSwagger returns the content of the embedded swagger specification file 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 |
