Add validation to utils
This commit is contained in:
@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.mattrixwv.raidbuilder.annotation.AccountAuthorization;
|
||||
import com.mattrixwv.raidbuilder.annotation.GameAuthorization;
|
||||
@@ -22,6 +23,7 @@ import com.mattrixwv.raidbuilder.entity.Game;
|
||||
import com.mattrixwv.raidbuilder.service.GameService;
|
||||
import com.mattrixwv.raidbuilder.util.DatabaseTypeUtil.AccountPermissionType;
|
||||
import com.mattrixwv.raidbuilder.util.DatabaseTypeUtil.GamePermissionType;
|
||||
import com.mattrixwv.raidbuilder.util.validation.GameValidationUtil;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -34,8 +36,19 @@ import lombok.extern.slf4j.Slf4j;
|
||||
public class GameController{
|
||||
private final ObjectMapper mapper;
|
||||
private final GameService gameService;
|
||||
//Utilities
|
||||
private final GameValidationUtil gvUtil;
|
||||
|
||||
|
||||
@GetMapping("/{gameId}")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
||||
public Game getGame(@PathVariable("gameId") UUID gameId){
|
||||
log.info("Getting game {}", gameId);
|
||||
|
||||
|
||||
return gameService.getGameById(gameId);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
||||
public List<Game> getGames(@RequestParam("page") int page, @RequestParam("pageSize") int pageSize, @RequestParam(value = "searchTerm", required = false) String searchTerm){
|
||||
@@ -85,28 +98,29 @@ public class GameController{
|
||||
log.info("Creating game {}", gameName);
|
||||
|
||||
|
||||
//TODO: New game verification
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
Game game = new Game();
|
||||
game.setGameName(gameName);
|
||||
game = gameService.createGame(game, file);
|
||||
returnNode.put("gameId", game.getGameId().toString());
|
||||
returnNode.put("status", "success");
|
||||
List<String> errors = gvUtil.validateNewGame(game);
|
||||
if(errors.isEmpty()){
|
||||
game = gameService.createGame(game, file);
|
||||
|
||||
returnNode.put("gameId", game.getGameId().toString());
|
||||
returnNode.put("status", "success");
|
||||
|
||||
log.info("Successfully created game: {}", game.getGameId());
|
||||
}
|
||||
else{
|
||||
returnNode.put("status", "error");
|
||||
ArrayNode errorNode = mapper.createArrayNode();
|
||||
errors.forEach(errorNode::add);
|
||||
returnNode.set("errors", errorNode);
|
||||
}
|
||||
|
||||
log.info("Successfully created game: {}", game.getGameId());
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
@GetMapping("/{gameId}")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
||||
public Game getGame(@PathVariable("gameId") UUID gameId){
|
||||
log.info("Getting game {}", gameId);
|
||||
|
||||
|
||||
return gameService.getGameById(gameId);
|
||||
}
|
||||
|
||||
@PutMapping("/{gameId}")
|
||||
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
||||
@GameAuthorization(permissions = {GamePermissionType.ADMIN})
|
||||
@@ -119,11 +133,19 @@ public class GameController{
|
||||
game.setGameId(gameId);
|
||||
game.setGameName(gameName);
|
||||
game.setGameIcon(gameIcon);
|
||||
game = gameService.updateGame(game, file);
|
||||
returnNode.put("gameId", game.getGameId().toString());
|
||||
returnNode.put("status", "success");
|
||||
List<String> errors = gvUtil.validateExistingGame(game);
|
||||
if(errors.isEmpty()){
|
||||
game = gameService.updateGame(game, file);
|
||||
|
||||
log.info("Successfully updated game: {}", game.getGameId());
|
||||
returnNode.put("gameId", game.getGameId().toString());
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
else{
|
||||
returnNode.put("status", "error");
|
||||
ArrayNode errorNode = mapper.createArrayNode();
|
||||
errors.forEach(errorNode::add);
|
||||
returnNode.set("errors", errorNode);
|
||||
}
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
@@ -136,10 +158,19 @@ public class GameController{
|
||||
|
||||
|
||||
ObjectNode returnNode = mapper.createObjectNode();
|
||||
gameService.deleteById(gameId);
|
||||
returnNode.put("status", "success");
|
||||
Game game = gameService.getGameById(gameId);
|
||||
if(game != null){
|
||||
gameService.deleteById(gameId);
|
||||
|
||||
returnNode.put("status", "success");
|
||||
}
|
||||
else{
|
||||
returnNode.put("status", "error");
|
||||
ArrayNode errorNode = mapper.createArrayNode();
|
||||
errorNode.add("Game ID is invalid");
|
||||
returnNode.set("errors", errorNode);
|
||||
}
|
||||
|
||||
log.info("Successfully deleted game: {}", gameId);
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user