Games tab on admin page working
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
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.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/game")
|
||||
@RequiredArgsConstructor
|
||||
public class GameController{
|
||||
private final ObjectMapper mapper;
|
||||
private final GameService gameService;
|
||||
|
||||
|
||||
@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");
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
//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");
|
||||
|
||||
log.info("Successfully created game: {}", game.getGameId());
|
||||
|
||||
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);
|
||||
game = gameService.updateGame(game, file);
|
||||
returnNode.put("gameId", game.getGameId().toString());
|
||||
returnNode.put("status", "success");
|
||||
|
||||
log.info("Successfully updated game: {}", game.getGameId());
|
||||
|
||||
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();
|
||||
gameService.deleteById(gameId);
|
||||
returnNode.put("status", "success");
|
||||
|
||||
log.info("Successfully deleted game: {}", gameId);
|
||||
|
||||
return returnNode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user