diff --git a/http/account.http b/http/account.http index 51aa14a..1bbe2e4 100644 --- a/http/account.http +++ b/http/account.http @@ -1,2 +1,16 @@ ### get user by id GET {{hostname}}/account-00000000-0000-7000-0000-000000000001 + +### Get admin information +POST {{hostname}}/account +Content-Type: application/json + +{ + "username": "user01010101", + "password": "p4ssw0rd", + "enabled": true, + "roles": [ + "ROLE_ADMIN", + "ROLE_TEST" + ] +} diff --git a/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt b/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt index a75a6dc..405d0d4 100644 --- a/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt +++ b/src/main/kotlin/ltd/hlaeja/controller/AccountController.kt @@ -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 = accountService.getUserById(uuid) .map { it.toAccountResponse() } + + @PostMapping("/account") + fun addAccount( + @RequestBody request: Account.Request, + ): Mono = accountService.addAccount(request.toAccountEntity(passwordEncoder)) + .map { it.toAccountResponse() } } diff --git a/src/main/kotlin/ltd/hlaeja/service/AccountService.kt b/src/main/kotlin/ltd/hlaeja/service/AccountService.kt index 77f3714..c83ddbe 100644 --- a/src/main/kotlin/ltd/hlaeja/service/AccountService.kt +++ b/src/main/kotlin/ltd/hlaeja/service/AccountService.kt @@ -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 = 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 = 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)) + } + } } diff --git a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt index 5b1f3b1..2c3954c 100644 --- a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt +++ b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt @@ -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(","), +)