Add Account

- add addAccount to AccountController
- add account request toAccountEntity to Mapping.kt
- add addAccount to AccountService
This commit is contained in:
2025-01-01 05:50:13 +01:00
parent 0681e367e7
commit dbadfcf731
4 changed files with 51 additions and 0 deletions

View File

@@ -1,2 +1,16 @@
### get user by id ### get user by id
GET {{hostname}}/account-00000000-0000-7000-0000-000000000001 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"
]
}

View File

@@ -3,15 +3,20 @@ package ltd.hlaeja.controller
import java.util.UUID import java.util.UUID
import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Account
import ltd.hlaeja.service.AccountService import ltd.hlaeja.service.AccountService
import ltd.hlaeja.util.toAccountEntity
import ltd.hlaeja.util.toAccountResponse 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.GetMapping
import org.springframework.web.bind.annotation.PathVariable 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 org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono import reactor.core.publisher.Mono
@RestController @RestController
class AccountController( class AccountController(
private val accountService: AccountService, private val accountService: AccountService,
private val passwordEncoder: PasswordEncoder,
) { ) {
@GetMapping("/account-{uuid}") @GetMapping("/account-{uuid}")
@@ -19,4 +24,10 @@ class AccountController(
@PathVariable uuid: UUID, @PathVariable uuid: UUID,
): Mono<Account.Response> = accountService.getUserById(uuid) ): Mono<Account.Response> = accountService.getUserById(uuid)
.map { it.toAccountResponse() } .map { it.toAccountResponse() }
@PostMapping("/account")
fun addAccount(
@RequestBody request: Account.Request,
): Mono<Account.Response> = accountService.addAccount(request.toAccountEntity(passwordEncoder))
.map { it.toAccountResponse() }
} }

View File

@@ -4,6 +4,7 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import java.util.UUID import java.util.UUID
import ltd.hlaeja.entity.AccountEntity import ltd.hlaeja.entity.AccountEntity
import ltd.hlaeja.repository.AccountRepository import ltd.hlaeja.repository.AccountRepository
import org.springframework.dao.DuplicateKeyException
import org.springframework.http.HttpStatus import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
@@ -27,4 +28,16 @@ class AccountService(
): Mono<AccountEntity> = accountRepository.findByUsername(username) ): Mono<AccountEntity> = accountRepository.findByUsername(username)
.doOnNext { log.debug { "Get account ${it.id} for username $username" } } .doOnNext { log.debug { "Get account ${it.id} for username $username" } }
.switchIfEmpty(Mono.error(ResponseStatusException(HttpStatus.NOT_FOUND))) .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))
}
}
} }

View File

@@ -1,8 +1,10 @@
package ltd.hlaeja.util package ltd.hlaeja.util
import java.time.ZonedDateTime
import ltd.hlaeja.entity.AccountEntity import ltd.hlaeja.entity.AccountEntity
import ltd.hlaeja.library.accountRegistry.Account import ltd.hlaeja.library.accountRegistry.Account
import org.springframework.http.HttpStatus.EXPECTATION_FAILED import org.springframework.http.HttpStatus.EXPECTATION_FAILED
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.web.server.ResponseStatusException import org.springframework.web.server.ResponseStatusException
fun AccountEntity.toAccountResponse(): Account.Response = Account.Response( fun AccountEntity.toAccountResponse(): Account.Response = Account.Response(
@@ -13,3 +15,14 @@ fun AccountEntity.toAccountResponse(): Account.Response = Account.Response(
roles.split(","), 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(","),
)