Fix arrow depreciations

This commit is contained in:
Hubert Van De Walle 2020-10-25 23:46:56 +01:00
parent c2c03e415e
commit e64352f54c
5 changed files with 23 additions and 27 deletions

View File

@ -1,10 +1,6 @@
package be.simplenotes.domain.usecases
import arrow.core.Either
import arrow.core.extensions.fx
import be.simplenotes.types.Note
import be.simplenotes.types.PersistedNote
import be.simplenotes.types.PersistedNoteMetadata
import arrow.core.computations.either
import be.simplenotes.domain.security.HtmlSanitizer
import be.simplenotes.domain.usecases.markdown.MarkdownConverter
import be.simplenotes.domain.usecases.markdown.MarkdownParsingError
@ -12,6 +8,9 @@ import be.simplenotes.persistance.repositories.NoteRepository
import be.simplenotes.persistance.repositories.UserRepository
import be.simplenotes.search.NoteSearcher
import be.simplenotes.search.SearchTerms
import be.simplenotes.types.Note
import be.simplenotes.types.PersistedNote
import be.simplenotes.types.PersistedNoteMetadata
import java.util.*
class NoteService(
@ -21,7 +20,7 @@ class NoteService(
private val searcher: NoteSearcher,
) {
fun create(userId: Int, markdownText: String) = Either.fx<MarkdownParsingError, PersistedNote> {
fun create(userId: Int, markdownText: String) = either.eager<MarkdownParsingError, PersistedNote> {
val persistedNote = !markdownConverter.renderDocument(markdownText)
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
@ -31,7 +30,7 @@ class NoteService(
persistedNote
}
fun update(userId: Int, uuid: UUID, markdownText: String) = Either.fx<MarkdownParsingError, PersistedNote?> {
fun update(userId: Int, uuid: UUID, markdownText: String) = either.eager<MarkdownParsingError, PersistedNote?> {
val persistedNote = !markdownConverter.renderDocument(markdownText)
.map { it.copy(html = HtmlSanitizer.sanitize(it.html)) }
.map { Note(it.metadata, markdown = markdownText, html = it.html) }

View File

@ -1,11 +1,11 @@
package be.simplenotes.domain.usecases.markdown
import arrow.core.Either
import arrow.core.extensions.fx
import arrow.core.computations.either
import arrow.core.left
import arrow.core.right
import be.simplenotes.types.NoteMetadata
import be.simplenotes.domain.validation.NoteValidations
import be.simplenotes.types.NoteMetadata
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension
import com.vladsch.flexmark.html.HtmlRenderer
import com.vladsch.flexmark.parser.Parser
@ -73,10 +73,10 @@ internal class MarkdownConverterImpl : MarkdownConverter {
private fun renderMarkdown(markdown: String) = parser.parse(markdown).run(renderer::render)
override fun renderDocument(input: String) = Either.fx<MarkdownParsingError, Document> {
override fun renderDocument(input: String) = either.eager<MarkdownParsingError, Document> {
val (meta, md) = !splitMetaFromDocument(input)
val parsedMeta = !parseMeta(meta)
!NoteValidations.validateMetadata(parsedMeta).toEither { }.swap()
!Either.fromNullable(NoteValidations.validateMetadata(parsedMeta)).swap()
val html = renderMarkdown(md)
Document(parsedMeta, html)
}

View File

@ -1,11 +1,11 @@
package be.simplenotes.domain.usecases.users.delete
import arrow.core.Either
import arrow.core.extensions.fx
import arrow.core.computations.either
import arrow.core.rightIfNotNull
import be.simplenotes.domain.security.PasswordHash
import be.simplenotes.persistance.repositories.UserRepository
import be.simplenotes.domain.validation.UserValidations
import be.simplenotes.persistance.repositories.UserRepository
import be.simplenotes.search.NoteSearcher
internal class DeleteUseCaseImpl(
@ -13,15 +13,14 @@ internal class DeleteUseCaseImpl(
private val passwordHash: PasswordHash,
private val searcher: NoteSearcher,
) : DeleteUseCase {
override fun delete(form: DeleteForm) = Either.fx<DeleteError, Unit> {
override fun delete(form: DeleteForm) = either.eager<DeleteError, Unit> {
val user = !UserValidations.validateDelete(form)
val persistedUser = !userRepository.find(user.username).rightIfNotNull { DeleteError.Unregistered }
!Either.cond(
!Either.conditionally(
passwordHash.verify(user.password, persistedUser.password),
{ Unit },
{ DeleteError.WrongPassword }
)
!Either.cond(userRepository.delete(persistedUser.id), { Unit }, { DeleteError.Unregistered })
{ DeleteError.WrongPassword },
{ Unit })
!Either.conditionally(userRepository.delete(persistedUser.id), { DeleteError.Unregistered }, { Unit })
searcher.dropIndex(persistedUser.id)
}
}

View File

@ -1,7 +1,6 @@
package be.simplenotes.domain.usecases.users.login
import arrow.core.Either
import arrow.core.extensions.fx
import arrow.core.computations.either
import arrow.core.filterOrElse
import arrow.core.rightIfNotNull
import be.simplenotes.domain.security.PasswordHash
@ -15,7 +14,7 @@ internal class LoginUseCaseImpl(
private val passwordHash: PasswordHash,
private val jwt: SimpleJwt
) : LoginUseCase {
override fun login(form: LoginForm) = Either.fx<LoginError, Token> {
override fun login(form: LoginForm) = either.eager<LoginError, Token> {
val user = !UserValidations.validateLogin(form)
!userRepository.find(user.username)
.rightIfNotNull { Unregistered }

View File

@ -1,8 +1,7 @@
package be.simplenotes.domain.validation
import arrow.core.*
import be.simplenotes.types.NoteMetadata
import be.simplenotes.domain.usecases.markdown.ValidationError
import be.simplenotes.types.NoteMetadata
import io.konform.validation.Validation
import io.konform.validation.jsonschema.maxItems
import io.konform.validation.jsonschema.maxLength
@ -28,9 +27,9 @@ internal object NoteValidations {
}
}
fun validateMetadata(meta: NoteMetadata): Option<ValidationError> {
fun validateMetadata(meta: NoteMetadata): ValidationError? {
val errors = metaValidator.validate(meta).errors
return if (errors.isEmpty()) none()
else return ValidationError(errors).some()
return if (errors.isEmpty()) null
else return ValidationError(errors)
}
}