Hey guys I was wondering if this is the place to a...
# ktor
k
Hey guys I was wondering if this is the place to ask about mongo coroutine driver to be used with Ktor? I'm trying to have a better understanding on how to make the flow results a bit cleaner instead of trying to send an empty list if something is not found and handle that to throw the 404 error
UserRouting
Copy code
userRepository.userBy(id)
    .collect { users ->
        if (users.isEmpty()) {
            call.respond(
                status = HttpStatusCode.NotFound,
                message = CallResponse(error = true, message = "User not found")
            )
        } else {
            call.respond(status = HttpStatusCode.OK, message = users)
        }
    }
The database Flow
Copy code
fun obtainUser(id: String): Flow<List<User>> = flow { emit(userCollection.find(filter = Filters.eq(IdField, id)).toList()) }
if I don't conver it toList it won't let me handle the error which is going to be 404 (which is fine), but I'm building all errors to send the
CallResponse
object as part of the response I tried with the catch operator and was ignored and even wrapping the routing with a try catch didn't do the work either
I was hoping to get something more like
Copy code
userRepository.userBy(id)
    .catch { TODO() }
    .collect { user ->
        call.respond(status = HttpStatusCode.OK, message = users)
    }
g
Adjust to your code and test if it works for you. @kagomez
Copy code
fun Routing.userRoutes(userRepository: UserRepository) {
    get("/users/{id}") {
        val userId = call.parameters["id"]
        if (userId != null) {
            userRepository.obtainUser(userId)
                .collect { users ->
                    if (users.isEmpty()) {
                        call.respond(
                            status = HttpStatusCode.NotFound,
                            message = CallResponse(error = true, message = "User not found")
                        )
                    } else {
                        call.respond(status = HttpStatusCode.OK, message = users)
                    }
                }
        } else {
            call.respond(HttpStatusCode.BadRequest, "Invalid user ID")
        }
    }
}
👍 1
k
I actually check for the parameter like this
Copy code
val id: String = call.parameters["id"] ?: return@get call.respond(
    status = HttpStatusCode.BadRequest,
    message = CallResponse(error = true, message = "Id parameter not found")
)
g
Copy code
fun Routing.userRoutes(userRepository: UserRepository) {
    get("/users/{id}") {
        val id: String = call.parameters["id"] ?: return@get call.respond(
            status = HttpStatusCode.BadRequest,
            message = CallResponse(error = true, message = "Id parameter not found")
        )
        
        userRepository.obtainUser(id)
            .collect { users ->
                if (users.isEmpty()) {
                    call.respond(
                        status = HttpStatusCode.NotFound,
                        message = CallResponse(error = true, message = "User not found")
                    )
                } else {
                    call.respond(status = HttpStatusCode.OK, message = users)
                }
            }
    }
}