import io.ktor.server.application.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* import io.ktor.util.pipeline.* import kotlinx.html.* fun Routing.racePage() { get("/race/list") { raceOverviewPage(RaceHolder.races) } get("/races/{id}/show") { } get("/races/{raceId}/remove/{trackId}") { val raceId = call.parameters["raceId"] val trackId = call.parameters["trackId"]?.toLong() RaceHolder.races.find { it.name == raceId }?.let { raceData -> val toBeRemovedTrack = raceData.getTracks().find { it.trackId == trackId } toBeRemovedTrack?.let { raceData.removeTrack(it) } } call.respondRedirect("/races/$raceId/show") } get("/race/new") { newRacePage() } post("/race/new") { val parameters = call.receiveParameters() val name = parameters["name"] val description = parameters["description"] if (name == null || description == null) { newRacePage("Not all parameters given.", name ?: "", description ?: "") return@post } if (RaceHolder.races.any { it.name == name }) { newRacePage("Race with the same name already exists.", name, description) return@post } RaceHolder.races.add(RaceData(name, description)) call.respondRedirect("/race/list") } } private suspend fun PipelineContext.raceOverviewPage(races: ArrayList) { respondThemedHtml("Race Overview") { div("raceList") { races.forEach { div("raceEntry") { h1 { +it.name } p { +it.description } } } } } } private suspend fun PipelineContext.newRacePage( error: String? = null, name: String = "", description: String = "" ) { respondThemedHtml("Race Overview") { form("/race/new", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) { error?.let { p(classes = "error") { +it } } p { +"Name:" textInput(name = "name") { value = name } } p { +"Description:" } p { TEXTAREA(mapOf("name" to "description", "rows" to "4", "cols" to "40"), consumer).visit { +description } } p { submitInput() } } } }