Compare commits

..

No commits in common. "02793fa80637031bf4ebee9309858a112d139203" and "b67cc939b62761fa563aac3fd3a77ad15255dc61" have entirely different histories.

6 changed files with 27 additions and 36 deletions

View File

@ -7,21 +7,15 @@ 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'
@ -53,15 +47,13 @@ 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.SampleObjects import com.kmalbz.database.dao.SampleObjects
import io.ktor.serialization.*
import io.ktor.util.* import io.ktor.util.*
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,7 +21,8 @@ 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) {
json() gson {
}
} }
install(Koin) { install(Koin) {

View File

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

View File

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

View File

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

View File

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