Skip to content

Commit

Permalink
Merge pull request #63 from CSCI-GA-2820-SP24-003/search-items-by-pro…
Browse files Browse the repository at this point in the history
…duct-id

Search items by product
  • Loading branch information
cisc0f authored Apr 2, 2024
2 parents 40f2465 + 38db5dc commit 184631c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions service/models/shop_cart_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ def find_by_name(cls, name):
logger.info("Processing name query for %s ...", name)
return cls.query.filter(cls.name == name).first()

@classmethod
def find_by_product_id(cls, product_id):
"""Returns all ShopCart Items with the given product_id
Args:
product_id (string): the product_id of the ShopCart Item you want to match
"""
logger.info("Processing lookup for product_id: %s", product_id)
return cls.query.filter(cls.product_id == product_id).first()

# @classmethod
# def find_by_shopcart_id(cls, shopcart_id):
# """Finds a ShopCart by its ID
Expand Down
33 changes: 33 additions & 0 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,39 @@ def get_shopcart_items(shopcart_id, item_id):
return jsonify(item.serialize()), status.HTTP_200_OK


######################################################################
# RETRIEVE AN ITEM FROM SHOPCART BY PRODUCT ID
######################################################################
@app.route("/shopcarts/<int:shopcart_id>/products/<int:product_id>", methods=["GET"])
def get_shopcart_items_by_product_id(shopcart_id, product_id):
"""
Get an Item
This endpoint returns just an item using the product id
"""
app.logger.info(
"Request to retrieve Item %s for ShopCart id: %s", (product_id, shopcart_id)
)

# Search for the shopcart
shopcart = ShopCart.find(shopcart_id)
if not shopcart:
abort(
status.HTTP_404_NOT_FOUND,
f"ShopCart with ID '{shopcart_id}' could not be found",
)

# See if the item exists and abort if it doesn't
item = ShopCartItem.find_by_product_id(product_id)
if not item:
abort(
status.HTTP_404_NOT_FOUND,
f"Product with id '{product_id}' could not be found.",
)

return jsonify(item.serialize()), status.HTTP_200_OK


######################################################################
# DELETE AN ITEM FROM SHOPCART
######################################################################
Expand Down
31 changes: 31 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ def test_get_shopcart_item_when_no_shopcart(self):
)
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)

def test_get_shopcart_item_by_product_id(self):
"""It should Get an item from a shopcart by product id"""
# create a known item
shopcart = self._create_shopcarts(1)[0]
item = ShopCartItemFactory()
resp = self.client.post(
f"{BASE_URL}/{shopcart.id}/items",
json=item.serialize(),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

data = resp.get_json()
logging.debug(data)
product_id = data["product_id"]

# retrieve it back
resp = self.client.get(
f"{BASE_URL}/{shopcart.id}/products/{product_id}",
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)

data = resp.get_json()
logging.debug(data)
self.assertEqual(data["shop_cart_id"], shopcart.id)
self.assertEqual(data["name"], item.name)
self.assertEqual(data["product_id"], item.product_id)
self.assertEqual(data["quantity"], item.quantity)
self.assertEqual(data["price"], str(item.price))

def test_delete_shopcart_item(self):
"""It should delete a shopcart item"""
shopcart = self._create_shopcarts(1)[0]
Expand Down

0 comments on commit 184631c

Please sign in to comment.