added basic edit save account

- changes to AccountController
  - added postEditAccount
  - change postCreateAccount to use lambda
- added success message to edit.html
- added lambda to deal with password to toAccountRequest in Mapping.kt
- added updateAccount to AccountRegistryService
- added accountRegistryUpdate to WebClientCalls.kt
- added NoChangeException
- added NotFoundException
This commit is contained in:
2025-01-28 21:49:31 +01:00
parent 14e7971f73
commit 794c0c49d6
7 changed files with 137 additions and 22 deletions

View File

@@ -1,6 +1,5 @@
package ltd.hlaeja.util
import ltd.hlaeja.exception.PasswordException
import ltd.hlaeja.form.AccountForm
import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.library.accountRegistry.Authentication
@@ -11,9 +10,9 @@ fun SpringAuthentication.toAuthenticationRequest(): Authentication.Request = Aut
credentials as String,
)
fun AccountForm.toAccountRequest(): Account.Request = Account.Request(
fun AccountForm.toAccountRequest(operation: (CharSequence?) -> CharSequence?): Account.Request = Account.Request(
username = username,
password = password ?: throw PasswordException("Password requirements failed"),
password = operation(password),
enabled = enabled,
roles = listOf("ROLE_${role.uppercase()}"),
)
@@ -21,5 +20,5 @@ fun AccountForm.toAccountRequest(): Account.Request = Account.Request(
fun Account.Response.toAccountForm(): AccountForm = AccountForm(
username = username,
enabled = enabled,
role = roles.first().removePrefix("ROLE_").lowercase()
role = roles.first().removePrefix("ROLE_").lowercase(),
)

View File

@@ -2,10 +2,13 @@ package ltd.hlaeja.util
import java.util.UUID
import ltd.hlaeja.exception.AccountRegistryException
import ltd.hlaeja.exception.NoChangeException
import ltd.hlaeja.exception.NotFoundException
import ltd.hlaeja.exception.UsernameDuplicateException
import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.library.accountRegistry.Authentication
import ltd.hlaeja.property.AccountRegistryProperty
import org.springframework.http.HttpStatus.ACCEPTED
import org.springframework.http.HttpStatus.BAD_REQUEST
import org.springframework.http.HttpStatus.CONFLICT
import org.springframework.http.HttpStatus.LOCKED
@@ -36,7 +39,7 @@ fun WebClient.accountRegistryAccounts(
size: Int,
property: AccountRegistryProperty,
): Flux<Account.Response> = get()
.uri("${property.url}/accounts?page=$page&size=$size".also(::logCall))
.uri("${property.url}/accounts/page-$page/show-$size".also(::logCall))
.retrieve()
.bodyToFlux(Account.Response::class.java)
@@ -47,7 +50,7 @@ fun WebClient.accountRegistryCreate(
.uri("${property.url}/account".also(::logCall))
.bodyValue(request)
.retrieve()
.onStatus(CONFLICT::equals) { throw UsernameDuplicateException() }
.onStatus(CONFLICT::equals) { throw UsernameDuplicateException("Remote service returned 409") }
.onStatus(BAD_REQUEST::equals) { throw AccountRegistryException("Remote service returned 400") }
.bodyToMono(Account.Response::class.java)
@@ -59,3 +62,17 @@ fun WebClient.accountRegistryAccount(
.retrieve()
.onStatus(NOT_FOUND::equals) { throw ResponseStatusException(NOT_FOUND) }
.bodyToMono(Account.Response::class.java)
fun WebClient.accountRegistryUpdate(
account: UUID,
request: Account.Request,
property: AccountRegistryProperty,
): Mono<Account.Response> = put()
.uri("${property.url}/account-$account".also(::logCall))
.bodyValue(request)
.retrieve()
.onStatus(ACCEPTED::equals) { throw NoChangeException("Remote service returned 202") }
.onStatus(BAD_REQUEST::equals) { throw AccountRegistryException("Remote service returned 400") }
.onStatus(NOT_FOUND::equals) { throw NotFoundException("Remote service returned 404") }
.onStatus(CONFLICT::equals) { throw UsernameDuplicateException("Remote service returned 409") }
.bodyToMono(Account.Response::class.java)