I have the following endpoint with a nested (if) else if statement
@Patch("/games/:id")
@HttpCode(200)
async updateGame(@Param("id") id: number, @Body() update: Partial<Game>) {
const updatedGame = await Game.findOne(id);
if (!updatedGame) {
throw new NotFoundError("HTTP 404 Not Found: No Games Here");
} else if (update.color && !validColor(update.color)) {
throw new BadRequestError("HTTP 400 Bad Request: No Such Color");
} else if (
update.board !== undefined &&
moves(update.board, updatedGame.board) > 1
) {
throw new BadRequestError(
"HTTP 400 Bad Request: Only one move allowed. Wait your turn"
);
} else {
console.log("Game has been updated");
return Game.merge(updatedGame, update).save();
}
}
And I would like to convert it to a switch statement for readability I've tried several attempts but somehow the values of the constant variables aren't being read.
the following is my switch statement attempt to replace the else if list
switch(updatedGame){
case (!updatedGame):
throw new NotFoundError("HTTP 404 Not Found: No Games Here")
case(update.color && !validColor(update.color)):
throw new BadRequestError("HTTP 400 Bad Request: No Such Color")
case(update.board !== undefined && moves(update.board, updatedGame.board) > 1):
throw new BadRequestError("HTTP 400 Bad Request: Only one move allowed. Wait your turn")
default : console.log("Game has been updated");
Game.merge(updatedGame, update).save();
}
the if else if list work as it should but the switch statement gives errors
elseneeded after throwing an exception ...switchis what thecaseevaluates, and how can it with e.g. ´case(update.color && !validColor(update.color))´ when neither is related toupdateGame?