Merge http4k
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package be.simplenotes.domain.usecases
|
||||
|
||||
import arrow.core.Either
|
||||
import be.simplenotes.domain.model.Note
|
||||
import be.simplenotes.domain.model.PersistedNote
|
||||
import be.simplenotes.domain.model.PersistedNoteMetadata
|
||||
import be.simplenotes.domain.security.HtmlSanitizer
|
||||
import be.simplenotes.domain.usecases.markdown.MarkdownConverter
|
||||
import be.simplenotes.domain.usecases.markdown.MarkdownParsingError
|
||||
import be.simplenotes.domain.usecases.repositories.NoteRepository
|
||||
import java.util.*
|
||||
|
||||
class NoteService(
|
||||
private val markdownConverter: MarkdownConverter,
|
||||
private val noteRepository: NoteRepository,
|
||||
) {
|
||||
|
||||
fun create(userId: Int, markdownText: String): Either<MarkdownParsingError, PersistedNote> =
|
||||
markdownConverter
|
||||
.renderDocument(markdownText)
|
||||
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
|
||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||
.map { noteRepository.create(userId, it) }
|
||||
|
||||
fun update(userId: Int, uuid: UUID, markdownText: String): Either<MarkdownParsingError, PersistedNote?> =
|
||||
markdownConverter
|
||||
.renderDocument(markdownText)
|
||||
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
|
||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||
.map { noteRepository.update(userId, uuid, it) }
|
||||
|
||||
fun paginatedNotes(userId: Int, page: Int, itemsPerPage: Int = 20): PaginatedNotes {
|
||||
val count = noteRepository.count(userId)
|
||||
val offset = (page - 1) * itemsPerPage
|
||||
val numberOfPages = (count / itemsPerPage) + 1
|
||||
val notes = if (count == 0) emptyList() else noteRepository.findAll(userId, itemsPerPage, offset)
|
||||
return PaginatedNotes(numberOfPages, notes)
|
||||
}
|
||||
|
||||
fun find(userId: Int, uuid: UUID) = noteRepository.find(userId, uuid)
|
||||
fun delete(userId: Int, uuid: UUID) = noteRepository.delete(userId, uuid)
|
||||
}
|
||||
|
||||
data class PaginatedNotes(val pages: Int, val notes: List<PersistedNoteMetadata>)
|
||||
|
||||
Reference in New Issue
Block a user