From 36edc91cf3ad1cd6a1790d2758274f6c89afc6fd Mon Sep 17 00:00:00 2001 From: djmil Date: Sun, 3 Sep 2023 16:27:54 +0200 Subject: [PATCH] SpringBoot: GameProposal - rest controller - poposal endpoint --- .../djmil/cordacheckers/ApiController.java | 43 ---------- .../cordacheckers/GameProposalController.java | 78 +++++++++++++++++++ .../cordaclient/CordaClient.java | 5 ++ .../cordaclient/dao/GameProposal.java | 13 ++++ .../djmil/cordacheckers/game/PlayerColor.java | 6 ++ .../gameproposal/CreateGameProposal.java | 9 +++ 6 files changed, 111 insertions(+), 43 deletions(-) delete mode 100644 backend/src/main/java/djmil/cordacheckers/ApiController.java create mode 100644 backend/src/main/java/djmil/cordacheckers/GameProposalController.java create mode 100644 backend/src/main/java/djmil/cordacheckers/cordaclient/dao/GameProposal.java create mode 100644 backend/src/main/java/djmil/cordacheckers/game/PlayerColor.java create mode 100644 backend/src/main/java/djmil/cordacheckers/gameproposal/CreateGameProposal.java diff --git a/backend/src/main/java/djmil/cordacheckers/ApiController.java b/backend/src/main/java/djmil/cordacheckers/ApiController.java deleted file mode 100644 index 0a80227..0000000 --- a/backend/src/main/java/djmil/cordacheckers/ApiController.java +++ /dev/null @@ -1,43 +0,0 @@ -package djmil.cordacheckers; - -import java.util.List; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.ResponseEntity; -import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -import djmil.cordacheckers.cordaclient.CordaClient; -import djmil.cordacheckers.cordaclient.dao.VirtualNode; - -@RestController -public class ApiController { - - @Autowired - CordaClient cordaClient; - - @GetMapping("/api/badjokes") - public ResponseEntity badJokes() { - - List vNodeList = cordaClient.getVirtualNodeList(); - - Joke joke = new Joke("What do you call a fly without wings? A walk! " + vNodeList.get(1).holdingIdentity().x500Name()); - - return ResponseEntity.ok(joke); - } - - /** - * - * @return a Json list of active games - */ - @GetMapping("/api/gameproposals") - public ResponseEntity dashboard(@AuthenticationPrincipal ApiUserDetails user) { - System.out.println("List of active games for " - + "user: " + user.getUsername() - + " with HoldingIdentity ShortHash: " + user.getHoldingIdentity().shortHash()); - - return ResponseEntity.ok("{ \"UnconsumedGameProposals\" : [\"id_game1\", \"id_game2\"] }" ); - } - -} \ No newline at end of file diff --git a/backend/src/main/java/djmil/cordacheckers/GameProposalController.java b/backend/src/main/java/djmil/cordacheckers/GameProposalController.java new file mode 100644 index 0000000..7f24f44 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/GameProposalController.java @@ -0,0 +1,78 @@ +package djmil.cordacheckers; + +import java.net.URI; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.util.UriComponentsBuilder; + +import djmil.cordacheckers.cordaclient.CordaClient; +import djmil.cordacheckers.cordaclient.HoldingIdentityResolver; +import djmil.cordacheckers.cordaclient.dao.HoldingIdentity; +import djmil.cordacheckers.cordaclient.dao.VirtualNode; +import djmil.cordacheckers.game.PlayerColor; +import djmil.cordacheckers.gameproposal.CreateGameProposal; + +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; + + +@RestController +@RequestMapping("/gameproposal") +public class GameProposalController { + + @Autowired + CordaClient cordaClient; + + @Autowired + HoldingIdentityResolver holdingIdentityResolver; + + @GetMapping + public ResponseEntity findAllUnconsumed( + @AuthenticationPrincipal ApiUserDetails user + ) { + String gpList = cordaClient.listGameProposals(user.getHoldingIdentity()); + + return ResponseEntity.ok(gpList); + } + + // @PostMapping() + // public ResponseEntity gameproposalSend(@AuthenticationPrincipal ApiUserDetails user) { + + // return ResponseEntity.ok(""); + // } + + @PostMapping() + public ResponseEntity create( + @AuthenticationPrincipal ApiUserDetails player, + @RequestBody CreateGameProposal gpRequest, + UriComponentsBuilder ucb + ) { + final HoldingIdentity gpSender = player.getHoldingIdentity(); + // TODO: throw execption with custom type + final HoldingIdentity gpReceiver = holdingIdentityResolver.getByCommonName(gpRequest.opponentName()); + final PlayerColor gpReceiverColor = PlayerColor.valueOf(gpRequest.opponentColor()); + + String newGameProposalUuid = cordaClient.sendGameProposal( + gpSender, + gpReceiver, + gpReceiverColor, + gpRequest.message() + ); + + URI locationOfNewGameProposal = ucb + .path("gameproposal/{id}") + .buildAndExpand(newGameProposalUuid) + .toUri(); + + return ResponseEntity + .created(locationOfNewGameProposal) + .build(); + } + +} \ No newline at end of file diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java index 5460d3c..7edbc37 100644 --- a/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/CordaClient.java @@ -22,6 +22,7 @@ import djmil.cordacheckers.cordaclient.dao.VirtualNodeList; import djmil.cordacheckers.cordaclient.dao.flow.RequestBody; import djmil.cordacheckers.cordaclient.dao.flow.ResponseBody; import djmil.cordacheckers.cordaclient.dao.flow.arguments.Empty; +import djmil.cordacheckers.game.PlayerColor; @Service public class CordaClient { @@ -72,6 +73,10 @@ public class CordaClient { return gameProposalsJsonString; } + public String sendGameProposal(HoldingIdentity sender, HoldingIdentity receiver, PlayerColor receiverColor, String message) { + return ""; + } + private String cordaFlowExecute(HoldingIdentity holdingIdentity, RequestBody requestBody) { try { diff --git a/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/GameProposal.java b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/GameProposal.java new file mode 100644 index 0000000..87f6d58 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/cordaclient/dao/GameProposal.java @@ -0,0 +1,13 @@ +package djmil.cordacheckers.cordaclient.dao; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +@JsonDeserialize +public record GameProposal( + String sender, + String recipient, + String youPlayAs, + String additionalMessage, + String id) { + +} diff --git a/backend/src/main/java/djmil/cordacheckers/game/PlayerColor.java b/backend/src/main/java/djmil/cordacheckers/game/PlayerColor.java new file mode 100644 index 0000000..9be7be9 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/game/PlayerColor.java @@ -0,0 +1,6 @@ +package djmil.cordacheckers.game; + +public enum PlayerColor { + WHITE, + BLACK +} diff --git a/backend/src/main/java/djmil/cordacheckers/gameproposal/CreateGameProposal.java b/backend/src/main/java/djmil/cordacheckers/gameproposal/CreateGameProposal.java new file mode 100644 index 0000000..f7a46df --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/gameproposal/CreateGameProposal.java @@ -0,0 +1,9 @@ +package djmil.cordacheckers.gameproposal; + +public record CreateGameProposal( + String opponentName, + String opponentColor, + String message) +{ + +}