Skip to content

Commit

Permalink
Merge pull request #44 from CSCI-GA-2820-FA23-001/qiyu_dev
Browse files Browse the repository at this point in the history
add action to change availability
  • Loading branch information
tokotority authored Nov 14, 2023
2 parents 8d99467 + 03190a1 commit 61daf9a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
32 changes: 31 additions & 1 deletion service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from flask import jsonify, request, abort, url_for
from service.common import status # HTTP Status Codes
from service.models import Product
from service.models import Product, db

# Import Flask application
from . import app
Expand Down Expand Up @@ -163,6 +163,36 @@ def read_products(product_id):
return jsonify(product.serialize()), status.HTTP_200_OK


######################################################################
# ACTION TO CHANGE A PRODUCT'S AVAILABILITY
######################################################################
@app.route("/products/<int:product_id>/change_availability", methods=["POST"])
def change_product_availability(product_id):
"""
Change Product Availability
This endpoint will change the availability of a Product based on the id specified in the path.
"""
app.logger.info(
"Request to change availability for product with id: %s", product_id
)
product = Product.find(product_id)
if not product:
abort(
status.HTTP_404_NOT_FOUND,
f"Product with id '{product_id}' was not found.",
)

new_availability = not product.available
product.available = new_availability
db.session.commit()

app.logger.info("Product availability changed for ID [%s].", product_id)
return (
jsonify({"message": f"Product availability changed to {new_availability}"}),
status.HTTP_200_OK,
)


######################################################################
# U T I L I T Y F U N C T I O N S
######################################################################
Expand Down
29 changes: 29 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,35 @@ def test_read_product_not_found(self):
response = self.client.get(f"{BASE_URL}/0")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_change_product_availability(self):
"""It should Change the availability of a single Product"""
# Manually create a product with initial availability set to True
test_product = Product(
id=123456789,
name="Test Product",
description="Test Description",
price=19.99,
available=True,
image_url="test_image.jpg",
category="TOYS",
)
# Add the product to the database
db.session.add(test_product)
db.session.commit()
# Check the initial availability
self.assertTrue(test_product.available)
# Change the availability
response = self.client.post(f"{BASE_URL}/{test_product.id}/change_availability")
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Check if the availability has changed in the database
updated_product = Product.query.get(test_product.id)
self.assertFalse(updated_product.available)

def test_change_product_availability_not_found(self):
"""It should not Change the availability of a Product that not be found"""
response = self.client.post(f"{BASE_URL}/0/change_availability")
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

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

0 comments on commit 61daf9a

Please sign in to comment.