From 34621b432cf629d19551244a39b1fe3ffe3f5306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Wed, 20 May 2020 22:49:48 +0200 Subject: [PATCH] refactor to input service skeleton --- src/api/model/ApiObject.kt | 9 +++- src/api/route/InputServiceServer.kt | 51 ++++----------------- src/database/dao/InputObjects.kt | 2 +- src/database/model/InputObject.kt | 2 +- src/database/service/IInputObjectService.kt | 10 +--- src/database/service/InputObjectService.kt | 51 ++++----------------- 6 files changed, 30 insertions(+), 95 deletions(-) diff --git a/src/api/model/ApiObject.kt b/src/api/model/ApiObject.kt index 6fda350..8e23b49 100644 --- a/src/api/model/ApiObject.kt +++ b/src/api/model/ApiObject.kt @@ -1,8 +1,15 @@ package com.kmalbz.api.model import com.google.gson.annotations.SerializedName +import java.time.LocalDate + +data class ResponseObject( + @SerializedName("message") val message: String +) data class ApiObject( @SerializedName("tag") val tag: String, - @SerializedName("probability") val probability: Double + @SerializedName("date") val date: LocalDate, + @SerializedName("device_id") val device_id: String + ) \ No newline at end of file diff --git a/src/api/route/InputServiceServer.kt b/src/api/route/InputServiceServer.kt index 429487e..a31db50 100644 --- a/src/api/route/InputServiceServer.kt +++ b/src/api/route/InputServiceServer.kt @@ -6,64 +6,33 @@ import io.ktor.http.HttpStatusCode import io.ktor.response.respond import io.ktor.routing.Routing import io.ktor.routing.get +import io.ktor.routing.post import org.koin.ktor.ext.inject import java.time.LocalDate import java.time.format.DateTimeFormatter /** - * Output Service - RDB + * Input Service * - * This is the output interface of the Birbnetes system. + * This is the input interface of the Birbnetes system. */ class InputServiceServer { /** - * output + * sample */ fun Routing.registerOutput() { val resultObjectService by inject() - get("/output"){ - call.respond(resultObjectService.getAllResultObjects()) + get("/sample"){ + call.respond(resultObjectService.getAllInputObjects()) } - get("/output/filter/negative") { - val resultList = resultObjectService.getResultObjecLessthanProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) - - call.respond(resultList) + post("/sample"){ + call.respond(resultObjectService.getAllInputObjects()) } - get("/output/filter/positive") { - val resultList = resultObjectService.getResultObjecGreaterthanProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) - - call.respond(resultList) - } - - get("/output/filter/undecided") { - val resultList = resultObjectService.getResultObjecEqualsProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) - - call.respond(resultList) - } - - get("/output/after/{dateAfter}") { - val dateAfter = call.parameters["dateAfter"] ?: error(HttpStatusCode.NotAcceptable) - val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE - val localDateAfter : LocalDate = LocalDate.parse(dateAfter,dateTimeFormatter) - val resultList = resultObjectService.getResultObjectafterDate(localDateAfter) ?: call.respond(HttpStatusCode.NotFound) - - call.respond(resultList) - } - - get("/output/before/{dateBefore}") { - val dateAfter = call.parameters["dateBefore"] ?: error(HttpStatusCode.NotAcceptable) - val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE - val localDateBefore : LocalDate = LocalDate.parse(dateAfter,dateTimeFormatter) - val resultList = resultObjectService.getResultObjectbeforeDate(localDateBefore) ?: call.respond(HttpStatusCode.NotFound) - - call.respond(resultList) - } - - get("/output/{tagID}") { + get("/sample/{tagID}") { val tagID = call.parameters["tagID"] ?: error(HttpStatusCode.NotAcceptable) - val resultObject = resultObjectService.getResultObjectbyTag(tagID) ?: call.respond(HttpStatusCode.NotFound) + val resultObject = resultObjectService.getInputObjectbyTag(tagID) ?: call.respond(HttpStatusCode.NotFound) call.respond(resultObject) } diff --git a/src/database/dao/InputObjects.kt b/src/database/dao/InputObjects.kt index 71dad31..539a653 100644 --- a/src/database/dao/InputObjects.kt +++ b/src/database/dao/InputObjects.kt @@ -8,6 +8,6 @@ import java.time.LocalDate object InputObjects : IntIdTable() { val tag: Column = varchar("tag",32) val date: Column = date("date").default(LocalDate.now()) - val probability: Column = double("probability") + val device_id: Column = varchar("device_id", 32) override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id") } \ No newline at end of file diff --git a/src/database/model/InputObject.kt b/src/database/model/InputObject.kt index 9b9ff36..d85e0d5 100644 --- a/src/database/model/InputObject.kt +++ b/src/database/model/InputObject.kt @@ -9,5 +9,5 @@ class InputObject(id: EntityID): IntEntity(id) { companion object : IntEntityClass(InputObjects) var tag by InputObjects.tag var date by InputObjects.date - var probability by InputObjects.probability + var device_id by InputObjects.device_id } \ No newline at end of file diff --git a/src/database/service/IInputObjectService.kt b/src/database/service/IInputObjectService.kt index c43d8c1..40179e7 100644 --- a/src/database/service/IInputObjectService.kt +++ b/src/database/service/IInputObjectService.kt @@ -5,12 +5,6 @@ import java.time.LocalDate interface IInputObjectService{ fun addOne(apiObject: ApiObject) - suspend fun getAllResultObjects(): List - suspend fun getResultObjectbyTag(tag: String): ApiObject? - suspend fun getResultObjectbyDate(date: LocalDate): List? - suspend fun getResultObjectbeforeDate(date: LocalDate): List? - suspend fun getResultObjectafterDate(date: LocalDate): List? - suspend fun getResultObjecGreaterthanProbability(probability: Double): List? - suspend fun getResultObjecLessthanProbability(probability: Double): List? - suspend fun getResultObjecEqualsProbability(probability: Double): List? + suspend fun getAllInputObjects(): List + suspend fun getInputObjectbyTag(tag: String): ApiObject? } \ No newline at end of file diff --git a/src/database/service/InputObjectService.kt b/src/database/service/InputObjectService.kt index 5719c9a..e54d76d 100644 --- a/src/database/service/InputObjectService.kt +++ b/src/database/service/InputObjectService.kt @@ -17,61 +17,26 @@ class InputObjectService : IInputObjectService { transaction { InputObject.new { tag = apiObject.tag - probability = apiObject.probability + device_id = apiObject.device_id } } } - override suspend fun getAllResultObjects(): List = dbQuery { - InputObjects.selectAll().map { toResultObject(it) } + override suspend fun getAllInputObjects(): List = dbQuery { + InputObjects.selectAll().map { toInputObject(it) } } - override suspend fun getResultObjectbyTag(tag: String): ApiObject? = dbQuery { + override suspend fun getInputObjectbyTag(tag: String): ApiObject? = dbQuery { InputObjects.select { (InputObjects.tag eq tag) - }.mapNotNull { toResultObject(it) } + }.mapNotNull { toInputObject(it) } .singleOrNull() } - override suspend fun getResultObjectbyDate(date: LocalDate): List? = dbQuery { - InputObjects.select { - (InputObjects.date eq date) - }.mapNotNull { toResultObject(it) } - } - - override suspend fun getResultObjectbeforeDate(date: LocalDate): List? = dbQuery { - InputObjects.select { - (InputObjects.date less date) - }.mapNotNull { toResultObject(it) } - } - - override suspend fun getResultObjectafterDate(date: LocalDate): List? = dbQuery { - InputObjects.select { - (InputObjects.date greater date) - }.mapNotNull { toResultObject(it) } - } - - override suspend fun getResultObjecGreaterthanProbability(probability: Double): List? = dbQuery { - InputObjects.select { - (InputObjects.probability greater probability) - }.mapNotNull { toResultObject(it) } - } - - override suspend fun getResultObjecLessthanProbability(probability: Double): List? = dbQuery { - InputObjects.select { - (InputObjects.probability less probability) - }.mapNotNull { toResultObject(it) } - } - - override suspend fun getResultObjecEqualsProbability(probability: Double): List? = dbQuery { - InputObjects.select { - (InputObjects.probability eq probability) - }.mapNotNull { toResultObject(it) } - } - - private fun toResultObject(row: ResultRow): ApiObject = + private fun toInputObject(row: ResultRow): ApiObject = ApiObject( tag = row[InputObjects.tag], - probability = row[InputObjects.probability] + date = row[InputObjects.date], + device_id = row[InputObjects.device_id] ) } \ No newline at end of file