Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit decrease item quantity in shopcart #68

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,53 @@
return jsonify(item.serialize()), status.HTTP_200_OK


######################################################################
# DECREASE ITEM IN A SHOPCART
######################################################################


@app.route(
"/shopcarts/<int:shopcart_id>/items/<int:item_id>/decrement", methods=["PUT"]
)
def decrement_item_quantity(shopcart_id, item_id):
"""
Decrement the quantity of an item in a Shopcart

This endpoint will decrement the quantity of an item in a Shopcart based the body that is posted
"""

app.logger.info(
"Request to decrement the quantity of item with id %d in a shopcart with id: %d",
item_id,
shopcart_id,
)

shopcart = Shopcart.find(shopcart_id)
if not shopcart:
error(

Check warning on line 418 in service/routes.py

View check run for this annotation

Codecov / codecov/patch

service/routes.py#L418

Added line #L418 was not covered by tests
status.HTTP_404_NOT_FOUND,
f"Shopcart with id: '{shopcart_id}' was not found.",
)

item = Item.find(item_id)

if not item:
error(

Check warning on line 426 in service/routes.py

View check run for this annotation

Codecov / codecov/patch

service/routes.py#L426

Added line #L426 was not covered by tests
status.HTTP_404_NOT_FOUND,
f"Item with id: '{item_id}' was not found in shopcart with id: '{shopcart_id}'",
)

item._quantity -= 1

item.update()

app.logger.info(
"Item with id %d in shopcart with id %d updated.", item_id, shopcart_id
)

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


######################################################################
# U T I L I T Y F U N C T I O N S
######################################################################
Expand Down
35 changes: 35 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,41 @@ def test_increment_quantity_by_one(self):
)
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)

def test_decrement_quantity_by_one(self):
"""It should decrement quantity by one"""
# add four items to the shopcart
shopcart = ShopcartFactory()
shopcart.create()
item = ItemFactory(shopcart=shopcart)
item.create()
# let's commit the newly created item to the DB
initial_quantity = item.quantity
resp = self.client.post(
f"{BASE_URL}/{shopcart.id}/items", json=item.serialize()
)
self.assertEqual(resp.status_code, status.HTTP_201_CREATED)

# let's get the item
resp = self.client.get(f"{BASE_URL}/{shopcart.id}/items")
self.assertEqual(resp.status_code, status.HTTP_200_OK)

resp = self.client.put(
f"{BASE_URL}/{shopcart.id}/items/{item.id}/decrement", json=item.serialize()
)
new_quantity = item.quantity
self.assertEqual(new_quantity, initial_quantity - 1)
self.assertEqual(resp.status_code, status.HTTP_200_OK)

resp = self.client.put(
f"{BASE_URL}/-1/items/{item.id}/decrement", json=item.serialize()
)
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)

resp = self.client.put(
f"{BASE_URL}/{shopcart.id}/items/-1/decrement", json=item.serialize()
)
self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)


######################################################################
# T E S T S A D P A T H S
Expand Down
Loading