Move search parsing to domain layer
This commit is contained in:
parent
c39a20cf96
commit
568a2c6831
@ -10,7 +10,6 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(project(":simplenotes-search"))
|
|
||||||
implementation(project(":simplenotes-domain"))
|
implementation(project(":simplenotes-domain"))
|
||||||
implementation(project(":simplenotes-types"))
|
implementation(project(":simplenotes-types"))
|
||||||
implementation(project(":simplenotes-config"))
|
implementation(project(":simplenotes-config"))
|
||||||
|
|||||||
@ -1,7 +1,6 @@
|
|||||||
package be.simplenotes.app.api
|
package be.simplenotes.app.api
|
||||||
|
|
||||||
import be.simplenotes.app.extensions.auto
|
import be.simplenotes.app.extensions.auto
|
||||||
import be.simplenotes.app.utils.parseSearchTerms
|
|
||||||
import be.simplenotes.domain.usecases.NoteService
|
import be.simplenotes.domain.usecases.NoteService
|
||||||
import be.simplenotes.types.LoggedInUser
|
import be.simplenotes.types.LoggedInUser
|
||||||
import be.simplenotes.types.PersistedNote
|
import be.simplenotes.types.PersistedNote
|
||||||
@ -58,8 +57,7 @@ class ApiNoteController(
|
|||||||
|
|
||||||
fun search(request: Request, loggedInUser: LoggedInUser): Response {
|
fun search(request: Request, loggedInUser: LoggedInUser): Response {
|
||||||
val query = searchContentLens(request)
|
val query = searchContentLens(request)
|
||||||
val terms = parseSearchTerms(query)
|
val notes = noteService.search(loggedInUser.userId, query)
|
||||||
val notes = noteService.search(loggedInUser.userId, terms)
|
|
||||||
return persistedNotesMetadataLens(notes, Response(OK))
|
return persistedNotesMetadataLens(notes, Response(OK))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package be.simplenotes.app.controllers
|
|||||||
|
|
||||||
import be.simplenotes.app.extensions.html
|
import be.simplenotes.app.extensions.html
|
||||||
import be.simplenotes.app.extensions.redirect
|
import be.simplenotes.app.extensions.redirect
|
||||||
import be.simplenotes.app.utils.parseSearchTerms
|
|
||||||
import be.simplenotes.domain.usecases.NoteService
|
import be.simplenotes.domain.usecases.NoteService
|
||||||
import be.simplenotes.domain.usecases.markdown.InvalidMeta
|
import be.simplenotes.domain.usecases.markdown.InvalidMeta
|
||||||
import be.simplenotes.domain.usecases.markdown.MissingMeta
|
import be.simplenotes.domain.usecases.markdown.MissingMeta
|
||||||
@ -69,8 +68,7 @@ class NoteController(
|
|||||||
|
|
||||||
fun search(request: Request, loggedInUser: LoggedInUser): Response {
|
fun search(request: Request, loggedInUser: LoggedInUser): Response {
|
||||||
val query = request.form("search") ?: ""
|
val query = request.form("search") ?: ""
|
||||||
val terms = parseSearchTerms(query)
|
val notes = noteService.search(loggedInUser.userId, query)
|
||||||
val notes = noteService.search(loggedInUser.userId, terms)
|
|
||||||
val deletedCount = noteService.countDeleted(loggedInUser.userId)
|
val deletedCount = noteService.countDeleted(loggedInUser.userId)
|
||||||
return Response(OK).html(view.search(loggedInUser, notes, query, deletedCount))
|
return Response(OK).html(view.search(loggedInUser, notes, query, deletedCount))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,11 @@ import arrow.core.computations.either
|
|||||||
import be.simplenotes.domain.security.HtmlSanitizer
|
import be.simplenotes.domain.security.HtmlSanitizer
|
||||||
import be.simplenotes.domain.usecases.markdown.MarkdownConverter
|
import be.simplenotes.domain.usecases.markdown.MarkdownConverter
|
||||||
import be.simplenotes.domain.usecases.markdown.MarkdownParsingError
|
import be.simplenotes.domain.usecases.markdown.MarkdownParsingError
|
||||||
|
import be.simplenotes.domain.usecases.search.parseSearchTerms
|
||||||
import be.simplenotes.persistance.repositories.NoteRepository
|
import be.simplenotes.persistance.repositories.NoteRepository
|
||||||
import be.simplenotes.persistance.repositories.UserRepository
|
import be.simplenotes.persistance.repositories.UserRepository
|
||||||
import be.simplenotes.persistance.transactions.TransactionService
|
import be.simplenotes.persistance.transactions.TransactionService
|
||||||
import be.simplenotes.search.NoteSearcher
|
import be.simplenotes.search.NoteSearcher
|
||||||
import be.simplenotes.search.SearchTerms
|
|
||||||
import be.simplenotes.types.LoggedInUser
|
import be.simplenotes.types.LoggedInUser
|
||||||
import be.simplenotes.types.Note
|
import be.simplenotes.types.Note
|
||||||
import be.simplenotes.types.PersistedNote
|
import be.simplenotes.types.PersistedNote
|
||||||
@ -102,7 +102,7 @@ class NoteService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun search(userId: Int, searchTerms: SearchTerms) = searcher.search(userId, searchTerms)
|
fun search(userId: Int, searchInput: String) = searcher.search(userId, parseSearchTerms(searchInput))
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
fun dropAllIndexes() = searcher.dropAll()
|
fun dropAllIndexes() = searcher.dropAll()
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package be.simplenotes.app.utils
|
package be.simplenotes.domain.usecases.search
|
||||||
|
|
||||||
import be.simplenotes.search.SearchTerms
|
import be.simplenotes.search.SearchTerms
|
||||||
|
|
||||||
@ -16,7 +16,7 @@ private val outerTagRe = outerRegex("tag")
|
|||||||
private val contentRe = innerRegex("content")
|
private val contentRe = innerRegex("content")
|
||||||
private val outerContentRe = outerRegex("content")
|
private val outerContentRe = outerRegex("content")
|
||||||
|
|
||||||
fun parseSearchTerms(input: String): SearchTerms {
|
internal fun parseSearchTerms(input: String): SearchTerms {
|
||||||
var c: String = input
|
var c: String = input
|
||||||
|
|
||||||
fun extract(innerRegex: Regex, outerRegex: Regex): String? {
|
fun extract(innerRegex: Regex, outerRegex: Regex): String? {
|
||||||
@ -1,7 +1,8 @@
|
|||||||
package be.simplenotes.app.utils
|
package be.simplenotes.domain.usecases.search
|
||||||
|
|
||||||
import be.simplenotes.search.SearchTerms
|
import be.simplenotes.search.SearchTerms
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import com.natpryce.hamkrest.assertion.assertThat
|
||||||
|
import com.natpryce.hamkrest.equalTo
|
||||||
import org.junit.jupiter.params.ParameterizedTest
|
import org.junit.jupiter.params.ParameterizedTest
|
||||||
import org.junit.jupiter.params.provider.MethodSource
|
import org.junit.jupiter.params.provider.MethodSource
|
||||||
import java.util.stream.Stream
|
import java.util.stream.Stream
|
||||||
@ -13,7 +14,7 @@ internal class SearchTermsParserKtTest {
|
|||||||
title: String? = null,
|
title: String? = null,
|
||||||
tag: String? = null,
|
tag: String? = null,
|
||||||
content: String? = null,
|
content: String? = null,
|
||||||
all: String? = null
|
all: String? = null,
|
||||||
): Pair<String, SearchTerms> = input to SearchTerms(title, tag, content, all)
|
): Pair<String, SearchTerms> = input to SearchTerms(title, tag, content, all)
|
||||||
|
|
||||||
@Suppress("Unused")
|
@Suppress("Unused")
|
||||||
@ -39,6 +40,6 @@ internal class SearchTermsParserKtTest {
|
|||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource("results")
|
@MethodSource("results")
|
||||||
fun `valid search parser`(case: Pair<String, SearchTerms>) {
|
fun `valid search parser`(case: Pair<String, SearchTerms>) {
|
||||||
assertThat(parseSearchTerms(case.first)).isEqualTo(case.second)
|
assertThat(parseSearchTerms(case.first), equalTo(case.second))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user