Skip to content

Commit

Permalink
Fix getting products by restaurant id
Browse files Browse the repository at this point in the history
  • Loading branch information
blaz-cerpnjak committed Mar 20, 2024
1 parent 263ccd6 commit 2e86346
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -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.*
Expand Down Expand Up @@ -42,17 +42,20 @@ class ProductController {
return productRepository.findById(objectId)
}

@GET
@Produces(MediaType.APPLICATION_JSON)
fun getAllProducts(): Uni<List<Product>> {
return productRepository.getAll()
}

@GET
@Path("/restaurant/{id}")
@Produces(MediaType.APPLICATION_JSON)
fun getAllProductsByRestaurantId(@PathParam("id") restaurantId: String): Uni<List<Product>> {
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)
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Product> {
Expand All @@ -12,12 +13,8 @@ class ProductRepository: ReactivePanacheMongoRepository<Product> {
return persist(product)
}

fun getAllByRestaurantId(restaurantId: String): Uni<List<Product>> {
fun getAllByRestaurantId(restaurantId: ObjectId): Uni<List<Product>> {
return find("restaurantId", restaurantId).list()
}

fun getAll(): Uni<List<Product>> {
return listAll()
}

}

0 comments on commit 2e86346

Please sign in to comment.