use fancy di
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Torma Kristóf 2020-05-20 17:06:15 +02:00
parent d8e0819f00
commit d4a2d9925c
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
8 changed files with 54 additions and 25 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
*.iml
*.ipr
*.iws
*.log

View File

@ -7,6 +7,7 @@ buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.jengelman.gradle.plugins:shadow:5.2.0"
classpath "org.koin:koin-gradle-plugin:$koin_version"
}
}
@ -18,6 +19,7 @@ tasks.withType(JavaCompile) {
apply plugin: 'kotlin'
apply plugin: "com.github.johnrengelman.shadow"
apply plugin: 'application'
apply plugin: 'koin'
group 'com.kmalbz'
version '0.0.1'
@ -45,6 +47,7 @@ dependencies {
compile 'com.rabbitmq:amqp-client:2.7.1'
compile 'com.zaxxer:HikariCP:2.7.8'
compile 'com.viartemev:the-white-rabbit:0.0.5'
implementation "org.koin:koin-ktor:$koin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "io.ktor:ktor-server-netty:$ktor_version"
implementation "ch.qos.logback:logback-classic:$logback_version"

View File

@ -2,3 +2,4 @@ ktor_version=1.3.2
kotlin.code.style=official
kotlin_version=1.3.72
logback_version=1.2.1
koin_version=2.1.5

View File

@ -23,6 +23,7 @@ import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
import org.koin.ktor.ext.Koin
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@ -33,13 +34,16 @@ fun Application.module() {
gson {
}
}
install(Koin) {
printLogger()
modules(com.kmalbz.di.injectionModule)
}
DatabaseFactory.init()
transaction{
SchemaUtils.create(ResultObjects)
}
val appConfig = HoconApplicationConfig(ConfigFactory.load())
val factory = ConnectionFactory()
factory.host = appConfig.property("ktor.mq.host").getString()
@ -72,14 +76,6 @@ fun Application.module() {
routing {
install(StatusPages) {
exception<AuthenticationException> {
call.respond(HttpStatusCode.Unauthorized)
}
exception<AuthorizationException> {
call.respond(HttpStatusCode.Forbidden)
}
exception<HttpException> {
call.respond(HttpStatusCode.BadRequest)
}

View File

@ -1,10 +1,12 @@
package com.kmalbz.api.route
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.http.*
import com.kmalbz.database.service.ResultObjectService
import com.kmalbz.database.service.IResultObjectService
import io.ktor.application.call
import io.ktor.http.HttpStatusCode
import io.ktor.response.respond
import io.ktor.routing.Routing
import io.ktor.routing.get
import org.koin.ktor.ext.inject
import java.time.LocalDate
import java.time.format.DateTimeFormatter
@ -17,8 +19,8 @@ class OutputServiceRDBServer {
/**
* output
*/
private val resultObjectService = ResultObjectService()
fun Routing.registerOutput() {
val resultObjectService by inject<IResultObjectService>()
get("/output"){
call.respond(resultObjectService.getAllResultObjects())
}

View File

@ -0,0 +1,16 @@
package com.kmalbz.database.service
import com.kmalbz.api.model.ApiObject
import java.time.LocalDate
interface IResultObjectService{
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>?
}

View File

@ -11,9 +11,9 @@ import org.jetbrains.exposed.sql.transactions.transaction
import java.time.LocalDate
class ResultObjectService {
class ResultObjectService : IResultObjectService {
fun addOne(apiObject: ApiObject) {
override fun addOne(apiObject: ApiObject) {
transaction {
ResultObject.new {
tag = apiObject.tag
@ -22,48 +22,48 @@ class ResultObjectService {
}
}
suspend fun getAllResultObjects(): List<ApiObject> = dbQuery {
override suspend fun getAllResultObjects(): List<ApiObject> = dbQuery {
ResultObjects.selectAll().map { toResultObject(it) }
}
suspend fun getResultObjectbyTag(tag: String): ApiObject? = dbQuery {
override suspend fun getResultObjectbyTag(tag: String): ApiObject? = dbQuery {
ResultObjects.select {
(ResultObjects.tag eq tag)
}.mapNotNull { toResultObject(it) }
.singleOrNull()
}
suspend fun getResultObjectbyDate(date: LocalDate): List<ApiObject>? = dbQuery {
override suspend fun getResultObjectbyDate(date: LocalDate): List<ApiObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.date eq date)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjectbeforeDate(date: LocalDate): List<ApiObject>? = dbQuery {
override suspend fun getResultObjectbeforeDate(date: LocalDate): List<ApiObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.date less date)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjectafterDate(date: LocalDate): List<ApiObject>? = dbQuery {
override suspend fun getResultObjectafterDate(date: LocalDate): List<ApiObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.date greater date)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>? = dbQuery {
override suspend fun getResultObjecGreaterthanProbability(probability: Double): List<ApiObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.probability greater probability)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>? = dbQuery {
override suspend fun getResultObjecLessthanProbability(probability: Double): List<ApiObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.probability less probability)
}.mapNotNull { toResultObject(it) }
}
suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>? = dbQuery {
override suspend fun getResultObjecEqualsProbability(probability: Double): List<ApiObject>? = dbQuery {
ResultObjects.select {
(ResultObjects.probability eq probability)
}.mapNotNull { toResultObject(it) }

10
src/di/InjectionModule.kt Normal file
View File

@ -0,0 +1,10 @@
package com.kmalbz.di
import com.kmalbz.database.service.IResultObjectService
import com.kmalbz.database.service.ResultObjectService
import org.koin.dsl.module
import org.koin.experimental.builder.singleBy
val injectionModule = module(createdAtStart = true) {
singleBy<IResultObjectService,ResultObjectService>()
}