From 2e8634667a79cb0b060537661da1bd2f354388f8 Mon Sep 17 00:00:00 2001 From: blaz-cerpnjak Date: Wed, 20 Mar 2024 16:15:08 +0100 Subject: [PATCH] Fix getting products by restaurant id --- .../com/blazc/controller/ProductController.kt | 19 +++++++++++-------- .../com/blazc/repository/ProductRepository.kt | 7 ++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt b/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt index e07f6b3..993aa5c 100644 --- a/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt +++ b/RestaurantManagementAPI/src/main/kotlin/com/blazc/controller/ProductController.kt @@ -1,7 +1,7 @@ package com.blazc.controller -import com.blazc.repository.ProductRepository import com.blazc.model.Product +import com.blazc.repository.ProductRepository import io.smallrye.mutiny.Uni import jakarta.inject.Inject import jakarta.ws.rs.* @@ -42,17 +42,20 @@ class ProductController { return productRepository.findById(objectId) } - @GET - @Produces(MediaType.APPLICATION_JSON) - fun getAllProducts(): Uni> { - return productRepository.getAll() - } - @GET @Path("/restaurant/{id}") @Produces(MediaType.APPLICATION_JSON) fun getAllProductsByRestaurantId(@PathParam("id") restaurantId: String): Uni> { - return productRepository.getAllByRestaurantId(restaurantId) + val objectId: ObjectId + + try { + objectId = ObjectId(restaurantId) + } catch (e: Exception) { + LOG.error("Error parsing id", e) + return Uni.createFrom().failure(e) + } + + return productRepository.getAllByRestaurantId(objectId) } diff --git a/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt b/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt index 68b5a1b..b9e42c2 100644 --- a/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt +++ b/RestaurantManagementAPI/src/main/kotlin/com/blazc/repository/ProductRepository.kt @@ -4,6 +4,7 @@ import com.blazc.model.Product import io.quarkus.mongodb.panache.reactive.ReactivePanacheMongoRepository import io.smallrye.mutiny.Uni import jakarta.enterprise.context.ApplicationScoped +import org.bson.types.ObjectId @ApplicationScoped class ProductRepository: ReactivePanacheMongoRepository { @@ -12,12 +13,8 @@ class ProductRepository: ReactivePanacheMongoRepository { return persist(product) } - fun getAllByRestaurantId(restaurantId: String): Uni> { + fun getAllByRestaurantId(restaurantId: ObjectId): Uni> { return find("restaurantId", restaurantId).list() } - fun getAll(): Uni> { - return listAll() - } - } \ No newline at end of file