use kotlin serialize

This commit is contained in:
Torma Kristóf 2021-08-18 16:44:29 +02:00
parent f46386d465
commit 4fc8db6fa4
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
7 changed files with 45 additions and 43 deletions

View File

@ -7,15 +7,21 @@ buildscript {
dependencies { dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.jengelman.gradle.plugins:shadow:5.2.0" classpath "com.github.jengelman.gradle.plugins:shadow:5.2.0"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
} }
} }
plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.5.21'
}
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
sourceCompatibility = JavaVersion.VERSION_11 sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11
} }
apply plugin: 'kotlin' apply plugin: 'kotlin'
apply plugin: 'kotlinx-serialization'
apply plugin: "com.github.johnrengelman.shadow" apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'application' apply plugin: 'application'
@ -43,13 +49,15 @@ dependencies {
compile 'org.jetbrains.exposed:exposed-jdbc:0.33.1' compile 'org.jetbrains.exposed:exposed-jdbc:0.33.1'
compile 'org.jetbrains.exposed:exposed-java-time:0.33.1' compile 'org.jetbrains.exposed:exposed-java-time:0.33.1'
compile 'com.zaxxer:HikariCP:5.0.0' compile 'com.zaxxer:HikariCP:5.0.0'
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2"
implementation "org.jetbrains.kotlinx:kotlinx-datetime:0.2.1"
implementation "io.insert-koin:koin-core:$koin_version" implementation "io.insert-koin:koin-core:$koin_version"
implementation "io.insert-koin:koin-ktor:$koin_version" implementation "io.insert-koin:koin-ktor:$koin_version"
implementation "io.ktor:ktor-serialization:$ktor_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "io.ktor:ktor-server-netty:$ktor_version" implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "ch.qos.logback:logback-classic:$logback_version" implementation "ch.qos.logback:logback-classic:$logback_version"
implementation "io.ktor:ktor-server-core:$ktor_version" implementation "io.ktor:ktor-server-core:$ktor_version"
implementation "io.ktor:ktor-gson:$ktor_version"
implementation "io.ktor:ktor-server-host-common:$ktor_version" implementation "io.ktor:ktor-server-host-common:$ktor_version"
implementation "io.ktor:ktor-client-core:$ktor_version" implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-core-jvm:$ktor_version" implementation "io.ktor:ktor-client-core-jvm:$ktor_version"

View File

@ -5,11 +5,11 @@ import io.ktor.application.*
import io.ktor.response.* import io.ktor.response.*
import io.ktor.routing.* import io.ktor.routing.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.gson.*
import io.ktor.features.* import io.ktor.features.*
import org.apache.http.HttpException import org.apache.http.HttpException
import com.kmalbz.database.DatabaseFactory import com.kmalbz.database.DatabaseFactory
import com.kmalbz.database.dao.ResultObjects import com.kmalbz.database.dao.ResultObjects
import io.ktor.serialization.*
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
@ -21,8 +21,7 @@ fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@Suppress("unused") // Referenced in application.conf @Suppress("unused") // Referenced in application.conf
fun Application.module() { fun Application.module() {
install(ContentNegotiation) { install(ContentNegotiation) {
gson { json()
}
} }
install(Koin) { install(Koin) {

View File

@ -1,9 +1,9 @@
package com.kmalbz.api.model package com.kmalbz.api.model
import com.google.gson.annotations.SerializedName import kotlinx.serialization.Serializable
import java.util.*
@Serializable
data class ApiObject( data class ApiObject(
@SerializedName("tag") val tag: String, val tag: String,
@SerializedName("probability") val probability: Double val probability: Double
) )

View File

@ -6,11 +6,9 @@ import io.ktor.http.HttpStatusCode
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Routing import io.ktor.routing.Routing
import io.ktor.routing.get import io.ktor.routing.get
import kotlinx.datetime.LocalDateTime
import org.koin.ktor.ext.inject import org.koin.ktor.ext.inject
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import kotlin.math.ceil
import kotlin.math.roundToInt
/** /**
* Output Service - RDB * Output Service - RDB
@ -40,41 +38,39 @@ class OutputServiceRDBServer {
get("/output/filter/negative") { get("/output/filter/negative") {
val resultList = val resultList =
resultObjectService.getResultObjecLessthanProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) resultObjectService.getResultObjecLessthanProbability(0.5)
call.respond(resultList) call.respond(resultList)
} }
get("/output/filter/positive") { get("/output/filter/positive") {
val resultList = val resultList =
resultObjectService.getResultObjecGreaterthanProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) resultObjectService.getResultObjecGreaterthanProbability(0.5)
call.respond(resultList) call.respond(resultList)
} }
get("/output/filter/undecided") { get("/output/filter/undecided") {
val resultList = val resultList =
resultObjectService.getResultObjecEqualsProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) resultObjectService.getResultObjecEqualsProbability(0.5)
call.respond(resultList) call.respond(resultList)
} }
get("/output/after/{dateAfter}") { get("/output/after/{dateAfter}") {
val dateAfter = call.parameters["dateAfter"] ?: error(HttpStatusCode.NotAcceptable) val dateAfter = call.parameters["dateAfter"] ?: error(HttpStatusCode.NotAcceptable)
val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE val localDateAfter: LocalDateTime = LocalDateTime.parse(dateAfter)
val localDateAfter: LocalDate = LocalDate.parse(dateAfter, dateTimeFormatter)
val resultList = val resultList =
resultObjectService.getResultObjectafterDate(localDateAfter) ?: call.respond(HttpStatusCode.NotFound) resultObjectService.getResultObjectafterDate(localDateAfter)
call.respond(resultList) call.respond(resultList)
} }
get("/output/before/{dateBefore}") { get("/output/before/{dateBefore}") {
val dateAfter = call.parameters["dateBefore"] ?: error(HttpStatusCode.NotAcceptable) val dateAfter = call.parameters["dateBefore"] ?: error(HttpStatusCode.NotAcceptable)
val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE val localDateBefore: LocalDateTime = LocalDateTime.parse(dateAfter)
val localDateBefore: LocalDate = LocalDate.parse(dateAfter, dateTimeFormatter)
val resultList = val resultList =
resultObjectService.getResultObjectbeforeDate(localDateBefore) ?: call.respond(HttpStatusCode.NotFound) resultObjectService.getResultObjectbeforeDate(localDateBefore)
call.respond(resultList) call.respond(resultList)
} }

View File

@ -2,12 +2,12 @@ package com.kmalbz.database.dao
import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.`java-time`.date import org.jetbrains.exposed.sql.`java-time`.datetime
import java.time.LocalDate import java.time.LocalDateTime
object ResultObjects : IntIdTable() { object ResultObjects : IntIdTable() {
val tag: Column<String> = varchar("tag", 32) val tag: Column<String> = varchar("tag", 32)
val date: Column<LocalDate> = date("date").default(LocalDate.now()) val date: Column<LocalDateTime> = datetime("date").default(LocalDateTime.now())
val probability: Column<Double> = double("probability") val probability: Column<Double> = double("probability")
override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id") override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id")
} }

View File

@ -1,8 +1,7 @@
package com.kmalbz.database.service package com.kmalbz.database.service
import com.kmalbz.api.model.ApiObject import com.kmalbz.api.model.ApiObject
import java.time.LocalDate import kotlinx.datetime.LocalDateTime
import java.util.*
interface IResultObjectService{ interface IResultObjectService{
fun addOne(apiObject: ApiObject) fun addOne(apiObject: ApiObject)
@ -10,10 +9,10 @@ interface IResultObjectService{
suspend fun getCount(): Long suspend fun getCount(): Long
suspend fun getPage(page: Long): List<ApiObject> suspend fun getPage(page: Long): List<ApiObject>
suspend fun getResultObjectbyTag(tag: String): ApiObject? suspend fun getResultObjectbyTag(tag: String): ApiObject?
suspend fun getResultObjectbyDate(date: LocalDate): List<ApiObject>? suspend fun getResultObjectbyDate(date: LocalDateTime): List<ApiObject>
suspend fun getResultObjectbeforeDate(date: LocalDate): List<ApiObject>? suspend fun getResultObjectbeforeDate(date: LocalDateTime): List<ApiObject>
suspend fun getResultObjectafterDate(date: LocalDate): List<ApiObject>? suspend fun getResultObjectafterDate(date: LocalDateTime): List<ApiObject>
suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>? suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>
suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>? suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>
suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>? suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>
} }

View File

@ -4,12 +4,12 @@ import com.kmalbz.database.DatabaseFactory.dbQuery
import com.kmalbz.database.model.ResultObject import com.kmalbz.database.model.ResultObject
import com.kmalbz.database.dao.ResultObjects import com.kmalbz.database.dao.ResultObjects
import com.kmalbz.api.model.ApiObject import com.kmalbz.api.model.ApiObject
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDateTime
import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import java.time.LocalDate
import java.util.*
class ResultObjectService : IResultObjectService { class ResultObjectService : IResultObjectService {
@ -42,37 +42,37 @@ class ResultObjectService : IResultObjectService {
.singleOrNull() .singleOrNull()
} }
override suspend fun getResultObjectbyDate(date: LocalDate): List<ApiObject>? = dbQuery { override suspend fun getResultObjectbyDate(date: LocalDateTime): List<ApiObject> = dbQuery {
ResultObjects.select { ResultObjects.select {
(ResultObjects.date eq date) (ResultObjects.date eq date.toJavaLocalDateTime())
}.mapNotNull { toResultObject(it) } }.mapNotNull { toResultObject(it) }
} }
override suspend fun getResultObjectbeforeDate(date: LocalDate): List<ApiObject>? = dbQuery { override suspend fun getResultObjectbeforeDate(date: LocalDateTime): List<ApiObject> = dbQuery {
ResultObjects.select { ResultObjects.select {
(ResultObjects.date less date) (ResultObjects.date less date.toJavaLocalDateTime())
}.mapNotNull { toResultObject(it) } }.mapNotNull { toResultObject(it) }
} }
override suspend fun getResultObjectafterDate(date: LocalDate): List<ApiObject>? = dbQuery { override suspend fun getResultObjectafterDate(date: LocalDateTime): List<ApiObject> = dbQuery {
ResultObjects.select { ResultObjects.select {
(ResultObjects.date greater date) (ResultObjects.date greater date.toJavaLocalDateTime())
}.mapNotNull { toResultObject(it) } }.mapNotNull { toResultObject(it) }
} }
override suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>? = dbQuery { override suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject> = dbQuery {
ResultObjects.select { ResultObjects.select {
(ResultObjects.probability greater probability) (ResultObjects.probability greater probability)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toResultObject(it) }
} }
override suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>? = dbQuery { override suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject> = dbQuery {
ResultObjects.select { ResultObjects.select {
(ResultObjects.probability less probability) (ResultObjects.probability less probability)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toResultObject(it) }
} }
override suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>? = dbQuery { override suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject> = dbQuery {
ResultObjects.select { ResultObjects.select {
(ResultObjects.probability eq probability) (ResultObjects.probability eq probability)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toResultObject(it) }