use kotlin serialization

This commit is contained in:
Torma Kristóf 2021-08-18 15:42:24 +02:00
parent f2ab81e0f4
commit c9411b6424
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
6 changed files with 36 additions and 27 deletions

View File

@ -7,15 +7,21 @@ buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
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) {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
apply plugin: 'kotlin'
apply plugin: 'kotlinx-serialization'
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'application'
@ -47,13 +53,15 @@ dependencies {
compile 'org.jetbrains.exposed:exposed-jdbc:0.33.1'
compile 'org.jetbrains.exposed:exposed-java-time:0.33.1'
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-ktor:$koin_version"
implementation "io.ktor:ktor-serialization:$ktor_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "ch.qos.logback:logback-classic:$logback_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-client-core:$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.routing.*
import io.ktor.http.*
import io.ktor.gson.*
import io.ktor.features.*
import org.apache.http.HttpException
import com.kmalbz.database.DatabaseFactory
import com.kmalbz.database.dao.SampleObjects
import io.ktor.serialization.*
import io.ktor.util.*
import org.jetbrains.exposed.sql.SchemaUtils
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
fun Application.module() {
install(ContentNegotiation) {
gson {
}
json()
}
install(Koin) {

View File

@ -1,10 +1,11 @@
package com.kmalbz.api.model
import com.google.gson.annotations.SerializedName
import java.time.LocalDate
import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable
@Serializable
data class ApiObject(
@SerializedName("tag") val tag: String,
@SerializedName("device_id") val device_id: Int,
@SerializedName("device_date") val device_date: LocalDate
val tag: String,
val device_id: Int,
val device_date: LocalDateTime
)

View File

@ -2,13 +2,13 @@ package com.kmalbz.database.dao
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.`java-time`.date
import java.time.LocalDate
import org.jetbrains.exposed.sql.`java-time`.datetime
import java.time.LocalDateTime
object SampleObjects : IntIdTable() {
val tag: Column<String> = varchar("tag", 32)
val timestamp: Column<LocalDate> = date("timestamp").default(LocalDate.now())
val timestamp: Column<LocalDateTime> = datetime("timestamp").default(LocalDateTime.now())
val device_id: Column<Int> = integer("device_id")
val device_date: Column<LocalDate> = date("device_date")
val device_date: Column<LocalDateTime> = datetime("device_date")
override val primaryKey = PrimaryKey(id, name = "PK_SampleObject_Id")
}

View File

@ -1,8 +1,7 @@
package com.kmalbz.database.service
import com.kmalbz.api.model.ApiObject
import java.time.LocalDate
import java.util.*
import kotlinx.datetime.LocalDateTime
interface ISampleObjectService{
fun addOne(apiObject: ApiObject)
@ -11,7 +10,7 @@ interface ISampleObjectService{
suspend fun getPage(page: Long): List<ApiObject>
suspend fun getSampleObjectbyDeviceID(device_id: Int): List<ApiObject>
suspend fun getSampleObjectbyTag(tag: String): ApiObject?
suspend fun getSampleObjectbyDate(date: LocalDate): List<ApiObject>
suspend fun getSampleObjectbeforeDate(date: LocalDate): List<ApiObject>
suspend fun getSampleObjectafterDate(date: LocalDate): List<ApiObject>
suspend fun getSampleObjectbyDate(date: LocalDateTime): List<ApiObject>
suspend fun getSampleObjectbeforeDate(date: LocalDateTime): List<ApiObject>
suspend fun getSampleObjectafterDate(date: LocalDateTime): List<ApiObject>
}

View File

@ -5,11 +5,13 @@ import com.kmalbz.database.model.SampleObject
import com.kmalbz.database.dao.SampleObjects
import com.kmalbz.api.model.ApiObject
import io.ktor.util.*
import kotlinx.datetime.LocalDateTime
import kotlinx.datetime.toJavaLocalDateTime
import kotlinx.datetime.toKotlinLocalDateTime
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import java.time.LocalDate
@KtorExperimentalAPI
@ -19,7 +21,7 @@ class SampleObjectService : ISampleObjectService {
transaction {
SampleObject.new {
tag = apiObject.tag
device_date = apiObject.device_date
device_date = apiObject.device_date.toJavaLocalDateTime()
device_id = apiObject.device_id
}
}
@ -50,28 +52,28 @@ class SampleObjectService : ISampleObjectService {
.singleOrNull()
}
override suspend fun getSampleObjectbyDate(date: LocalDate): List<ApiObject> = dbQuery {
override suspend fun getSampleObjectbyDate(date: LocalDateTime): List<ApiObject> = dbQuery {
SampleObjects.select {
(SampleObjects.timestamp eq date)
(SampleObjects.timestamp eq date.toJavaLocalDateTime())
}.mapNotNull { toSampleObject(it) }
}
override suspend fun getSampleObjectbeforeDate(date: LocalDate): List<ApiObject> = dbQuery {
override suspend fun getSampleObjectbeforeDate(date: LocalDateTime): List<ApiObject> = dbQuery {
SampleObjects.select {
(SampleObjects.timestamp less date)
(SampleObjects.timestamp less date.toJavaLocalDateTime())
}.mapNotNull { toSampleObject(it) }
}
override suspend fun getSampleObjectafterDate(date: LocalDate): List<ApiObject> = dbQuery {
override suspend fun getSampleObjectafterDate(date: LocalDateTime): List<ApiObject> = dbQuery {
SampleObjects.select {
(SampleObjects.timestamp greater date)
(SampleObjects.timestamp greater date.toJavaLocalDateTime())
}.mapNotNull { toSampleObject(it) }
}
private fun toSampleObject(row: ResultRow): ApiObject =
ApiObject(
tag = row[SampleObjects.tag],
device_date = row[SampleObjects.device_date],
device_date = row[SampleObjects.device_date].toKotlinLocalDateTime(),
device_id = row[SampleObjects.device_id]
)
}