refactor to input service skeleton
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
9fd976a298
commit
34621b432c
@ -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
|
||||
|
||||
)
|
@ -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<IInputObjectService>()
|
||||
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)
|
||||
}
|
||||
|
@ -8,6 +8,6 @@ import java.time.LocalDate
|
||||
object InputObjects : IntIdTable() {
|
||||
val tag: Column<String> = varchar("tag",32)
|
||||
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")
|
||||
}
|
@ -9,5 +9,5 @@ class InputObject(id: EntityID<Int>): IntEntity(id) {
|
||||
companion object : IntEntityClass<InputObject>(InputObjects)
|
||||
var tag by InputObjects.tag
|
||||
var date by InputObjects.date
|
||||
var probability by InputObjects.probability
|
||||
var device_id by InputObjects.device_id
|
||||
}
|
@ -5,12 +5,6 @@ import java.time.LocalDate
|
||||
|
||||
interface IInputObjectService{
|
||||
fun addOne(apiObject: ApiObject)
|
||||
suspend fun getAllResultObjects(): List<ApiObject>
|
||||
suspend fun getResultObjectbyTag(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>?
|
||||
suspend fun getAllInputObjects(): List<ApiObject>
|
||||
suspend fun getInputObjectbyTag(tag: String): ApiObject?
|
||||
}
|
@ -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<ApiObject> = dbQuery {
|
||||
InputObjects.selectAll().map { toResultObject(it) }
|
||||
override suspend fun getAllInputObjects(): List<ApiObject> = 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<ApiObject>? = dbQuery {
|
||||
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 =
|
||||
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]
|
||||
)
|
||||
}
|
Loading…
Reference in New Issue
Block a user