|
| 1 | +package com.example.route |
| 2 | + |
| 3 | +import com.example.dto.UserLoginDto |
| 4 | +import com.example.dto.UserRegisterDto |
| 5 | +import com.example.service.impl.userService |
| 6 | +import io.ktor.http.* |
| 7 | +import io.ktor.server.application.* |
| 8 | +import io.ktor.server.request.* |
| 9 | +import io.ktor.server.response.* |
| 10 | +import io.ktor.server.routing.* |
| 11 | +import java.util.UUID |
| 12 | + |
| 13 | +fun Application.userRoute() { |
| 14 | + routing { |
| 15 | + post("/register") { |
| 16 | + val userRegisterDto = call.receive<UserRegisterDto>() |
| 17 | + userService.register(userRegisterDto).fold( |
| 18 | + onSuccess = { call.respond(HttpStatusCode.Created) }, |
| 19 | + onFailure = { throwable -> |
| 20 | + call.respond( |
| 21 | + status = HttpStatusCode.BadRequest, |
| 22 | + message = throwable.message ?: "Something went wrong" |
| 23 | + ) |
| 24 | + } |
| 25 | + ) |
| 26 | + } |
| 27 | + |
| 28 | + post("/login") { |
| 29 | + val userLoginDto: UserLoginDto = call.receive() |
| 30 | + userService.login(userLoginDto).fold( |
| 31 | + onSuccess = { userId -> call.respond(message = userId) }, |
| 32 | + onFailure = { throwable -> |
| 33 | + call.respond( |
| 34 | + status = HttpStatusCode.Unauthorized, |
| 35 | + message = throwable.message ?: "Something went wrong" |
| 36 | + ) |
| 37 | + } |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + get("/profile/{userId}") { |
| 42 | + val userId = call.parameters["userId"]?.let { UUID.fromString(it) } |
| 43 | + if (userId == null) { |
| 44 | + call.respond( |
| 45 | + status = HttpStatusCode.BadRequest, |
| 46 | + message = "Invalid user id" |
| 47 | + ) |
| 48 | + return@get |
| 49 | + } |
| 50 | + userService.getProfile(userId).fold( |
| 51 | + onSuccess = { userDto -> call.respond(userDto) }, |
| 52 | + onFailure = { throwable -> |
| 53 | + call.respond( |
| 54 | + status = HttpStatusCode.NotFound, |
| 55 | + message = throwable.message ?: "Something went wrong" |
| 56 | + ) |
| 57 | + } |
| 58 | + ) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments