Modify search for all terms

This commit is contained in:
2025-01-14 08:36:24 +01:00
parent c1e8f9654f
commit 6af39026b0
3 changed files with 46 additions and 8 deletions

View File

@@ -6,12 +6,19 @@ import models.TermFull
import models.Pronunciation import models.Pronunciation
import models.Translation import models.Translation
enum class SearchType {
EXACT,
START,
END,
FULL
}
interface TermService { interface TermService {
fun getTerm(id: Int): Term? fun getTerm(id: Int): Term?
fun getFullTerm(id: Int): TermFull? fun getFullTerm(id: Int): TermFull?
fun getAllTerm(): List<Term> fun getAllTerm(): List<Term>
fun getPronunciationsForTerm(id: Int): List<Pronunciation>? 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>? fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List<TermFull>?
} }

View File

@@ -9,6 +9,7 @@ import models.TermFull
import models.Pronunciation import models.Pronunciation
import models.Translation import models.Translation
import tables.* import tables.*
import service.SearchType
class TermServiceImpl : TermService { class TermServiceImpl : TermService {
override fun getTerm(id: Int): Term? = dbExecId(1) { override fun getTerm(id: Int): Term? = dbExecId(1) {
@@ -27,16 +28,32 @@ class TermServiceImpl : TermService {
TermDao.findById(id)?.toFullModel()?.pronunciations TermDao.findById(id)?.toFullModel()?.pronunciations
} }
override fun getTranslationForTerm(term: String,trans: Translation): List<TermFull>? = override fun getTranslationForTerm(term: String,trans: Translation, type: SearchType): List<TermFull>? {
dbExecId(trans.dictionaryId) { 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) 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 else
TermDao.find { Terms.string2 eq term }.map { it.toFullModel() } TermDao.find { Terms.string1 like s }.map { it.toFullModel() }
else
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) { override fun getTranslationForTerm(term: String, fromLang: String, toLang: String): List<TermFull>? = dbExecName(fromLang,toLang) {
TermDao.find { Terms.string1 eq term }.map { it.toFullModel() } TermDao.find { Terms.string1 eq term }.map { it.toFullModel() }
} }
} }

View File

@@ -11,6 +11,7 @@ import service.TranslationService
import service.TermServiceImpl import service.TermServiceImpl
import service.TermService import service.TermService
import service.DatabaseFactory.getTranslationForLanguages import service.DatabaseFactory.getTranslationForLanguages
import service.SearchType
fun Route.translation(TranslationService: TranslationServiceImpl,TermService: TermServiceImpl) { 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) if (terms == null) call.respond(HttpStatusCode.NotFound)
else call.respond(terms) 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)
}
} }
} }