Skip to content

Instantly share code, notes, and snippets.

@manuelernesto
Created July 5, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelernesto/d25a0a36e3e1e7d60f93813a094cb860 to your computer and use it in GitHub Desktop.
Save manuelernesto/d25a0a36e3e1e7d60f93813a094cb860 to your computer and use it in GitHub Desktop.
@Service
class PlayerService(val repository: PlayerRepository) {
fun getAll(): List<Player> = repository.findAll()
fun getById(id: Long): Player = repository.findByIdOrNull(id) ?:
throw ResponseStatusException(HttpStatus.NOT_FOUND)
fun create(player: Player): Player = repository.save(player)
fun remove(id: Long) {
if (repository.existsById(id)) repository.deleteById(id)
else throw ResponseStatusException(HttpStatus.NOT_FOUND)
}
fun update(id: Long, player: Player): Player {
return if (repository.existsById(id)) {
player.id = id
repository.save(player)
} else throw ResponseStatusException(HttpStatus.NOT_FOUND)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment