diff --git a/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt b/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt index 9d6468d..f770801 100644 --- a/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt +++ b/src/main/kotlin/com/ray650128/extension/ResponseExtension.kt @@ -20,10 +20,10 @@ suspend fun ApplicationCall.sendSuccess(data: T?) { ) } -suspend fun ApplicationCall.sendUnauthorized(errorResponse: ErrorResponse) { +suspend fun ApplicationCall.sendUnauthorized() { this.respond( status = HttpStatusCode.Unauthorized, - message = errorResponse + message = ErrorResponse.UNAUTHORIZED_RESPONSE ) } @@ -34,9 +34,9 @@ suspend fun ApplicationCall.sendBadRequest(errorResponse: ErrorResponse) { ) } -suspend fun ApplicationCall.sendNotFound(errorResponse: ErrorResponse) { +suspend fun ApplicationCall.sendNotFound() { this.respond( status = HttpStatusCode.NotFound, - message = errorResponse + message = ErrorResponse.NOT_FOUND_RESPONSE ) } diff --git a/src/main/kotlin/com/ray650128/plugins/UserRouting.kt b/src/main/kotlin/com/ray650128/plugins/UserRouting.kt index 5a7fb77..ceb2299 100644 --- a/src/main/kotlin/com/ray650128/plugins/UserRouting.kt +++ b/src/main/kotlin/com/ray650128/plugins/UserRouting.kt @@ -50,18 +50,18 @@ fun Application.configureUserRouting() { } call.sendSuccess(LoginResult(request.account, token!!)) } else { - call.sendUnauthorized(ErrorResponse("Account or Password wrong.")) + call.sendBadRequest(ErrorResponse("Account or Password wrong.")) } } authenticate { post("/logout") { val account = call.authentication.principal()?.account ?: run { - call.sendUnauthorized(ErrorResponse("Unauthorized")) + call.sendUnauthorized() return@post } val user = service.findByAccount(account) ?: run { - call.sendUnauthorized(ErrorResponse("Unauthorized")) + call.sendUnauthorized() return@post } user.apply { @@ -83,7 +83,7 @@ fun Application.configureUserRouting() { val id = call.parameters["id"].toString() service.findById(id) ?.let { foundPerson -> call.respond(foundPerson.toDto()) } - ?: call.sendNotFound(ErrorResponse.NOT_FOUND_RESPONSE) + ?: call.sendNotFound() } get("/search") {