forked from ITStudio_OUC/itstudio_manager
refactor: messageHandler
This commit is contained in:
+4
-11
@@ -5,6 +5,7 @@ import club.itstudio.itmanager.entity.enums.EmailType
|
||||
import club.itstudio.itmanager.entity.vo.request.EmailRegisterVO
|
||||
import club.itstudio.itmanager.entity.vo.request.PasswordResetVO
|
||||
import club.itstudio.itmanager.service.v1.AuthorizationService
|
||||
import club.itstudio.itmanager.utils.Const
|
||||
import jakarta.validation.Valid
|
||||
import jakarta.validation.constraints.Email
|
||||
import jakarta.validation.constraints.NotBlank
|
||||
@@ -31,21 +32,21 @@ class AuthorizeController(
|
||||
logger.info("Trying to ask code")
|
||||
val ip = exchange.request.remoteAddress?.address?.hostAddress ?: "unknown"
|
||||
return service.askEmailVerifyCode(type, email, ip)
|
||||
.map(::messageHandler)
|
||||
.map(Const::messageHandler)
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
fun emailRegister(@RequestBody @Valid vo: EmailRegisterVO): Mono<ApiResponse<Any?>> {
|
||||
logger.info("Trying to register")
|
||||
return service.registerEmailAccount(vo)
|
||||
.map(::messageHandler)
|
||||
.map(Const::messageHandler)
|
||||
}
|
||||
|
||||
@PostMapping("/reset")
|
||||
fun emailResetPassword(@RequestBody @Valid vo: PasswordResetVO): Mono<ApiResponse<Any?>> {
|
||||
logger.info("Trying to reset password")
|
||||
return service.resetEmailAccountPassword(vo)
|
||||
.map(::messageHandler)
|
||||
.map(Const::messageHandler)
|
||||
}
|
||||
|
||||
@GetMapping("/logout")
|
||||
@@ -68,12 +69,4 @@ class AuthorizeController(
|
||||
?.map { ApiResponse.success(it) }
|
||||
?: Mono.just(ApiResponse.failure(401, null, "Please login with password."))
|
||||
}
|
||||
|
||||
private fun messageHandler(wrongMessage: String): ApiResponse<Any?> {
|
||||
return if (wrongMessage.isBlank()) {
|
||||
ApiResponse.success()
|
||||
} else {
|
||||
ApiResponse.failure(400, null, wrongMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package club.itstudio.itmanager.utils
|
||||
|
||||
import club.itstudio.itmanager.entity.ApiResponse
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
object Const {
|
||||
@@ -14,4 +15,13 @@ object Const {
|
||||
|
||||
const val INTERNAL_ERROR_STR = "something went wrong. Please contact the administrator."
|
||||
val INTERNAL_ERROR_MONO = Mono.just(INTERNAL_ERROR_STR)
|
||||
|
||||
// for Controllers
|
||||
fun messageHandler(wrongMessage: String): ApiResponse<Any?> {
|
||||
return if (wrongMessage.isBlank()) {
|
||||
ApiResponse.success()
|
||||
} else {
|
||||
ApiResponse.failure(400, null, wrongMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user