Steffen Nitschke
4 years ago
3 changed files with 90 additions and 8 deletions
-
40fh.fd.ci.server/src/main/java/de/fd/fh/server/game/web/GameController.java
-
44fh.fd.ci.server/src/test/java/de/fd/fh/server/game/web/GameControllerTest.java
-
14fh.fd.ci.shared/src/main/java/de/fd/fh/shared/network/messages/CreateGameRequest.java
@ -0,0 +1,40 @@ |
|||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,14 @@ |
|||||
|
package de.fd.fh.shared.network.messages; |
||||
|
|
||||
|
import lombok.AllArgsConstructor; |
||||
|
import lombok.Getter; |
||||
|
import lombok.NoArgsConstructor; |
||||
|
|
||||
|
@Getter |
||||
|
@AllArgsConstructor |
||||
|
@NoArgsConstructor |
||||
|
public class CreateGameRequest |
||||
|
{ |
||||
|
private String whiteUserId; |
||||
|
private String blackUserId; |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue