Add searcher to note service
This commit is contained in:
parent
3861fb6b97
commit
ab3766b8b8
@ -14,6 +14,11 @@
|
|||||||
<artifactId>shared</artifactId>
|
<artifactId>shared</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>be.simplenotes</groupId>
|
||||||
|
<artifactId>search</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>be.simplenotes</groupId>
|
<groupId>be.simplenotes</groupId>
|
||||||
<artifactId>shared</artifactId>
|
<artifactId>shared</artifactId>
|
||||||
|
|||||||
@ -26,7 +26,7 @@ val domainModule = module {
|
|||||||
single<PasswordHash> { BcryptPasswordHash() }
|
single<PasswordHash> { BcryptPasswordHash() }
|
||||||
single { SimpleJwt(get()) }
|
single { SimpleJwt(get()) }
|
||||||
single { JwtPayloadExtractor(get()) }
|
single { JwtPayloadExtractor(get()) }
|
||||||
single { NoteService(get(), get()) }
|
single { NoteService(get(), get(), get()) }
|
||||||
single<MarkdownConverter> { MarkdownConverterImpl() }
|
single<MarkdownConverter> { MarkdownConverterImpl() }
|
||||||
single<ExportUseCase> { ExportUseCaseImpl(get()) }
|
single<ExportUseCase> { ExportUseCaseImpl(get()) }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package be.simplenotes.domain.usecases
|
package be.simplenotes.domain.usecases
|
||||||
|
|
||||||
import arrow.core.Either
|
import arrow.core.Either
|
||||||
|
import arrow.core.extensions.fx
|
||||||
import be.simplenotes.domain.model.Note
|
import be.simplenotes.domain.model.Note
|
||||||
import be.simplenotes.domain.model.PersistedNote
|
import be.simplenotes.domain.model.PersistedNote
|
||||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||||
@ -8,33 +9,41 @@ 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.repositories.NoteRepository
|
import be.simplenotes.domain.usecases.repositories.NoteRepository
|
||||||
|
import be.simplenotes.search.NoteSearcher
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class NoteService(
|
class NoteService(
|
||||||
private val markdownConverter: MarkdownConverter,
|
private val markdownConverter: MarkdownConverter,
|
||||||
private val noteRepository: NoteRepository,
|
private val noteRepository: NoteRepository,
|
||||||
|
private val searcher: NoteSearcher,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
fun create(userId: Int, markdownText: String): Either<MarkdownParsingError, PersistedNote> =
|
fun create(userId: Int, markdownText: String) = Either.fx<MarkdownParsingError, PersistedNote> {
|
||||||
markdownConverter
|
val persistedNote = !markdownConverter.renderDocument(markdownText)
|
||||||
.renderDocument(markdownText)
|
|
||||||
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
|
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
|
||||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||||
.map { noteRepository.create(userId, it) }
|
.map { noteRepository.create(userId, it) }
|
||||||
|
|
||||||
fun update(userId: Int, uuid: UUID, markdownText: String): Either<MarkdownParsingError, PersistedNote?> =
|
searcher.indexNote(userId, persistedNote)
|
||||||
markdownConverter
|
persistedNote
|
||||||
.renderDocument(markdownText)
|
}
|
||||||
|
|
||||||
|
fun update(userId: Int, uuid: UUID, markdownText: String) = Either.fx<MarkdownParsingError, PersistedNote?> {
|
||||||
|
val persistedNote = !markdownConverter.renderDocument(markdownText)
|
||||||
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
|
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
|
||||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||||
.map { noteRepository.update(userId, uuid, it) }
|
.map { noteRepository.update(userId, uuid, it) }
|
||||||
|
|
||||||
|
persistedNote?.let { searcher.updateIndex(userId, it) }
|
||||||
|
persistedNote
|
||||||
|
}
|
||||||
|
|
||||||
fun paginatedNotes(
|
fun paginatedNotes(
|
||||||
userId: Int,
|
userId: Int,
|
||||||
page: Int,
|
page: Int,
|
||||||
itemsPerPage: Int = 20,
|
itemsPerPage: Int = 20,
|
||||||
tag: String? = null,
|
tag: String? = null,
|
||||||
deleted: Boolean = false
|
deleted: Boolean = false,
|
||||||
): PaginatedNotes {
|
): PaginatedNotes {
|
||||||
val count = noteRepository.count(userId, tag, deleted)
|
val count = noteRepository.count(userId, tag, deleted)
|
||||||
val offset = (page - 1) * itemsPerPage
|
val offset = (page - 1) * itemsPerPage
|
||||||
@ -44,9 +53,25 @@ class NoteService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun find(userId: Int, uuid: UUID) = noteRepository.find(userId, uuid)
|
fun find(userId: Int, uuid: UUID) = noteRepository.find(userId, uuid)
|
||||||
fun trash(userId: Int, uuid: UUID) = noteRepository.delete(userId, uuid, permanent = false)
|
|
||||||
fun restore(userId: Int, uuid: UUID) = noteRepository.restore(userId, uuid)
|
fun trash(userId: Int, uuid: UUID): Boolean {
|
||||||
fun delete(userId: Int, uuid: UUID) = noteRepository.delete(userId, uuid, permanent = true)
|
val res = noteRepository.delete(userId, uuid, permanent = false)
|
||||||
|
if (res) searcher.deleteIndex(userId, uuid)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
fun restore(userId: Int, uuid: UUID): Boolean {
|
||||||
|
val res = noteRepository.restore(userId, uuid)
|
||||||
|
if (res) find(userId, uuid)?.let { note -> searcher.indexNote(userId, note) }
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
fun delete(userId: Int, uuid: UUID): Boolean {
|
||||||
|
val res = noteRepository.delete(userId, uuid, permanent = true)
|
||||||
|
if (res) searcher.deleteIndex(userId, uuid)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
fun countDeleted(userId: Int) = noteRepository.count(userId, deleted = true)
|
fun countDeleted(userId: Int) = noteRepository.count(userId, deleted = true)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
7
search/src/main/kotlin/SeachModule.kt
Normal file
7
search/src/main/kotlin/SeachModule.kt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
package be.simplenotes.search
|
||||||
|
|
||||||
|
import org.koin.dsl.module
|
||||||
|
|
||||||
|
val searchModule = module {
|
||||||
|
single { NoteSearcher() }
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user