Modify search for all terms
This commit is contained in:
@@ -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<Term>
|
||||
fun getPronunciationsForTerm(id: Int): List<Pronunciation>?
|
||||
fun getTranslationForTerm(term: String, trans: Translation): List<TermFull>?
|
||||
fun getTranslationForTerm(term: String, trans: Translation, type: SearchType = SearchType.EXACT): List<TermFull>?
|
||||
fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List<TermFull>?
|
||||
|
||||
}
|
||||
|
||||
@@ -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<TermFull>? =
|
||||
dbExecId(trans.dictionaryId) {
|
||||
override fun getTranslationForTerm(term: String,trans: Translation, type: SearchType): List<TermFull>? {
|
||||
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<TermFull>? = dbExecName(fromLang,toLang) {
|
||||
TermDao.find { Terms.string1 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() }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user