@RestController
@RequestMapping("/player")
@Profile("api")
public class PlayerController{
private static final Logger log = LoggerFactory.getLogger(PlayerController.class);
private final PlayerService playerService;
@Autowired
public PlayerController(PlayerService playerService) {
this.playerService = playerService;
}
@GetMapping
public ResponseEntity<List<Player>> findAll() {
return ResponseEntity.ok(playerService.findAll());
}
@GetMapping("/{id}")
public ResponseEntity<Player> findById(@PathVariable("id") Long id) {
log.debug("Finding player by id: {}", id);
return playerService.findById(id)
.map(ResponseEntity::ok)
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Player not found with id: " + id
));
}
@PostMapping
public ResponseEntity<Player> create(@RequestBody Player player) {
return ResponseEntity.ok(playerService.save(player));
}
@PutMapping("/{id}")
public ResponseEntity<Player> update(@PathVariable("id") Long id, @RequestBody Player player) {
player.setId(id);
return ResponseEntity.ok(playerService.save(player));
}
@PatchMapping("/{id}")
public ResponseEntity<Player> patchPlayer(Long id, Map<String, Object> updates) {
Player existingPlayer = playerService.findById(id)
.orElseThrow(() -> new ResponseStatusException(
HttpStatus.NOT_FOUND,
"Player not found with id: " + id
));
return ResponseEntity.ok(playerService.patch(existingPlayer, updates));
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable("id") Long id) {
playerService.deleteById(id);
return ResponseEntity.ok(null);
}
}