79 lines
2.7 KiB
Kotlin
79 lines
2.7 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 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
|
|
|
|
|
|
@KtorExperimentalAPI
|
|
class SampleObjectService : ISampleObjectService {
|
|
|
|
override fun addOne(apiObject: ApiObject) {
|
|
transaction {
|
|
SampleObject.new {
|
|
tag = apiObject.tag
|
|
device_date = apiObject.device_date.toJavaLocalDateTime()
|
|
device_id = apiObject.device_id
|
|
}
|
|
}
|
|
}
|
|
|
|
override suspend fun getAllSampleObjects(): List<ApiObject> = dbQuery {
|
|
SampleObjects.selectAll().map { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getCount(): Long = dbQuery {
|
|
SampleObjects.selectAll().count()
|
|
}
|
|
|
|
override suspend fun getPage(page: Long): List<ApiObject> = dbQuery {
|
|
SampleObjects.selectAll().limit(10, page * 10).map { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectbyDeviceID(device_id: Int): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.device_id eq device_id)
|
|
}.mapNotNull { 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: LocalDateTime): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.timestamp eq date.toJavaLocalDateTime())
|
|
}.mapNotNull { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectbeforeDate(date: LocalDateTime): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(SampleObjects.timestamp less date.toJavaLocalDateTime())
|
|
}.mapNotNull { toSampleObject(it) }
|
|
}
|
|
|
|
override suspend fun getSampleObjectafterDate(date: LocalDateTime): List<ApiObject> = dbQuery {
|
|
SampleObjects.select {
|
|
(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].toKotlinLocalDateTime(),
|
|
device_id = row[SampleObjects.device_id]
|
|
)
|
|
} |