diff --git a/src/main/kotlin/service/TermService.kt b/src/main/kotlin/service/TermService.kt index a8a0bef..85104ef 100644 --- a/src/main/kotlin/service/TermService.kt +++ b/src/main/kotlin/service/TermService.kt @@ -6,12 +6,19 @@ import models.TermFull import models.Pronunciation import models.Translation +enum class SearchType { + EXACT, + START, + END, + FULL +} + interface TermService { fun getTerm(id: Int): Term? fun getFullTerm(id: Int): TermFull? fun getAllTerm(): List fun getPronunciationsForTerm(id: Int): List? - fun getTranslationForTerm(term: String, trans: Translation): List? + fun getTranslationForTerm(term: String, trans: Translation, type: SearchType = SearchType.EXACT): List? fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List? } diff --git a/src/main/kotlin/service/TermServiceImpl.kt b/src/main/kotlin/service/TermServiceImpl.kt index 5058ad7..6e86b15 100644 --- a/src/main/kotlin/service/TermServiceImpl.kt +++ b/src/main/kotlin/service/TermServiceImpl.kt @@ -9,6 +9,7 @@ import models.TermFull import models.Pronunciation import models.Translation import tables.* +import service.SearchType class TermServiceImpl : TermService { override fun getTerm(id: Int): Term? = dbExecId(1) { @@ -27,16 +28,32 @@ class TermServiceImpl : TermService { TermDao.findById(id)?.toFullModel()?.pronunciations } - override fun getTranslationForTerm(term: String,trans: Translation): List? = - dbExecId(trans.dictionaryId) { + override fun getTranslationForTerm(term: String,trans: Translation, type: SearchType): List? { + val s : String + when (type) { + SearchType.START -> s = term + "%" + SearchType.END -> s = "%" + term + SearchType.FULL -> s = "%" + term + "%" + SearchType.EXACT -> s = term + } + + return dbExecId(trans.dictionaryId) { if (trans.direction == 1) - TermDao.find { Terms.string1 eq term }.map { it.toFullModel() } + if (type == SearchType.EXACT) + TermDao.find { Terms.string1 eq s }.map { it.toFullModel() } + else + TermDao.find { Terms.string1 like s }.map { it.toFullModel() } else - TermDao.find { Terms.string2 eq term }.map { it.toFullModel() } + if (type == SearchType.EXACT) + TermDao.find { Terms.string2 eq s }.map { it.toFullModel() } + else + TermDao.find { Terms.string1 like s }.map { it.toFullModel() } + } + } + + override fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List? = dbExecName(fromLang,toLang) { + TermDao.find { Terms.string1 eq term }.map { it.toFullModel() } } - override fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List? = dbExecName(fromLang,toLang) { - TermDao.find { Terms.string1 eq term }.map { it.toFullModel() } - } } \ No newline at end of file diff --git a/src/main/kotlin/web/TranslationResource.kt b/src/main/kotlin/web/TranslationResource.kt index d86d5fd..0a04d9d 100644 --- a/src/main/kotlin/web/TranslationResource.kt +++ b/src/main/kotlin/web/TranslationResource.kt @@ -11,6 +11,7 @@ import service.TranslationService import service.TermServiceImpl import service.TermService import service.DatabaseFactory.getTranslationForLanguages +import service.SearchType fun Route.translation(TranslationService: TranslationServiceImpl,TermService: TermServiceImpl) { @@ -40,7 +41,20 @@ fun Route.translation(TranslationService: TranslationServiceImpl,TermService: Te if (terms == null) call.respond(HttpStatusCode.NotFound) else call.respond(terms) } + } + get("/{from}/{to}/{term}/all") { + 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,SearchType.START) + if (terms == null) call.respond(HttpStatusCode.NotFound) + else call.respond(terms) + } } }