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.EmailRegisterVO
|
||||||
import club.itstudio.itmanager.entity.vo.request.PasswordResetVO
|
import club.itstudio.itmanager.entity.vo.request.PasswordResetVO
|
||||||
import club.itstudio.itmanager.service.v1.AuthorizationService
|
import club.itstudio.itmanager.service.v1.AuthorizationService
|
||||||
|
import club.itstudio.itmanager.utils.Const
|
||||||
import jakarta.validation.Valid
|
import jakarta.validation.Valid
|
||||||
import jakarta.validation.constraints.Email
|
import jakarta.validation.constraints.Email
|
||||||
import jakarta.validation.constraints.NotBlank
|
import jakarta.validation.constraints.NotBlank
|
||||||
@@ -31,21 +32,21 @@ class AuthorizeController(
|
|||||||
logger.info("Trying to ask code")
|
logger.info("Trying to ask code")
|
||||||
val ip = exchange.request.remoteAddress?.address?.hostAddress ?: "unknown"
|
val ip = exchange.request.remoteAddress?.address?.hostAddress ?: "unknown"
|
||||||
return service.askEmailVerifyCode(type, email, ip)
|
return service.askEmailVerifyCode(type, email, ip)
|
||||||
.map(::messageHandler)
|
.map(Const::messageHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
fun emailRegister(@RequestBody @Valid vo: EmailRegisterVO): Mono<ApiResponse<Any?>> {
|
fun emailRegister(@RequestBody @Valid vo: EmailRegisterVO): Mono<ApiResponse<Any?>> {
|
||||||
logger.info("Trying to register")
|
logger.info("Trying to register")
|
||||||
return service.registerEmailAccount(vo)
|
return service.registerEmailAccount(vo)
|
||||||
.map(::messageHandler)
|
.map(Const::messageHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/reset")
|
@PostMapping("/reset")
|
||||||
fun emailResetPassword(@RequestBody @Valid vo: PasswordResetVO): Mono<ApiResponse<Any?>> {
|
fun emailResetPassword(@RequestBody @Valid vo: PasswordResetVO): Mono<ApiResponse<Any?>> {
|
||||||
logger.info("Trying to reset password")
|
logger.info("Trying to reset password")
|
||||||
return service.resetEmailAccountPassword(vo)
|
return service.resetEmailAccountPassword(vo)
|
||||||
.map(::messageHandler)
|
.map(Const::messageHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/logout")
|
@GetMapping("/logout")
|
||||||
@@ -68,12 +69,4 @@ class AuthorizeController(
|
|||||||
?.map { ApiResponse.success(it) }
|
?.map { ApiResponse.success(it) }
|
||||||
?: Mono.just(ApiResponse.failure(401, null, "Please login with password."))
|
?: 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
|
package club.itstudio.itmanager.utils
|
||||||
|
|
||||||
|
import club.itstudio.itmanager.entity.ApiResponse
|
||||||
import reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
|
|
||||||
object Const {
|
object Const {
|
||||||
@@ -14,4 +15,13 @@ object Const {
|
|||||||
|
|
||||||
const val INTERNAL_ERROR_STR = "something went wrong. Please contact the administrator."
|
const val INTERNAL_ERROR_STR = "something went wrong. Please contact the administrator."
|
||||||
val INTERNAL_ERROR_MONO = Mono.just(INTERNAL_ERROR_STR)
|
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