Hi, I’m curious if this can be rewritten with
when
block (
hasMaxRole
is extension method on my
User
data class):
if (loggedInUser.hasMaxRole(<http://Role.NO|Role.NO>_ROLE)) {
ResponseEntity(HttpStatus.FORBIDDEN)
} else if (loggedInUser.hasMaxRole(Role.USER_ROLE)) {
if (loggedInUser.id == id) {
ResponseEntity.ok(loggedInUser)
} else {
ResponseEntity(HttpStatus.FORBIDDEN)
}
} else {
userApiService.findById(id)
}
I would like something like this:
when(loggedInUser) {
hasMaxRole(<http://Role.NO|Role.NO>_ROLE) -> ResponseEntity(HttpStatus.FORBIDDEN)
hasMaxRole(Role.USER_ROLE) && (loggedInUser.id == id) -> ResponseEntity.ok(loggedInUser as Any)
hasMaxRole(Role.USER_ROLE) -> ResponseEntity(HttpStatus.FORBIDDEN)
else -> userApiService.findById(id)
}
(even with this code, I am repeating
loggedInUser
in second match since I need to call it twice in && operation)