🚧 Skrevet bedre søkefunksjon som putter mindre press på database
Co-authored-by: Sindre Kjelsrud <sindre.kjelsrud@nav.no> Co-authored-by: Markus A. R. Johansen <markus.aleksander.rakil.johansen@nav.no>
This commit is contained in:
parent
8134cfc239
commit
d0164c2688
6 changed files with 49 additions and 92 deletions
|
@ -6,6 +6,7 @@ import no.nav.helse.sprik.db.FeilmeldingTable.dato
|
|||
import no.nav.helse.sprik.db.FeilmeldingTable.tittel
|
||||
import no.nav.helse.sprik.modell.Feilmelding
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.like
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.time.LocalDateTime
|
||||
|
||||
|
@ -31,4 +32,18 @@ class FeilmeldingRepository {
|
|||
fun hentAlleFeilmeldinger(): List<Feilmelding> = transaction {
|
||||
FeilmeldingTable.selectAll().map(::radTilFeilmelding)
|
||||
}
|
||||
|
||||
fun hentSokteFeilmeldinger(sokeord: String): List<Feilmelding> = transaction {
|
||||
/* val query = FeilmeldingTable.selectAll()
|
||||
tittel?.let {
|
||||
query.andWhere { FeilmeldingTable.tittel like sokeord }
|
||||
}
|
||||
beskrivelse?.let {
|
||||
query.andWhere { FeilmeldingTable.beskrivelse like sokeord }
|
||||
} */
|
||||
val sok = "%${sokeord}%"
|
||||
|
||||
FeilmeldingTable.select((FeilmeldingTable.tittel like sok) or (FeilmeldingTable.beskrivelse like sok)).map(::radTilFeilmelding)
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
package no.nav.helse.sprik.modell
|
||||
|
||||
import no.nav.helse.sprik.db.FeilmeldingRepository
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class Sokemotor {
|
||||
private val feilmeldingRepository = FeilmeldingRepository()
|
||||
|
||||
fun sok(s: String): List<Feilmelding> {
|
||||
|
||||
val feilmeldinger = feilmeldingRepository.hentAlleFeilmeldinger()
|
||||
val resultat = ArrayList<Feilmelding>()
|
||||
|
||||
val processed = s.lowercase()
|
||||
|
||||
for (i in feilmeldinger) {
|
||||
if (i.tittel.lowercase().contains(processed) or i.beskrivelse.lowercase().contains(processed) or processed.equals("")) {
|
||||
println(i.toString() + " matcher" + processed)
|
||||
resultat.add(i)
|
||||
}else{
|
||||
println(i.toString() + " matcher ikke")
|
||||
}
|
||||
}
|
||||
|
||||
return resultat.toList()
|
||||
}
|
||||
}
|
|
@ -11,11 +11,8 @@ import io.ktor.server.engine.*
|
|||
import io.ktor.server.http.content.*
|
||||
import io.ktor.server.plugins.cors.routing.*
|
||||
import io.ktor.server.request.*
|
||||
import no.nav.helse.sprik.Test
|
||||
import no.nav.helse.sprik.db.FeilmeldingRepository
|
||||
import no.nav.helse.sprik.modell.Feilmelding
|
||||
import no.nav.helse.sprik.modell.Sokemotor
|
||||
import java.time.LocalDateTime
|
||||
|
||||
fun configureRouting(): ApplicationEngine = embeddedServer(CIO, applicationEngineEnvironment {
|
||||
//Repositories for handlinger mot database:
|
||||
|
@ -59,9 +56,9 @@ fun configureRouting(): ApplicationEngine = embeddedServer(CIO, applicationEngin
|
|||
val testMelding = feilmeldingRepository.hentAlleFeilmeldinger()
|
||||
call.respond(status = HttpStatusCode.Created, message = testMelding)
|
||||
}
|
||||
post("/api/hentsok"){
|
||||
get("/api/hentsok/{sokestreng}"){
|
||||
val sokestreng = call.receive<String>()
|
||||
call.respond(status = HttpStatusCode.Created, message = sokestreng + ": " + sokemotor.sok(sokestreng))
|
||||
call.respond(status = HttpStatusCode.Created, message = sokestreng)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue