@@ 1,6 1,6 @@
/*
* This file is part of sdbapi
- * Copyright (C) 2022 Jonni Liljamo <jonni@liljamo.com>
+ * Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* Licensed under GPL-3.0-only.
* See LICENSE for licensing information.
@@ 10,41 10,40 @@ package apierror
// general errors
var (
- Placeholder APIError = APIError{1000, "Placeholder", "placeholder"}
- InvalidInput APIError = APIError{1001, "InvalidInput", "invalid input"}
- NotAuthorized APIError = APIError{1002, "NotAuthorized", "not authorized"}
- MissingAuth APIError = APIError{1003, "MissingAuthorization", "missing authorization"}
+ Placeholder APIError = APIError{1000, "Placeholder", "placeholder"}
+ InvalidInput APIError = APIError{1001, "InvalidInput", "invalid input"}
+ NotAuthorized APIError = APIError{1002, "NotAuthorized", "not authorized"}
+ MissingAuth APIError = APIError{1003, "MissingAuthorization", "missing authorization"}
GenericJWTError APIError = APIError{1004, "GenericJWTError", ""}
)
// user related errors
var (
UserNotFound APIError = APIError{2000, "UserNotFound", "user not found"}
- InvalidCred APIError = APIError{2001, "InvalidCred", "invalid credentials"}
- NewJWTError APIError = APIError{2003, "NewJWTError", "jwt creation error"}
+ InvalidCred APIError = APIError{2001, "InvalidCred", "invalid credentials"}
+ NewJWTError APIError = APIError{2003, "NewJWTError", "jwt creation error"}
// login errors
- EmailInvalid APIError = APIError{2100, "EmailInvalid", "invalid email address"}
- UsernameTooShort APIError = APIError{2101, "UsernameTooShort", "username should not be shroter than 3 characters"}
- PasswordTooShort APIError = APIError{2102, "PasswordTooShort", "password should not be shorter than 8 characters"}
+ EmailInvalid APIError = APIError{2100, "EmailInvalid", "invalid email address"}
+ UsernameTooShort APIError = APIError{2101, "UsernameTooShort", "username should not be shroter than 3 characters"}
+ PasswordTooShort APIError = APIError{2102, "PasswordTooShort", "password should not be shorter than 8 characters"}
PasswordHashFailed APIError = APIError{2103, "PasswordHashFailed", "password hash failed"}
UserCreationFailed APIError = APIError{2104, "UserCreationFailed", "user creation failed"}
)
// game related errors
var (
- GameNotFound APIError = APIError{3000, "GameNotFound", "game not found"}
- GameCreationFailed APIError = APIError{3001, "GameCreationFailed", "game creation failed"}
- GetAllFormingFailed APIError = APIError{3002, "GetAllFormingFailed", "failed to get all forming games"}
+ GameNotFound APIError = APIError{3000, "GameNotFound", "game not found"}
+ GameCreationFailed APIError = APIError{3001, "GameCreationFailed", "game creation failed"}
+ GetAllFormingFailed APIError = APIError{3002, "GetAllFormingFailed", "failed to get all forming games"}
GameStatePatchFailed APIError = APIError{3003, "GameStatePatchFailed", "failed to patch game state"}
- NoGamesForUser APIError = APIError{3004, "NoGamesForUser", "no games found for user"}
- GameNotForming APIError = APIError{3005, "GameNotForming", "game is not forming"}
- CannotJoinOwnGame APIError = APIError{3006, "CannotJoinOwnGame", "can not join own game"}
- GameFull APIError = APIError{3007, "GameFull", "game is full"}
+ NoGamesForUser APIError = APIError{3004, "NoGamesForUser", "no games found for user"}
+ GameNotForming APIError = APIError{3005, "GameNotForming", "game is not forming"}
+ CannotJoinOwnGame APIError = APIError{3006, "CannotJoinOwnGame", "can not join own game"}
+ GameFull APIError = APIError{3007, "GameFull", "game is full"}
)
type APIError struct {
- ID uint16 `json:"id"`
- Name string `json:"name"`
+ ID uint16 `json:"id"`
+ Name string `json:"name"`
Description string `json:"description"`
}
-
@@ 44,6 44,32 @@ func PatchGameState(c *gin.Context) {
return
}
+ // store failed state
+ failed := false
+ // a long set of verification
+ if game.State == models.GAMESTATE_FORMING {
+ // allow INPROGRESS and CANCELLED
+ if patchedGame.State != models.GAMESTATE_INPROGRESS && patchedGame.State != models.GAMESTATE_CANCELLED {
+ failed = true
+ }
+ } else if game.State == models.GAMESTATE_INPROGRESS {
+ // allow only FINISHED
+ if patchedGame.State != models.GAMESTATE_FINISHED {
+ failed = true
+ }
+ } else if game.State == models.GAMESTATE_FINISHED {
+ failed = true
+ } else if game.State == models.GAMESTATE_CANCELLED {
+ failed = true
+ }
+
+ if failed {
+ c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.GameStatePatchFailed})
+ c.Abort()
+ return
+ }
+
+ // everything is fine so far, so go through with it
updatedRecord := db.DbConn.Model(&game).Update("state", patchedGame.State)
if updatedRecord.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": apierror.GameStatePatchFailed})
@@ 52,5 78,5 @@ func PatchGameState(c *gin.Context) {
}
// don't have anything to return
- c.JSON(http.StatusNoContent, nil)
+ c.JSON(http.StatusOK, gin.H{})
}