80 lines
2.8 KiB
Java
80 lines
2.8 KiB
Java
package be.naaturel.boardmateapi.controllers;
|
|
|
|
import be.naaturel.boardmateapi.common.exceptions.ServiceException;
|
|
import be.naaturel.boardmateapi.common.models.Game;
|
|
import be.naaturel.boardmateapi.controllers.dtos.ResponseBody;
|
|
import be.naaturel.boardmateapi.controllers.dtos.GameDto;
|
|
import be.naaturel.boardmateapi.controllers.mappings.GameMapper;
|
|
import be.naaturel.boardmateapi.services.GameService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
@RestController
|
|
public class GameController {
|
|
|
|
private final GameService service;
|
|
|
|
@Autowired
|
|
public GameController(GameService service){
|
|
this.service = service;
|
|
}
|
|
|
|
@GetMapping("/games/{id}")
|
|
public ResponseEntity<ResponseBody<GameDto>> retrieveGames(@PathVariable String id){
|
|
ResponseBody<GameDto> result = ResponseBody.createEmpty();
|
|
try{
|
|
Game g = service.retrieveGame(id);
|
|
GameDto dto = GameMapper.toDto(g);
|
|
result.setData(dto);
|
|
result.setSuccess(true);
|
|
return ResponseEntity
|
|
.status(HttpStatus.OK)
|
|
.body(result);
|
|
} catch (Exception e){
|
|
result.setMessage(e.getMessage());
|
|
return ResponseEntity
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
.body(result);
|
|
}
|
|
}
|
|
|
|
@PostMapping("/create")
|
|
public ResponseEntity<ResponseBody<String>> CreateParty(@RequestBody GameDto game){
|
|
ResponseBody<String> result = ResponseBody.createEmpty();
|
|
try{
|
|
Game model = GameMapper.toModel(game);
|
|
String id = service.create(model);
|
|
result.setData(id);
|
|
result.setSuccess(true);
|
|
return ResponseEntity.
|
|
status(HttpStatus.OK)
|
|
.body(result);
|
|
} catch (Exception e){
|
|
result.setMessage(e.getMessage());
|
|
return ResponseEntity
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
.body(result);
|
|
}
|
|
}
|
|
|
|
@PostMapping("/moves/add/{gameId}")
|
|
public ResponseEntity<ResponseBody<String>> AddMove(@PathVariable String gameId, @RequestBody String move){
|
|
ResponseBody<String> result = ResponseBody.createEmpty();
|
|
try{
|
|
String gamedId = service.addMove(gameId, move);
|
|
result.setSuccess(true);
|
|
result.setData(gamedId);
|
|
return ResponseEntity
|
|
.status(HttpStatus.OK)
|
|
.body(result);
|
|
} catch (ServiceException e){
|
|
result.setMessage(e.getMessage());
|
|
return ResponseEntity
|
|
.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
|
.body(result);
|
|
}
|
|
}
|
|
}
|