Files
hlaeja-account-registry/src/main/kotlin/ltd/hlaeja/util/Mapping.kt
Swordsteel dbadfcf731 Add Account
- add addAccount to AccountController
- add account request toAccountEntity to Mapping.kt
- add addAccount to AccountService
2025-01-02 06:57:38 +01:00

29 lines
876 B
Kotlin

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(
id ?: throw ResponseStatusException(EXPECTATION_FAILED),
updatedAt,
enabled,
username,
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(","),
)