can now display poule data

This commit is contained in:
Joachim 2018-09-01 17:02:27 +02:00
parent 4f766c31e7
commit dba491bf1b
4 changed files with 168 additions and 1 deletions

9
conf/config.json Normal file
View File

@ -0,0 +1,9 @@
{
"database": {
"host": "localhost",
"username": "raceviewer",
"password": "viewingdatraceboiii",
"database" : "race",
"port": 5432
}
}

17
pom.xml
View File

@ -44,6 +44,16 @@
<artifactId>vertx-jdbc-client</artifactId>
<version>${vertx-version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx-version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-mysql-postgresql-client</artifactId>
<version>${vertx-version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
@ -55,6 +65,13 @@
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>

View File

@ -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 <T> RoutingContext.end(singleObject: Single<T>) {
singleObject.subscribe({
this.response().end(Json.encode(it))
}, {
this.fail(it)
})
}

View File

@ -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<String>) {
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)
}
}