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.go93
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