package de.fd.fh.server.game.web; import com.fasterxml.jackson.databind.ObjectMapper; import de.fd.fh.server.game.GameId; import de.fd.fh.server.game.GameService; import de.fd.fh.server.user.UserId; import de.fd.fh.shared.network.messages.CreateGameRequest; import lombok.RequiredArgsConstructor; import spark.Request; import spark.Response; import java.io.IOException; @RequiredArgsConstructor public class GameController { private final ObjectMapper objectMapper = new ObjectMapper(); private final GameService service; public void createGame(final Request request, final Response response) throws IOException { final CreateGameRequest createRequest = objectMapper.readValue(request.body(), CreateGameRequest.class); final UserId whiteUser = UserId.of(createRequest.getWhiteUserId()); final UserId blackUser = UserId.of(createRequest.getBlackUserId()); final GameId gameId = service.createGame(blackUser, whiteUser); if (gameId == null) { response.status(400); } else { response.body(objectMapper.writeValueAsString(gameId)); response.status(201); } } }