Add Download CSV Url

Dieser Commit ist enthalten in:
Chaoscaot 2023-02-15 20:34:23 +01:00
Ursprung f290e6c1c4
Commit 6b30a53765

Datei anzeigen

@ -20,9 +20,11 @@
package de.steamwar.routes
import de.steamwar.ResponseError
import de.steamwar.data.Groups
import de.steamwar.sql.Event
import de.steamwar.sql.EventFight
import de.steamwar.sql.SchematicType
import de.steamwar.sql.Team
import de.steamwar.sql.TeamTeilnahme
import io.ktor.http.*
import io.ktor.server.application.*
@ -30,6 +32,7 @@ import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import java.lang.StringBuilder
import java.sql.Timestamp
import java.time.Instant
@ -106,6 +109,34 @@ fun Routing.configureEventsRoute() {
}
call.respond(EventFight.getFromEvent(event.eventID).map { ResponseEventFight(it) })
}
get("/csv") {
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {
call.respond(HttpStatusCode.BadRequest, ResponseError("Invalid ID"))
return@get
}
val event = Event.get(id)
if (event == null) {
call.respond(HttpStatusCode.NotFound, ResponseError("Event not found"))
return@get
}
val fights = EventFight.getFromEvent(event.eventID)
val csv = StringBuilder();
csv.append(arrayOf("Start", "BlueTeam", "RedTeam", "WinnerTeam", "Group").joinToString(","))
fights.forEach {
csv.appendLine()
val blue = Team.get(it.teamBlue)
val red = Team.get(it.teamRed)
val winner = if (it.ergebnis == 1) blue.teamName else if(it.ergebnis == 2) red.teamName else "Tie"
csv.append(arrayOf(it.startTime.toString(), Team.get(it.teamBlue).teamName, Team.get(it.teamRed).teamName, winner, Groups.getGroup(it.fightID)).joinToString(","))
}
call.response.header("Content-Disposition", "attachment; filename=\"${event.eventName}.csv\"")
call.response.header("Content-Type", "text/csv")
call.response.header("Content-Transfer-Encoding", "binary")
call.response.header("Pragma", "no-cache")
call.respondText(csv.toString(), ContentType.Text.Plain, HttpStatusCode.OK)
}
put {
val id = call.parameters["id"]?.toIntOrNull()
if (id == null) {