Added move registration and validation

This commit is contained in:
2025-12-12 16:45:12 +01:00
parent 47e812d710
commit f457911f3b
17 changed files with 269 additions and 45 deletions

View File

@@ -1,8 +1,10 @@
package be.naaturel.boardmateapi.controllers;
import be.naaturel.boardmateapi.common.models.Move;
import be.naaturel.boardmateapi.common.models.Game;
import be.naaturel.boardmateapi.controllers.dtos.GameDto;
import be.naaturel.boardmateapi.controllers.mappings.GameMapper;
import be.naaturel.boardmateapi.services.GameService;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -30,47 +32,54 @@ public class GameController {
}
@GetMapping("/games/{id}")
public ResponseEntity<Game> retrieveGames(@PathVariable String id){
public ResponseEntity<ResponseBody<GameDto>> retrieveGames(@PathVariable String id){
ResponseBody<GameDto> response = ResponseBody.createEmpty();
try{
Game g = service.retrieveGame(id);
GameDto dto = GameMapper.toDto(g);
response.setData(dto);
response.setSuccess(true);
return ResponseEntity
.status(HttpStatus.OK)
.body(g);
.body(response);
} catch (Exception e){
e.printStackTrace();
response.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build();
.body(response);
}
}
@PostMapping("/create")
public ResponseEntity<?> CreateParty(@RequestBody Game game){
public ResponseEntity<ResponseBody<String>> CreateParty(@RequestBody GameDto game){
ResponseBody<String> response = ResponseBody.createEmpty();
try{
service.create(game);
Game model = GameMapper.toModel(game);
String result = service.create(model);
response.setData(result);
response.setSuccess(true);
return ResponseEntity.
status(HttpStatus.INTERNAL_SERVER_ERROR)
.build();
status(HttpStatus.OK)
.body(response);
} catch (Exception e){
e.printStackTrace();
response.setMessage(e.getMessage());
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(e.getStackTrace());
.body(response);
}
}
@PostMapping("/moves/add")
public ResponseEntity<?> AddMove(@RequestBody String gameId, @RequestBody Move move){
@PostMapping("/moves/add/{gameId}")
public ResponseEntity<?> AddMove(@PathVariable String gameId, @RequestBody String move){
try{
service.addMove(gameId, move);
return ResponseEntity
.status(HttpStatus.OK)
.build();
} catch (Exception e){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build();
}
}
}

View File

@@ -0,0 +1,49 @@
package be.naaturel.boardmateapi.controllers;
import java.util.Optional;
public class ResponseBody<T> {
private T data;
private String message;
private boolean success;
/**
* Create a default empty response body containing no data, no message and flagged as failure
* @return Empty response of T
* @param <T> The wrapped data type
*/
public static <T> ResponseBody<T> createEmpty() {
return new ResponseBody<>(null, null,false);
}
private ResponseBody(T data, String messageOpt, boolean success){
this.data = data;
this.message = null;
this.success = success;
}
public T getData() {
return data;
}
public Optional<String> getMessage() {
return Optional.ofNullable(message);
}
public boolean isSuccess() {
return success;
}
public void setData(T data) {
this.data = data;
}
public void setSuccess(boolean success) {
this.success = success;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@@ -0,0 +1,40 @@
package be.naaturel.boardmateapi.controllers.dtos;
public class GameDto {
private String whiteName;
private String blackName;
private int timeValue;
private int increment;
public String getWhiteName() {
return whiteName;
}
public String getBlackName() {
return blackName;
}
public int getTimeValue() {
return timeValue;
}
public int getIncrement() {
return increment;
}
public void setWhiteName(String whiteName) {
this.whiteName = whiteName;
}
public void setBlackName(String blackName) {
this.blackName = blackName;
}
public void setTimeValue(int timeValue) {
this.timeValue = timeValue;
}
public void setIncrement(int increment){
this.increment = increment;
}
}

View File

@@ -0,0 +1,22 @@
package be.naaturel.boardmateapi.controllers.mappings;
import be.naaturel.boardmateapi.common.models.Game;
import be.naaturel.boardmateapi.controllers.dtos.GameDto;
public class GameMapper {
public static Game toModel(GameDto dto){
return new Game(dto.getWhiteName(), dto.getBlackName(), dto.getTimeValue(), dto.getIncrement());
}
public static GameDto toDto(Game model){
GameDto g = new GameDto();
g.setWhiteName(model.getWhiteName());
g.setBlackName(model.getBlackName());
g.setTimeValue(model.getTimeValue());
g.setIncrement(model.getIncrement());
return g;
}
}