SpringBoot: GameProposal
- rest controller - poposal endpoint
This commit is contained in:
parent
7403cf671a
commit
36edc91cf3
@ -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<Joke> badJokes() {
|
||||
|
||||
List<VirtualNode> 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<String> 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\"] }" );
|
||||
}
|
||||
|
||||
}
|
@ -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<String> findAllUnconsumed(
|
||||
@AuthenticationPrincipal ApiUserDetails user
|
||||
) {
|
||||
String gpList = cordaClient.listGameProposals(user.getHoldingIdentity());
|
||||
|
||||
return ResponseEntity.ok(gpList);
|
||||
}
|
||||
|
||||
// @PostMapping()
|
||||
// public ResponseEntity<String> gameproposalSend(@AuthenticationPrincipal ApiUserDetails user) {
|
||||
|
||||
// return ResponseEntity.ok("");
|
||||
// }
|
||||
|
||||
@PostMapping()
|
||||
public ResponseEntity<Void> 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();
|
||||
}
|
||||
|
||||
}
|
@ -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 {
|
||||
|
@ -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) {
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package djmil.cordacheckers.game;
|
||||
|
||||
public enum PlayerColor {
|
||||
WHITE,
|
||||
BLACK
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package djmil.cordacheckers.gameproposal;
|
||||
|
||||
public record CreateGameProposal(
|
||||
String opponentName,
|
||||
String opponentColor,
|
||||
String message)
|
||||
{
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user