178 lines
5.7 KiB
Java
178 lines
5.7 KiB
Java
package com.mattrixwv.raidbuilder.controller;
|
|
|
|
|
|
import java.util.List;
|
|
import java.util.UUID;
|
|
|
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
import org.springframework.web.bind.annotation.PutMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import com.mattrixwv.raidbuilder.annotation.AccountAuthorization;
|
|
import com.mattrixwv.raidbuilder.annotation.GameAuthorization;
|
|
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;
|
|
import tools.jackson.databind.ObjectMapper;
|
|
import tools.jackson.databind.node.ArrayNode;
|
|
import tools.jackson.databind.node.ObjectNode;
|
|
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/game")
|
|
@RequiredArgsConstructor
|
|
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){
|
|
log.info("Getting games page {} of size {} with search term {}", page, pageSize, searchTerm);
|
|
|
|
|
|
List<Game> games;
|
|
if((searchTerm == null) || (searchTerm.isBlank())){
|
|
games = gameService.getGames(page, pageSize);
|
|
}
|
|
else{
|
|
games = gameService.getGames(page, pageSize, searchTerm);
|
|
}
|
|
|
|
|
|
return games;
|
|
}
|
|
|
|
|
|
@GetMapping("/count")
|
|
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
|
public ObjectNode getGamesCount(@RequestParam(value = "searchTerm", required = false) String searchTerm){
|
|
log.info("Getting games count with search term {}", searchTerm);
|
|
|
|
|
|
Long gamesCount;
|
|
if((searchTerm == null) || (searchTerm.isBlank())){
|
|
gamesCount = gameService.getGamesCount();
|
|
}
|
|
else{
|
|
gamesCount = gameService.getGamesCount(searchTerm);
|
|
}
|
|
|
|
ObjectNode countNode = mapper.createObjectNode();
|
|
countNode.put("count", gamesCount);
|
|
countNode.put("status", "success");
|
|
|
|
|
|
return countNode;
|
|
}
|
|
|
|
|
|
@PostMapping
|
|
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
|
@GameAuthorization(permissions = {GamePermissionType.ADMIN})
|
|
public ObjectNode createGame(@RequestParam(value = "iconFile", required = false) MultipartFile file, @RequestParam("gameName") String gameName){
|
|
log.info("Creating game {}", gameName);
|
|
|
|
|
|
ObjectNode returnNode = mapper.createObjectNode();
|
|
Game game = new Game();
|
|
game.setGameName(gameName);
|
|
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);
|
|
}
|
|
|
|
|
|
return returnNode;
|
|
}
|
|
|
|
@PutMapping("/{gameId}")
|
|
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
|
@GameAuthorization(permissions = {GamePermissionType.ADMIN})
|
|
public ObjectNode updateGame(@PathVariable("gameId") UUID gameId, @RequestParam(value = "iconFile", required = false) MultipartFile file, @RequestParam("gameName") String gameName, @RequestParam(name = "gameIcon", required = false) String gameIcon){
|
|
log.info("Updating game {}", gameName);
|
|
|
|
|
|
ObjectNode returnNode = mapper.createObjectNode();
|
|
Game game = new Game();
|
|
game.setGameId(gameId);
|
|
game.setGameName(gameName);
|
|
game.setGameIcon(gameIcon);
|
|
List<String> errors = gvUtil.validateExistingGame(game);
|
|
if(errors.isEmpty()){
|
|
game = gameService.updateGame(game, file);
|
|
|
|
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;
|
|
}
|
|
|
|
@DeleteMapping("/{gameId}")
|
|
@AccountAuthorization(permissions = {AccountPermissionType.ADMIN, AccountPermissionType.USER})
|
|
@GameAuthorization(permissions = {GamePermissionType.ADMIN})
|
|
public ObjectNode deleteGame(@PathVariable("gameId") UUID gameId){
|
|
log.info("Deleting game {}", gameId);
|
|
|
|
|
|
ObjectNode returnNode = mapper.createObjectNode();
|
|
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);
|
|
}
|
|
|
|
|
|
return returnNode;
|
|
}
|
|
}
|