63 lines
2.0 KiB
Kotlin
63 lines
2.0 KiB
Kotlin
package com.kmalbz.database.service
|
|
|
|
import com.kmalbz.database.DatabaseFactory.dbQuery
|
|
import com.kmalbz.database.model.SampleObject
|
|
import com.kmalbz.database.dao.SampleObjects
|
|
import com.kmalbz.api.model.ApiObject
|
|
import io.ktor.util.*
|
|
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
|
|
class SampleObjectService : ISampleObjectService {
|
|
|
|
override fun addOne(apiObject: ApiObject) {
|
|
transaction {
|
|
SampleObject.new {
|
|
tag = apiObject.tag
|
|
device_date = apiObject.device_date
|
|
device_id = apiObject.device_id
|
|
}
|
|
}
|
|
}
|
|
|
|
override suspend fun getAllSampleObjects(): List<ApiObject> = dbQuery {
|
|
SampleObjects.selectAll().map { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectbyTag(tag: String): ApiObject? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.tag eq tag)
|
|
}.mapNotNull { toSampleObject(it) }
|
|
.singleOrNull()
|
|
}
|
|
|
|
override suspend fun getSampleObjectbyDate(date: LocalDate): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.timestamp eq date)
|
|
}.mapNotNull { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectbeforeDate(date: LocalDate): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.timestamp less date)
|
|
}.mapNotNull { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectafterDate(date: LocalDate): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.timestamp greater date)
|
|
}.mapNotNull { toSampleObject(it) }
|
|
}
|
|
|
|
private fun toSampleObject(row: ResultRow): ApiObject =
|
|
ApiObject(
|
|
tag = row[SampleObjects.tag],
|
|
device_date = row[SampleObjects.device_date],
|
|
device_id = row[SampleObjects.device_id]
|
|
)
|
|
} |