refactor sample
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2021-08-13 14:26:09 +02:00
parent 1f0f831742
commit b16a5702b4
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047

View File

@ -6,7 +6,6 @@ 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 org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.castTo
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
@ -27,35 +26,35 @@ class SampleObjectService : ISampleObjectService {
} }
override suspend fun getAllSampleObjects(): List<ApiObject> = dbQuery { override suspend fun getAllSampleObjects(): List<ApiObject> = dbQuery {
SampleObjects.selectAll().map { toResultObject(it) } SampleObjects.selectAll().map { toSampleObject(it) }
} }
override suspend fun getSampleObjectbyTag(tag: String): ApiObject? = dbQuery { override suspend fun getSampleObjectbyTag(tag: String): ApiObject? = dbQuery {
SampleObjects.select { SampleObjects.select {
(SampleObjects.tag eq tag) (SampleObjects.tag eq tag)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toSampleObject(it) }
.singleOrNull() .singleOrNull()
} }
override suspend fun getSampleObjectbyDate(date: LocalDate): List<ApiObject> = dbQuery { override suspend fun getSampleObjectbyDate(date: LocalDate): List<ApiObject> = dbQuery {
SampleObjects.select { SampleObjects.select {
(SampleObjects.timestamp eq date) (SampleObjects.timestamp eq date)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toSampleObject(it) }
} }
override suspend fun getSampleObjectbeforeDate(date: LocalDate): List<ApiObject> = dbQuery { override suspend fun getSampleObjectbeforeDate(date: LocalDate): List<ApiObject> = dbQuery {
SampleObjects.select { SampleObjects.select {
(SampleObjects.timestamp less date) (SampleObjects.timestamp less date)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toSampleObject(it) }
} }
override suspend fun getSampleObjectafterDate(date: LocalDate): List<ApiObject> = dbQuery { override suspend fun getSampleObjectafterDate(date: LocalDate): List<ApiObject> = dbQuery {
SampleObjects.select { SampleObjects.select {
(SampleObjects.timestamp greater date) (SampleObjects.timestamp greater date)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toSampleObject(it) }
} }
private fun toResultObject(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], device_date = row[SampleObjects.device_date],