refactor to input service skeleton
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-05-20 22:49:48 +02:00
parent 9fd976a298
commit 34621b432c
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
6 changed files with 30 additions and 95 deletions

View File

@ -1,8 +1,15 @@
package com.kmalbz.api.model package com.kmalbz.api.model
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName
import java.time.LocalDate
data class ResponseObject(
@SerializedName("message") val message: String
)
data class ApiObject( data class ApiObject(
@SerializedName("tag") val tag: String, @SerializedName("tag") val tag: String,
@SerializedName("probability") val probability: Double @SerializedName("date") val date: LocalDate,
@SerializedName("device_id") val device_id: String
) )

View File

@ -6,64 +6,33 @@ import io.ktor.http.HttpStatusCode
import io.ktor.response.respond import io.ktor.response.respond
import io.ktor.routing.Routing import io.ktor.routing.Routing
import io.ktor.routing.get import io.ktor.routing.get
import io.ktor.routing.post
import org.koin.ktor.ext.inject import org.koin.ktor.ext.inject
import java.time.LocalDate import java.time.LocalDate
import java.time.format.DateTimeFormatter 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 { class InputServiceServer {
/** /**
* output * sample
*/ */
fun Routing.registerOutput() { fun Routing.registerOutput() {
val resultObjectService by inject<IInputObjectService>() val resultObjectService by inject<IInputObjectService>()
get("/output"){ get("/sample"){
call.respond(resultObjectService.getAllResultObjects()) call.respond(resultObjectService.getAllInputObjects())
} }
get("/output/filter/negative") { post("/sample"){
val resultList = resultObjectService.getResultObjecLessthanProbability(0.5) ?: call.respond(HttpStatusCode.NotFound) call.respond(resultObjectService.getAllInputObjects())
call.respond(resultList)
} }
get("/output/filter/positive") { get("/sample/{tagID}") {
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}") {
val tagID = call.parameters["tagID"] ?: error(HttpStatusCode.NotAcceptable) 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) call.respond(resultObject)
} }

View File

@ -8,6 +8,6 @@ import java.time.LocalDate
object InputObjects : IntIdTable() { object InputObjects : IntIdTable() {
val tag: Column<String> = varchar("tag",32) val tag: Column<String> = varchar("tag",32)
val date: Column<LocalDate> = date("date").default(LocalDate.now()) val date: Column<LocalDate> = date("date").default(LocalDate.now())
val probability: Column<Double> = double("probability") val device_id: Column<String> = varchar("device_id", 32)
override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id") override val primaryKey = PrimaryKey(id, name = "PK_ResultObject_Id")
} }

View File

@ -9,5 +9,5 @@ class InputObject(id: EntityID<Int>): IntEntity(id) {
companion object : IntEntityClass<InputObject>(InputObjects) companion object : IntEntityClass<InputObject>(InputObjects)
var tag by InputObjects.tag var tag by InputObjects.tag
var date by InputObjects.date var date by InputObjects.date
var probability by InputObjects.probability var device_id by InputObjects.device_id
} }

View File

@ -5,12 +5,6 @@ import java.time.LocalDate
interface IInputObjectService{ interface IInputObjectService{
fun addOne(apiObject: ApiObject) fun addOne(apiObject: ApiObject)
suspend fun getAllResultObjects(): List<ApiObject> suspend fun getAllInputObjects(): List<ApiObject>
suspend fun getResultObjectbyTag(tag: String): ApiObject? suspend fun getInputObjectbyTag(tag: String): ApiObject?
suspend fun getResultObjectbyDate(date: LocalDate): List<ApiObject>?
suspend fun getResultObjectbeforeDate(date: LocalDate): List<ApiObject>?
suspend fun getResultObjectafterDate(date: LocalDate): List<ApiObject>?
suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>?
suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>?
suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>?
} }

View File

@ -17,61 +17,26 @@ class InputObjectService : IInputObjectService {
transaction { transaction {
InputObject.new { InputObject.new {
tag = apiObject.tag tag = apiObject.tag
probability = apiObject.probability device_id = apiObject.device_id
} }
} }
} }
override suspend fun getAllResultObjects(): List<ApiObject> = dbQuery { override suspend fun getAllInputObjects(): List<ApiObject> = dbQuery {
InputObjects.selectAll().map { toResultObject(it) } InputObjects.selectAll().map { toInputObject(it) }
} }
override suspend fun getResultObjectbyTag(tag: String): ApiObject? = dbQuery { override suspend fun getInputObjectbyTag(tag: String): ApiObject? = dbQuery {
InputObjects.select { InputObjects.select {
(InputObjects.tag eq tag) (InputObjects.tag eq tag)
}.mapNotNull { toResultObject(it) } }.mapNotNull { toInputObject(it) }
.singleOrNull() .singleOrNull()
} }
override suspend fun getResultObjectbyDate(date: LocalDate): List<ApiObject>? = dbQuery { private fun toInputObject(row: ResultRow): ApiObject =
InputObjects.select {
(InputObjects.date eq date)
}.mapNotNull { toResultObject(it) }
}
override suspend fun getResultObjectbeforeDate(date: LocalDate): List<ApiObject>? = dbQuery {
InputObjects.select {
(InputObjects.date less date)
}.mapNotNull { toResultObject(it) }
}
override suspend fun getResultObjectafterDate(date: LocalDate): List<ApiObject>? = dbQuery {
InputObjects.select {
(InputObjects.date greater date)
}.mapNotNull { toResultObject(it) }
}
override suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>? = dbQuery {
InputObjects.select {
(InputObjects.probability greater probability)
}.mapNotNull { toResultObject(it) }
}
override suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>? = dbQuery {
InputObjects.select {
(InputObjects.probability less probability)
}.mapNotNull { toResultObject(it) }
}
override suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>? = dbQuery {
InputObjects.select {
(InputObjects.probability eq probability)
}.mapNotNull { toResultObject(it) }
}
private fun toResultObject(row: ResultRow): ApiObject =
ApiObject( ApiObject(
tag = row[InputObjects.tag], tag = row[InputObjects.tag],
probability = row[InputObjects.probability] date = row[InputObjects.date],
device_id = row[InputObjects.device_id]
) )
} }