81 lines
2.8 KiB
Kotlin
81 lines
2.8 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 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
|
|
import java.util.*
|
|
|
|
|
|
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 { toResultObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectbyTag(tag: String): ApiObject? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.tag eq tag)
|
|
}.mapNotNull { toResultObject(it) }
|
|
.singleOrNull()
|
|
}
|
|
|
|
override suspend fun getSampleObjectbyDate(date: LocalDate): List<ApiObject>? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.date eq date)
|
|
}.mapNotNull { toResultObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectbeforeDate(date: LocalDate): List<ApiObject>? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.date less date)
|
|
}.mapNotNull { toResultObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectafterDate(date: LocalDate): List<ApiObject>? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.date greater date)
|
|
}.mapNotNull { toResultObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjecGreaterthanId(probability: Double): List<ApiObject>? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.probability greater probability)
|
|
}.mapNotNull { toResultObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjecLessthanId(probability: Double): List<ApiObject>? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.probability less probability)
|
|
}.mapNotNull { toResultObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjecEqualsId(probability: Double): List<ApiObject>? = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.probability eq probability)
|
|
}.mapNotNull { toResultObject(it) }
|
|
}
|
|
|
|
private fun toResultObject(row: ResultRow): ApiObject =
|
|
ApiObject(
|
|
tag = row[SampleObjects.tag],
|
|
device_date = row[SampleObjects.device_date],
|
|
device_id = row[SampleObjects.device_id],
|
|
id = row[SampleObjects.id.]
|
|
)
|
|
} |