Add Account
- add addAccount to AccountController - add account request toAccountEntity to Mapping.kt - add addAccount to AccountService
This commit is contained in:
@@ -3,15 +3,20 @@ package ltd.hlaeja.controller
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.library.accountRegistry.Account
|
||||
import ltd.hlaeja.service.AccountService
|
||||
import ltd.hlaeja.util.toAccountEntity
|
||||
import ltd.hlaeja.util.toAccountResponse
|
||||
import org.springframework.security.crypto.password.PasswordEncoder
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@RestController
|
||||
class AccountController(
|
||||
private val accountService: AccountService,
|
||||
private val passwordEncoder: PasswordEncoder,
|
||||
) {
|
||||
|
||||
@GetMapping("/account-{uuid}")
|
||||
@@ -19,4 +24,10 @@ class AccountController(
|
||||
@PathVariable uuid: UUID,
|
||||
): Mono<Account.Response> = accountService.getUserById(uuid)
|
||||
.map { it.toAccountResponse() }
|
||||
|
||||
@PostMapping("/account")
|
||||
fun addAccount(
|
||||
@RequestBody request: Account.Request,
|
||||
): Mono<Account.Response> = accountService.addAccount(request.toAccountEntity(passwordEncoder))
|
||||
.map { it.toAccountResponse() }
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.entity.AccountEntity
|
||||
import ltd.hlaeja.repository.AccountRepository
|
||||
import org.springframework.dao.DuplicateKeyException
|
||||
import org.springframework.http.HttpStatus
|
||||
import org.springframework.stereotype.Service
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
@@ -27,4 +28,16 @@ class AccountService(
|
||||
): Mono<AccountEntity> = accountRepository.findByUsername(username)
|
||||
.doOnNext { log.debug { "Get account ${it.id} for username $username" } }
|
||||
.switchIfEmpty(Mono.error(ResponseStatusException(HttpStatus.NOT_FOUND)))
|
||||
|
||||
fun addAccount(
|
||||
accountEntity: AccountEntity,
|
||||
): Mono<AccountEntity> = accountRepository.save(accountEntity)
|
||||
.doOnNext { log.debug { "Added new type: $it.id" } }
|
||||
.onErrorResume {
|
||||
log.debug { it.localizedMessage }
|
||||
when {
|
||||
it is DuplicateKeyException -> Mono.error(ResponseStatusException(HttpStatus.CONFLICT))
|
||||
else -> Mono.error(ResponseStatusException(HttpStatus.BAD_REQUEST))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import java.time.ZonedDateTime
|
||||
import ltd.hlaeja.entity.AccountEntity
|
||||
import ltd.hlaeja.library.accountRegistry.Account
|
||||
import org.springframework.http.HttpStatus.EXPECTATION_FAILED
|
||||
import org.springframework.security.crypto.password.PasswordEncoder
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
fun AccountEntity.toAccountResponse(): Account.Response = Account.Response(
|
||||
@@ -13,3 +15,14 @@ fun AccountEntity.toAccountResponse(): Account.Response = Account.Response(
|
||||
roles.split(","),
|
||||
)
|
||||
|
||||
fun Account.Request.toAccountEntity(
|
||||
passwordEncoder: PasswordEncoder,
|
||||
): AccountEntity = AccountEntity(
|
||||
id = null,
|
||||
createdAt = ZonedDateTime.now(),
|
||||
updatedAt = ZonedDateTime.now(),
|
||||
enabled = enabled,
|
||||
username = username,
|
||||
password = passwordEncoder.encode(password),
|
||||
roles = roles.joinToString(","),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user