Update config
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
package be.simplenotes.domain
|
||||
|
||||
import be.simplenotes.persistence.DbHealthCheck
|
||||
import javax.inject.Singleton
|
||||
|
||||
interface HealthCheckService {
|
||||
fun isOk(): Boolean
|
||||
}
|
||||
|
||||
@Singleton
|
||||
class HealthCheckServiceImpl(private val dbHealthCheck: DbHealthCheck) : HealthCheckService {
|
||||
override fun isOk() = dbHealthCheck.isOk()
|
||||
}
|
||||
+25
-23
@@ -5,7 +5,6 @@ import be.simplenotes.domain.security.HtmlSanitizer
|
||||
import be.simplenotes.domain.utils.parseSearchTerms
|
||||
import be.simplenotes.persistence.repositories.NoteRepository
|
||||
import be.simplenotes.persistence.repositories.UserRepository
|
||||
import be.simplenotes.persistence.transactions.TransactionService
|
||||
import be.simplenotes.search.NoteSearcher
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import be.simplenotes.types.Note
|
||||
@@ -22,31 +21,34 @@ class NoteService(
|
||||
private val noteRepository: NoteRepository,
|
||||
private val userRepository: UserRepository,
|
||||
private val searcher: NoteSearcher,
|
||||
private val htmlSanitizer: HtmlSanitizer,
|
||||
private val transaction: TransactionService,
|
||||
private val htmlSanitizer: HtmlSanitizer
|
||||
) {
|
||||
|
||||
fun create(user: LoggedInUser, markdownText: String) = transaction.use {
|
||||
either.eager<MarkdownParsingError, PersistedNote> {
|
||||
markdownService.renderDocument(markdownText)
|
||||
.map { it.copy(html = htmlSanitizer.sanitize(user, it.html)) }
|
||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||
.map { noteRepository.create(user.userId, it) }
|
||||
.bind()
|
||||
.also { searcher.indexNote(user.userId, it) }
|
||||
}
|
||||
fun create(user: LoggedInUser, markdownText: String) = either.eager<MarkdownParsingError, PersistedNote> {
|
||||
markdownService.renderDocument(markdownText)
|
||||
.map { it.copy(html = htmlSanitizer.sanitize(user, it.html)) }
|
||||
.map { Note(title = it.metadata.title, tags = it.metadata.tags, markdown = markdownText, html = it.html) }
|
||||
.map { noteRepository.create(user.userId, it) }
|
||||
.bind()
|
||||
.also { searcher.indexNote(user.userId, it) }
|
||||
}
|
||||
|
||||
fun update(user: LoggedInUser, uuid: UUID, markdownText: String) = transaction.use {
|
||||
fun update(user: LoggedInUser, uuid: UUID, markdownText: String) =
|
||||
either.eager<MarkdownParsingError, PersistedNote?> {
|
||||
markdownService.renderDocument(markdownText)
|
||||
.map { it.copy(html = htmlSanitizer.sanitize(user, it.html)) }
|
||||
.map { Note(it.metadata, markdown = markdownText, html = it.html) }
|
||||
.map {
|
||||
Note(
|
||||
title = it.metadata.title,
|
||||
tags = it.metadata.tags,
|
||||
markdown = markdownText,
|
||||
html = it.html
|
||||
)
|
||||
}
|
||||
.map { noteRepository.update(user.userId, uuid, it) }
|
||||
.bind()
|
||||
?.also { searcher.updateIndex(user.userId, it) }
|
||||
}
|
||||
}
|
||||
|
||||
fun paginatedNotes(
|
||||
userId: Int,
|
||||
@@ -64,22 +66,22 @@ class NoteService(
|
||||
|
||||
fun find(userId: Int, uuid: UUID) = noteRepository.find(userId, uuid)
|
||||
|
||||
fun trash(userId: Int, uuid: UUID): Boolean = transaction.use {
|
||||
fun trash(userId: Int, uuid: UUID): Boolean {
|
||||
val res = noteRepository.delete(userId, uuid, permanent = false)
|
||||
if (res) searcher.deleteIndex(userId, uuid)
|
||||
res
|
||||
return res
|
||||
}
|
||||
|
||||
fun restore(userId: Int, uuid: UUID): Boolean = transaction.use {
|
||||
fun restore(userId: Int, uuid: UUID): Boolean {
|
||||
val res = noteRepository.restore(userId, uuid)
|
||||
if (res) find(userId, uuid)?.let { note -> searcher.indexNote(userId, note) }
|
||||
res
|
||||
return res
|
||||
}
|
||||
|
||||
fun delete(userId: Int, uuid: UUID): Boolean = transaction.use {
|
||||
fun delete(userId: Int, uuid: UUID): Boolean {
|
||||
val res = noteRepository.delete(userId, uuid, permanent = true)
|
||||
if (res) searcher.deleteIndex(userId, uuid)
|
||||
res
|
||||
return res
|
||||
}
|
||||
|
||||
fun countDeleted(userId: Int) = noteRepository.count(userId, deleted = true)
|
||||
@@ -99,8 +101,8 @@ class NoteService(
|
||||
@PreDestroy
|
||||
fun dropAllIndexes() = searcher.dropAll()
|
||||
|
||||
fun makePublic(userId: Int, uuid: UUID) = transaction.use { noteRepository.makePublic(userId, uuid) }
|
||||
fun makePrivate(userId: Int, uuid: UUID) = transaction.use { noteRepository.makePrivate(userId, uuid) }
|
||||
fun makePublic(userId: Int, uuid: UUID) = noteRepository.makePublic(userId, uuid)
|
||||
fun makePrivate(userId: Int, uuid: UUID) = noteRepository.makePrivate(userId, uuid)
|
||||
fun findPublic(uuid: UUID) = noteRepository.findPublic(uuid)
|
||||
}
|
||||
|
||||
|
||||
+15
-21
@@ -9,7 +9,6 @@ import be.simplenotes.domain.security.PasswordHash
|
||||
import be.simplenotes.domain.security.SimpleJwt
|
||||
import be.simplenotes.domain.validation.UserValidations
|
||||
import be.simplenotes.persistence.repositories.UserRepository
|
||||
import be.simplenotes.persistence.transactions.TransactionService
|
||||
import be.simplenotes.search.NoteSearcher
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import be.simplenotes.types.PersistedUser
|
||||
@@ -29,16 +28,13 @@ internal class UserServiceImpl(
|
||||
private val passwordHash: PasswordHash,
|
||||
private val jwt: SimpleJwt<LoggedInUser>,
|
||||
private val searcher: NoteSearcher,
|
||||
private val transactionService: TransactionService,
|
||||
) : UserService {
|
||||
|
||||
override fun register(form: RegisterForm) = transactionService.use {
|
||||
UserValidations.validateRegister(form)
|
||||
.filterOrElse({ !userRepository.exists(it.username) }, { RegisterError.UserExists })
|
||||
.map { it.copy(password = passwordHash.crypt(it.password)) }
|
||||
.map { userRepository.create(it) }
|
||||
.leftIfNull { RegisterError.UserExists }
|
||||
}
|
||||
override fun register(form: RegisterForm) = UserValidations.validateRegister(form)
|
||||
.filterOrElse({ !userRepository.exists(it.username) }, { RegisterError.UserExists })
|
||||
.map { it.copy(password = passwordHash.crypt(it.password)) }
|
||||
.map { userRepository.create(it) }
|
||||
.leftIfNull { RegisterError.UserExists }
|
||||
|
||||
override fun login(form: LoginForm) = either.eager<LoginError, Token> {
|
||||
UserValidations.validateLogin(form)
|
||||
@@ -50,18 +46,16 @@ internal class UserServiceImpl(
|
||||
.bind()
|
||||
}
|
||||
|
||||
override fun delete(form: DeleteForm) = transactionService.use {
|
||||
either.eager<DeleteError, Unit> {
|
||||
val user = !UserValidations.validateDelete(form)
|
||||
val persistedUser = !userRepository.find(user.username).rightIfNotNull { DeleteError.Unregistered }
|
||||
!Either.conditionally(
|
||||
passwordHash.verify(user.password, persistedUser.password),
|
||||
{ DeleteError.WrongPassword },
|
||||
{ }
|
||||
)
|
||||
!Either.conditionally(userRepository.delete(persistedUser.id), { DeleteError.Unregistered }, { })
|
||||
searcher.dropIndex(persistedUser.id)
|
||||
}
|
||||
override fun delete(form: DeleteForm) = either.eager<DeleteError, Unit> {
|
||||
val user = !UserValidations.validateDelete(form)
|
||||
val persistedUser = !userRepository.find(user.username).rightIfNotNull { DeleteError.Unregistered }
|
||||
!Either.conditionally(
|
||||
passwordHash.verify(user.password, persistedUser.password),
|
||||
{ DeleteError.WrongPassword },
|
||||
{ }
|
||||
)
|
||||
!Either.conditionally(userRepository.delete(persistedUser.id), { DeleteError.Unregistered }, { })
|
||||
searcher.dropIndex(persistedUser.id)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import be.simplenotes.domain.security.UserJwtMapper
|
||||
import be.simplenotes.domain.testutils.isLeftOfType
|
||||
import be.simplenotes.domain.testutils.isRight
|
||||
import be.simplenotes.persistence.repositories.UserRepository
|
||||
import be.simplenotes.persistence.transactions.TransactionService
|
||||
import be.simplenotes.types.PersistedUser
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
@@ -23,16 +22,12 @@ internal class UserServiceTest {
|
||||
val passwordHash = BcryptPasswordHash(test = true)
|
||||
val jwtConfig = JwtConfig("a secret", 1, TimeUnit.HOURS)
|
||||
val simpleJwt = SimpleJwt(jwtConfig, UserJwtMapper())
|
||||
val noopTransactionService = object : TransactionService {
|
||||
override fun <T> use(block: () -> T) = block()
|
||||
}
|
||||
|
||||
val userService = UserServiceImpl(
|
||||
userRepository = userRepository,
|
||||
passwordHash = passwordHash,
|
||||
jwt = simpleJwt,
|
||||
searcher = mockk(),
|
||||
transactionService = noopTransactionService
|
||||
)
|
||||
|
||||
@BeforeEach
|
||||
|
||||
Reference in New Issue
Block a user