From d4a2d9925cd0cf0aea890bedb3c4980a179e8ed5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Wed, 20 May 2020 17:06:15 +0200 Subject: [PATCH] use fancy di --- .gitignore | 1 + build.gradle | 3 +++ gradle.properties | 1 + src/Application.kt | 14 +++++--------- src/api/route/OutputServiceRDBServer.kt | 14 ++++++++------ src/database/service/IResultObjectService.kt | 16 ++++++++++++++++ src/database/service/ResultObjectService.kt | 20 ++++++++++---------- src/di/InjectionModule.kt | 10 ++++++++++ 8 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/database/service/IResultObjectService.kt create mode 100644 src/di/InjectionModule.kt diff --git a/.gitignore b/.gitignore index fae6b74..9affc3d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ *.iml *.ipr *.iws +*.log \ No newline at end of file diff --git a/build.gradle b/build.gradle index 52d9df5..20e1136 100644 --- a/build.gradle +++ b/build.gradle @@ -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" diff --git a/gradle.properties b/gradle.properties index 1fafb72..a6f0c66 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 \ No newline at end of file diff --git a/src/Application.kt b/src/Application.kt index 23a9ee3..3a63b86 100644 --- a/src/Application.kt +++ b/src/Application.kt @@ -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): 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 { - call.respond(HttpStatusCode.Unauthorized) - } - - exception { - call.respond(HttpStatusCode.Forbidden) - } - exception { call.respond(HttpStatusCode.BadRequest) } diff --git a/src/api/route/OutputServiceRDBServer.kt b/src/api/route/OutputServiceRDBServer.kt index 1fefd54..ff756bd 100644 --- a/src/api/route/OutputServiceRDBServer.kt +++ b/src/api/route/OutputServiceRDBServer.kt @@ -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() get("/output"){ call.respond(resultObjectService.getAllResultObjects()) } diff --git a/src/database/service/IResultObjectService.kt b/src/database/service/IResultObjectService.kt new file mode 100644 index 0000000..d612f78 --- /dev/null +++ b/src/database/service/IResultObjectService.kt @@ -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 + 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? +} \ No newline at end of file diff --git a/src/database/service/ResultObjectService.kt b/src/database/service/ResultObjectService.kt index b328992..6765e67 100644 --- a/src/database/service/ResultObjectService.kt +++ b/src/database/service/ResultObjectService.kt @@ -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 = dbQuery { + override suspend fun getAllResultObjects(): List = 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? = dbQuery { + override suspend fun getResultObjectbyDate(date: LocalDate): List? = dbQuery { ResultObjects.select { (ResultObjects.date eq date) }.mapNotNull { toResultObject(it) } } - suspend fun getResultObjectbeforeDate(date: LocalDate): List? = dbQuery { + override suspend fun getResultObjectbeforeDate(date: LocalDate): List? = dbQuery { ResultObjects.select { (ResultObjects.date less date) }.mapNotNull { toResultObject(it) } } - suspend fun getResultObjectafterDate(date: LocalDate): List? = dbQuery { + override suspend fun getResultObjectafterDate(date: LocalDate): List? = dbQuery { ResultObjects.select { (ResultObjects.date greater date) }.mapNotNull { toResultObject(it) } } - suspend fun getResultObjecGreaterthanProbability(probability: Double): List? = dbQuery { + override suspend fun getResultObjecGreaterthanProbability(probability: Double): List? = dbQuery { ResultObjects.select { (ResultObjects.probability greater probability) }.mapNotNull { toResultObject(it) } } - suspend fun getResultObjecLessthanProbability(probability: Double): List? = dbQuery { + override suspend fun getResultObjecLessthanProbability(probability: Double): List? = dbQuery { ResultObjects.select { (ResultObjects.probability less probability) }.mapNotNull { toResultObject(it) } } - suspend fun getResultObjecEqualsProbability(probability: Double): List? = dbQuery { + override suspend fun getResultObjecEqualsProbability(probability: Double): List? = dbQuery { ResultObjects.select { (ResultObjects.probability eq probability) }.mapNotNull { toResultObject(it) } diff --git a/src/di/InjectionModule.kt b/src/di/InjectionModule.kt new file mode 100644 index 0000000..ee955de --- /dev/null +++ b/src/di/InjectionModule.kt @@ -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() +}