From dba491bf1beedc7480587384b38772f98310f5ae Mon Sep 17 00:00:00 2001 From: Joachim Date: Sat, 1 Sep 2018 17:02:27 +0200 Subject: [PATCH] can now display poule data --- conf/config.json | 9 ++++ pom.xml | 17 ++++++ src/main/java/RouteDefiner.kt | 99 +++++++++++++++++++++++++++++++++++ src/main/java/Server.kt | 44 +++++++++++++++- 4 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 conf/config.json create mode 100644 src/main/java/RouteDefiner.kt diff --git a/conf/config.json b/conf/config.json new file mode 100644 index 0000000..4bf3506 --- /dev/null +++ b/conf/config.json @@ -0,0 +1,9 @@ +{ + "database": { + "host": "localhost", + "username": "raceviewer", + "password": "viewingdatraceboiii", + "database" : "race", + "port": 5432 + } +} diff --git a/pom.xml b/pom.xml index d0781bc..631f42d 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,16 @@ vertx-jdbc-client ${vertx-version} + + io.vertx + vertx-web + ${vertx-version} + + + io.vertx + vertx-mysql-postgresql-client + ${vertx-version} + org.jetbrains.kotlin kotlin-stdlib-jdk8 @@ -55,6 +65,13 @@ ${kotlin.version} test + + + org.slf4j + slf4j-api + 1.7.25 + + diff --git a/src/main/java/RouteDefiner.kt b/src/main/java/RouteDefiner.kt new file mode 100644 index 0000000..601639c --- /dev/null +++ b/src/main/java/RouteDefiner.kt @@ -0,0 +1,99 @@ +import io.reactivex.Single +import io.vertx.core.json.Json +import io.vertx.kotlin.core.json.JsonArray +import io.vertx.reactivex.core.Vertx +import io.vertx.reactivex.ext.asyncsql.AsyncSQLClient +import io.vertx.reactivex.ext.web.Router +import io.vertx.reactivex.ext.web.RoutingContext + +class RouteDefiner(router: Router, vertx: Vertx, postgreSQLClient: AsyncSQLClient) { + + init { + /** + * get all championships + */ + router.get("/championship").handler { rc -> + rc.end(postgreSQLClient.rxQuery("select * from championship where publishresults").map { + it.rows + }) + } + + router.get("/championship/:idchampionship/tiers").handler { rc -> + rc.end(postgreSQLClient.rxQueryWithParams("select distinct(tier) from poule where idchampionship = ? order by tier", + JsonArray(rc.pathParam("idchampionship").toInt())).map { + println(it.rows) + it.rows.map { it.getInteger("tier") } + }) + } + + router.get("/championship/:idchampionship/tier/:tier/poules").handler { rc -> + rc.end(postgreSQLClient.rxQueryWithParams("select distinct(poule) from poule where idchampionship = ? and tier = ? order by poule", + JsonArray( + rc.pathParam("idchampionship").toInt(), + rc.pathParam("tier").toInt() + )).map { + println(it.rows) + it.rows.map { it.getInteger("poule") } + }) + } + + /** + * Get all teams in a poule + */ + router.get("/championship/:idchampionship/tier/:tier/poule/:poule/teams").handler { rc -> + rc.end(postgreSQLClient.rxQueryWithParams("select idteam, team.name, count(distinct idrace) racecount\n" + + "from poule\n" + + " inner join team_poule using (poule, tier, idchampionship)\n" + + " inner join team using (idteam, idchampionship)\n" + + "inner join race using (idteam, tier, poule, idchampionship)\n" + + "where poule = ?\n" + + " and tier = ?\n" + + " and idchampionship = ?\n" + + "group by idteam, team.name", + JsonArray( + rc.pathParam("poule").toInt(), + rc.pathParam("tier").toInt(), + rc.pathParam("idchampionship").toInt() + )).map { + it.rows + }) + } + + /** + * Get all times in a poule + */ + router.get("/championship/:idchampionship/tier/:tier/poule/:poule/times").handler { rc -> + rc.end(postgreSQLClient.rxQueryWithParams("select idteam, idrace, array_agg(extract(milliseconds from laptime)) laptimesms,\n" + + " (select sum(penaltyseconds) from comment where comment.idrace = race.idrace) penaltysum\n" + + "from poule\n" + + " inner join team_poule using (poule, tier, idchampionship)\n" + + " inner join team using (idteam, idchampionship)\n" + + " inner join race using (tier, idchampionship, idteam, poule)\n" + + "inner join valid_laptimes using (idrace)\n" + + "where poule = ?\n" + + " and tier = ?\n" + + " and idchampionship = ?\n" + + "and publishresults\n" + + "group by idteam, idrace\n" + + "order by idteam, idrace", + JsonArray( + rc.pathParam("poule").toInt(), + rc.pathParam("tier").toInt(), + rc.pathParam("idchampionship").toInt() + )).map { + it.rows + }) + } + } +} + +/** + * Gracefully handle a single of something encodable. + */ +private fun RoutingContext.end(singleObject: Single) { + singleObject.subscribe({ + this.response().end(Json.encode(it)) + }, { + this.fail(it) + }) +} diff --git a/src/main/java/Server.kt b/src/main/java/Server.kt index 821278c..7703135 100644 --- a/src/main/java/Server.kt +++ b/src/main/java/Server.kt @@ -1,7 +1,49 @@ -import io.vertx.core.Vertx +import io.vertx.core.http.HttpMethod +import io.vertx.reactivex.config.ConfigRetriever +import io.vertx.reactivex.core.Vertx +import io.vertx.reactivex.ext.asyncsql.PostgreSQLClient +import io.vertx.reactivex.ext.web.Router +import io.vertx.reactivex.ext.web.handler.CorsHandler fun main(args: Array) { var vertx = Vertx.vertx() + // fetching config file + var retriever = ConfigRetriever.create(vertx) + retriever.rxGetConfig().subscribe { config -> + // single out the database config + val databaseConfig = config.getJsonObject("database") + + // make the jdbc client, ready for connections + var postgreSQLClient = PostgreSQLClient.createShared(vertx, databaseConfig) + + var router = Router.router(vertx) + + + // set up cors + router.route().handler( + CorsHandler.create("http://localhost:4200") + .allowCredentials(true) + .allowedHeader("Access-Control-Allow-Method") + .allowedHeader("Access-Control-Allow-Origin") + .allowedHeader("Access-Control-Allow-Credentials") + .allowedHeader("Content-Type") + .allowedMethod(HttpMethod.GET) + .allowedMethod(HttpMethod.POST) + .allowedMethod(HttpMethod.PUT) + .allowedMethod(HttpMethod.OPTIONS) + ) + + // define the routes + val routeDefiner = RouteDefiner(router, vertx, postgreSQLClient) + + + var server = vertx.createHttpServer() + + server.requestHandler { router.accept(it) }.listen(8080) + + + } + } \ No newline at end of file