Skip to content

Commit

Permalink
-Multiple Products Endpoint Imp (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhubaibKhan4 committed Jun 21, 2024
1 parent c3e32ff commit 9ddf864
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class ProductRepository : ProductDao {
}
}

suspend fun getProductsByMultipleIds(ids: List<Long>): List<Product>? {
return DatabaseFactory.dbQuery {
ProductTable.select { ProductTable.id inList ids }
.mapNotNull { rowToResult(it) }
}
}

override suspend fun getAllProducts(): List<Product>? {
return DatabaseFactory.dbQuery {
Expand Down
25 changes: 25 additions & 0 deletions src/main/kotlin/com/example/plugins/Routes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,31 @@ fun Route.products(
}
}

get("v1/products/multiple") {
val idsString = call.request.queryParameters["ids"]
if (idsString.isNullOrBlank()) {
call.respond(HttpStatusCode.BadRequest, "No product IDs provided")
return@get
}

val ids = idsString.split(",").mapNotNull { it.toLongOrNull() }
if (ids.isEmpty()) {
call.respond(HttpStatusCode.BadRequest, "Invalid product IDs provided")
return@get
}

try {
val products = db.getProductsByMultipleIds(ids)
if (products.isNullOrEmpty()) {
call.respond(HttpStatusCode.NotFound, "No products found for the provided IDs")
} else {
call.respond(HttpStatusCode.OK, products)
}
} catch (e: Exception) {
call.respond(HttpStatusCode.InternalServerError, "Error while fetching products: ${e.message}")
}
}

put("v1/products/{id}") {
val id = call.parameters["id"]?.toLongOrNull() ?: return@put call.respondText(
text = "Invalid or Missing Product ID",
Expand Down

0 comments on commit 9ddf864

Please sign in to comment.