Basic funcionality of Ktor
This commit is contained in:
@@ -59,7 +59,7 @@ fun Application.module() {
|
||||
dictionary(dictionaryService)
|
||||
pronunciation(pronunciationService)
|
||||
language(languageService)
|
||||
translation(translationService)
|
||||
translation(translationService,termService)
|
||||
term(termService)
|
||||
}
|
||||
|
||||
|
||||
@@ -13,12 +13,14 @@ import javax.sql.DataSource
|
||||
import tables.*
|
||||
import dao.*
|
||||
import com.typesafe.config.ConfigFactory
|
||||
import models.Translation
|
||||
|
||||
object DatabaseFactory {
|
||||
|
||||
private val log = LoggerFactory.getLogger(this::class.java)
|
||||
private val dbs : MutableMap<Int,Database> = mutableMapOf()
|
||||
private val hdb : MutableMap<String,Database> = mutableMapOf()
|
||||
private val htrans: MutableMap<String,Translation> = mutableMapOf()
|
||||
|
||||
fun connectAndMigrate() {
|
||||
log.info("Initialising database")
|
||||
@@ -41,9 +43,18 @@ object DatabaseFactory {
|
||||
return HikariDataSource(config)
|
||||
}
|
||||
|
||||
fun getTranslationForLanguages(l1: String, l2: String): Translation? = htrans["${l1}${l2}"]?: null
|
||||
|
||||
fun connectAll(): Map<Int,Database> {
|
||||
val hMap : MutableMap<Int, Database> = mutableMapOf()
|
||||
transaction {
|
||||
for (trans in TranslationDao.all()) {
|
||||
val l1 = trans.lang1.shortName.lowercase()
|
||||
val l2 = trans.lang2.shortName.lowercase()
|
||||
|
||||
htrans["${l1}${l2}"] = trans.toModel()
|
||||
}
|
||||
|
||||
for (dict in DictionaryDao.all()) {
|
||||
val cfg = ConfigFactory.load().getConfig("h2")
|
||||
val config = HikariConfig().apply {
|
||||
|
||||
@@ -4,10 +4,14 @@ import dao.TermDao
|
||||
import models.Term
|
||||
import models.TermFull
|
||||
import models.Pronunciation
|
||||
import models.Translation
|
||||
|
||||
interface TermService {
|
||||
fun getTerm(id: Int): Term?
|
||||
fun getFullTerm(id: Int): TermFull?
|
||||
fun getAllTerm(): List<Term>
|
||||
fun getPronunciationsForTerm(id: Int): List<Pronunciation>?
|
||||
fun getTranslationForTerm(term: String, trans: Translation): List<TermFull>?
|
||||
fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List<TermFull>?
|
||||
|
||||
}
|
||||
|
||||
@@ -3,9 +3,12 @@ package service
|
||||
import dao.TermDao
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import service.DatabaseFactory.dbExecId
|
||||
import service.DatabaseFactory.dbExecName
|
||||
import models.Term
|
||||
import models.TermFull
|
||||
import models.Pronunciation
|
||||
import models.Translation
|
||||
import tables.*
|
||||
|
||||
class TermServiceImpl : TermService {
|
||||
override fun getTerm(id: Int): Term? = dbExecId(1) {
|
||||
@@ -21,6 +24,19 @@ class TermServiceImpl : TermService {
|
||||
}
|
||||
|
||||
override fun getPronunciationsForTerm(id: Int): List<Pronunciation>? = dbExecId(1) {
|
||||
null
|
||||
TermDao.findById(id)?.toFullModel()?.pronunciations
|
||||
}
|
||||
|
||||
override fun getTranslationForTerm(term: String,trans: Translation): List<TermFull>? =
|
||||
dbExecId(trans.dictionaryId) {
|
||||
if (trans.direction == 1)
|
||||
TermDao.find { Terms.string1 eq term }.map { it.toFullModel() }
|
||||
else
|
||||
TermDao.find { Terms.string2 eq term }.map { it.toFullModel() }
|
||||
}
|
||||
|
||||
override fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List<TermFull>? = dbExecName(fromLang,toLang) {
|
||||
TermDao.find { Terms.string1 eq term }.map { it.toFullModel() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,5 +5,6 @@ import models.Translation
|
||||
|
||||
interface TranslationService {
|
||||
fun getTranslation(id: Int): Translation?
|
||||
fun getTranslationFromTo(from: String, to: String): Translation?
|
||||
fun getAllTranslations(): List<Translation>
|
||||
}
|
||||
|
||||
@@ -1,17 +1,24 @@
|
||||
package service
|
||||
|
||||
import dao.TranslationDao
|
||||
import dao.LanguageDao
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import service.DatabaseFactory.dbExecId
|
||||
import service.DatabaseFactory.getTranslationForLanguages
|
||||
import models.Translation
|
||||
import tables.Translations
|
||||
import tables.Languages
|
||||
|
||||
class TranslationServiceImpl : TranslationService {
|
||||
override fun getTranslation(id: Int): Translation? = dbExecId(1) {
|
||||
override fun getTranslation(id: Int): Translation? = dbExecId(0) {
|
||||
TranslationDao.findById(id)?.toModel()
|
||||
}
|
||||
|
||||
override fun getAllTranslations(): List<Translation> = dbExecId(1) {
|
||||
override fun getAllTranslations(): List<Translation> = dbExecId(0) {
|
||||
TranslationDao.all().map { it.toModel() }
|
||||
}
|
||||
|
||||
override fun getTranslationFromTo(from: String, to: String): Translation? = dbExecId(0) {
|
||||
getTranslationForLanguages(from,to)
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,12 @@ import io.ktor.server.websocket.*
|
||||
import io.ktor.websocket.*
|
||||
import service.TranslationServiceImpl
|
||||
import service.TranslationService
|
||||
import service.TermServiceImpl
|
||||
import service.TermService
|
||||
import service.DatabaseFactory.getTranslationForLanguages
|
||||
|
||||
fun Route.translation(TranslationService: TranslationServiceImpl) {
|
||||
|
||||
fun Route.translation(TranslationService: TranslationServiceImpl,TermService: TermServiceImpl) {
|
||||
|
||||
route("/translation") {
|
||||
|
||||
@@ -24,6 +28,20 @@ fun Route.translation(TranslationService: TranslationServiceImpl) {
|
||||
else call.respond(Translation)
|
||||
}
|
||||
|
||||
get("/{from}/{to}/{term}") {
|
||||
val from = call.parameters["from"]?: throw IllegalStateException("Must provide from")
|
||||
val to = call.parameters["to"]?: throw IllegalStateException("Must provide to")
|
||||
val term = call.parameters["term"]?: throw IllegalStateException("Must provide term")
|
||||
|
||||
val translation = getTranslationForLanguages(from,to)
|
||||
if (translation == null) call.respond(HttpStatusCode.NotFound)
|
||||
else {
|
||||
val terms = TermService.getTranslationForTerm(term,translation)
|
||||
if (terms == null) call.respond(HttpStatusCode.NotFound)
|
||||
else call.respond(terms)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user