PlayersRoutes.kt 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package re.chasam.routes
  2. import io.ktor.http.*
  3. import io.ktor.server.application.*
  4. import io.ktor.server.request.*
  5. import io.ktor.server.response.*
  6. import io.ktor.server.routing.*
  7. import org.koin.ktor.ext.inject
  8. import re.chasam.models.impl.TournamentImpl
  9. import re.chasam.models.impl.Player
  10. fun Route.playersRouting() {
  11. val tournamentImpl by inject<TournamentImpl>()
  12. route("/players") {
  13. get {
  14. if (tournamentImpl.players.isNotEmpty()) {
  15. call.respond(tournamentImpl.players)
  16. } else {
  17. call.respondText("No players found", status = HttpStatusCode.OK)
  18. }
  19. }
  20. get("{id?}") {
  21. val id = call.parameters["id"] ?: return@get call.respondText("Bad Request", status = HttpStatusCode.BadRequest)
  22. println("status $id")
  23. val player = tournamentImpl.getPlayer(id)
  24. if (player != null) {
  25. call.respond(player)
  26. } else {
  27. call.respondText("No player found", status = HttpStatusCode.NotFound)
  28. }
  29. }
  30. post {
  31. val player = call.receive<Player>()
  32. println("post $player")
  33. tournamentImpl.addOrUpdate(Player(player.name))
  34. call.respondText("player stored correctly", status = HttpStatusCode.Created)
  35. }
  36. patch("{id?}") {
  37. val id = call.parameters["id"] ?: return@patch call.respondText("Bad Request", status = HttpStatusCode.BadRequest)
  38. tournamentImpl.getPlayer(id) ?: return@patch call.respondText("No player found", status = HttpStatusCode.NotFound)
  39. val player = call.receive<Player>()
  40. println("post $id $player")
  41. tournamentImpl.addOrUpdate(id, player.score)
  42. call.respondText("score updated correctly", status = HttpStatusCode.OK)
  43. }
  44. delete("") {
  45. tournamentImpl.clean()
  46. call.respondText("players removed correctly", status = HttpStatusCode.OK)
  47. }
  48. }
  49. }