update updateAccount in AccountController for PublicEventService

This commit is contained in:
2025-08-12 13:51:48 +02:00
committed by swordsteel
parent dec6b99281
commit 69e293a25f

View File

@@ -1,13 +1,15 @@
package ltd.hlaeja.controller
import java.util.UUID
import ltd.hlaeja.validator.ValidAccount
import ltd.hlaeja.entity.AccountEntity
import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.service.AccountService
import ltd.hlaeja.service.PublicEventService
import ltd.hlaeja.util.detectChanges
import ltd.hlaeja.util.toAccountEntity
import ltd.hlaeja.util.toAccountResponse
import ltd.hlaeja.util.updateAccountEntity
import ltd.hlaeja.validator.ValidAccount
import org.springframework.http.HttpStatus.ACCEPTED
import org.springframework.http.HttpStatus.CREATED
import org.springframework.security.crypto.password.PasswordEncoder
@@ -25,6 +27,7 @@ import reactor.core.publisher.Mono
class AccountController(
private val accountService: AccountService,
private val passwordEncoder: PasswordEncoder,
private val publicEventService: PublicEventService,
) {
@GetMapping("/account-{uuid}")
@@ -40,9 +43,13 @@ class AccountController(
): Mono<Account.Response> = accountService.getUserById(uuid)
.map { user ->
user.updateAccountEntity(request, passwordEncoder)
.also { if (hasChange(user, it)) throw ResponseStatusException(ACCEPTED) }
.let { it to it.detectChanges(user) }
.also { if (it.second.isEmpty()) throw ResponseStatusException(ACCEPTED) }
}
.flatMap { (updated: AccountEntity, changes: List<String>) ->
accountService.updateAccount(updated)
.flatMap { publicEventService.updateAccount(it, changes) }
}
.flatMap { accountService.updateAccount(it) }
.map { it.toAccountResponse() }
@PostMapping("/account")
@@ -51,12 +58,4 @@ class AccountController(
@RequestBody @ValidAccount request: Account.Request,
): Mono<Account.Response> = accountService.addAccount(request.toAccountEntity(passwordEncoder))
.map { it.toAccountResponse() }
private fun hasChange(
user: AccountEntity,
update: AccountEntity,
): Boolean = user.password == update.password &&
user.username == update.username &&
user.enabled == update.enabled &&
user.roles == update.roles
}