I have created a Spring Boot Microservice for playing Rock Paper Scissrors. I have tried to follow best practices, but still would appreciate some critique on my code. Thanks.
@RestController
public class RockPaperScissorsController {
@Autowired
private PlayerService playerService;
@Autowired
private HttpServletRequest context;
@Autowired
private GameSessionService gameSessionService;
@Autowired
private GameplayService gameplayService;
public RockPaperScissorsController(PlayerService playerService,
GameSessionService gameSessionService) {
this.playerService = playerService;
this.gameSessionService = gameSessionService;
}
@GetMapping(value = "/ping", produces = "application/json")
public ResponseEntity<String> ping() {
return ResponseEntity.ok("{\"response\":\"pong\"}");
}
@GetMapping(value = "/player/{playerName}", produces = "application/json")
public ResponseEntity player(@PathVariable("playerName") String playerName) {
try {
Player player = playerService.getPlayer(playerName);
return ResponseEntity.ok(player);
} catch (RPSException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
@PostMapping(value = "/player/{playerName}", produces = "application/json")
public ResponseEntity playerPOST(@PathVariable("playerName") String playerName) {
try {
playerService.createPlayer(playerName);
return ResponseEntity.ok().body("");
} catch (RPSException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
@DeleteMapping(value = "/player/{playerName}", produces = "application/json")
public ResponseEntity playerDELETE(@PathVariable("playerName") String playerName) {
try {
playerService.deletePlayer(playerName);
return ResponseEntity.ok("");
} catch (RPSException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
@PostMapping(value = "/createInvite/{playerName}", produces = "application/json")
public ResponseEntity createInvite(@PathVariable("playerName") String inviter) {
try {
Player player = playerService.getPlayer(inviter);
GameSession session = gameSessionService.createSessionFrom(new Invite(player));
return ResponseEntity.ok(session);
} catch (RPSException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
@PostMapping(value = "/acceptInvite/{inviteCode}/{playerName}", produces = "application/json")
public ResponseEntity acceptInvite(@PathVariable("inviteCode") String inviteCode,
@PathVariable("playerName") String playerName) throws InvalidOperationException {
try {
Player player = playerService.getPlayer(playerName);
return ResponseEntity.ok(gameSessionService.acceptInvite(player, inviteCode));
} catch (RPSException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
@GetMapping(value = "/session/{inviteCode}", produces = "application/json")
public ResponseEntity session(@PathVariable("inviteCode") String inviteCode) {
return ResponseEntity.ok(gameSessionService.sessions().get(inviteCode));
}
@PostMapping(value = "/readyplayer/{playername}", produces = "application/json")
public ResponseEntity ready(@PathVariable("playername") String playerName) {
try {
Player player = playerService.changePlayerState(playerName, Player.State.READY);
return ResponseEntity.ok(player);
} catch (RPSException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
@PostMapping(value = "/play", produces = "application/json")
public ResponseEntity play(@RequestBody PlayRequest playRequest) {
try {
gameplayService.play(playRequest);
return ResponseEntity.ok().body("");
} catch (RPSException e) {
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.NOT_IMPLEMENTED).body("");
}
}