Compare commits
No commits in common. "69ca94c77b62c1cb669fc18d8af098afdac1e06c" and "f259aa3993c78470cfde44cd67c40f221037ba36" have entirely different histories.
69ca94c77b
...
f259aa3993
@ -1,7 +1,7 @@
|
|||||||
from flask import jsonify, abort, request
|
from flask import jsonify, abort, request
|
||||||
from flask_jwt_extended import jwt_required, get_jwt_identity
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||||
|
|
||||||
from app.doc.cart_swag import show_cart_swagger, add_to_cart_swagger, remove_from_cart_swagger, update_count_in_cart_swagger, purchase_swagger
|
from app.doc.cart_swag import show_cart_swagger, add_to_cart_swagger
|
||||||
|
|
||||||
from flasgger import swag_from
|
from flasgger import swag_from
|
||||||
|
|
||||||
@ -35,7 +35,6 @@ def add_to_cart(product_id: int):
|
|||||||
|
|
||||||
@bp_cart.route('/remove/<int:product_id>', methods=['DELETE'])
|
@bp_cart.route('/remove/<int:product_id>', methods=['DELETE'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
@swag_from(remove_from_cart_swagger)
|
|
||||||
def remove_from_cart(product_id: int):
|
def remove_from_cart(product_id: int):
|
||||||
user_id = get_jwt_identity()
|
user_id = get_jwt_identity()
|
||||||
|
|
||||||
@ -45,7 +44,6 @@ def remove_from_cart(product_id: int):
|
|||||||
|
|
||||||
@bp_cart.route('/update/<int:product_id>', methods=['PUT'])
|
@bp_cart.route('/update/<int:product_id>', methods=['PUT'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
@swag_from(update_count_in_cart_swagger)
|
|
||||||
def update_count_in_cart(product_id: int):
|
def update_count_in_cart(product_id: int):
|
||||||
user_id = get_jwt_identity()
|
user_id = get_jwt_identity()
|
||||||
count = request.args.get('count', type=int)
|
count = request.args.get('count', type=int)
|
||||||
@ -59,7 +57,6 @@ def update_count_in_cart(product_id: int):
|
|||||||
|
|
||||||
@bp_cart.route('/purchase', methods=['GET'])
|
@bp_cart.route('/purchase', methods=['GET'])
|
||||||
@jwt_required()
|
@jwt_required()
|
||||||
@swag_from(purchase_swagger)
|
|
||||||
def purchase():
|
def purchase():
|
||||||
user_id = get_jwt_identity()
|
user_id = get_jwt_identity()
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from flask import jsonify, abort, request
|
from flask import jsonify, abort, request
|
||||||
from flask_jwt_extended import jwt_required, get_jwt_identity
|
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||||
|
|
||||||
from app.doc.product_swag import get_products_swagger, get_product_info_swagger
|
from app.doc.product_swag import get_products_swagger
|
||||||
|
|
||||||
from flasgger import swag_from
|
from flasgger import swag_from
|
||||||
|
|
||||||
@ -22,7 +22,6 @@ def get_products():
|
|||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@bp_product.route('/<int:id>', methods=['GET'])
|
@bp_product.route('/<int:id>', methods=['GET'])
|
||||||
@swag_from(get_product_info_swagger)
|
|
||||||
def get_product_info(id: int):
|
def get_product_info(id: int):
|
||||||
fields = ['name', 'price', 'image', 'image_name', 'seller']
|
fields = ['name', 'price', 'image', 'image_name', 'seller']
|
||||||
|
|
||||||
|
@ -1,19 +1,25 @@
|
|||||||
show_cart_swagger = {
|
show_cart_swagger = {
|
||||||
"tags": ["Cart"],
|
"tags": ["Cart"],
|
||||||
"security": [
|
"security":
|
||||||
|
[
|
||||||
{"JWT": []}
|
{"JWT": []}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses":
|
||||||
"200": {
|
{
|
||||||
|
"200":
|
||||||
|
{
|
||||||
"description": "Current content of user's shopping cart",
|
"description": "Current content of user's shopping cart",
|
||||||
"schema": {
|
"schema":
|
||||||
"items": {
|
{
|
||||||
|
"items":
|
||||||
|
{
|
||||||
"count": {"type": "int"},
|
"count": {"type": "int"},
|
||||||
"date_added": {"type": "string"},
|
"date_added": {"type": "string"},
|
||||||
"name": {"type": "string"},
|
"name": {"type": "string"},
|
||||||
"price_subtotal": {"type": "string"}
|
"price_subtotal": {"type": "string"}
|
||||||
},
|
},
|
||||||
"example": [
|
"example":
|
||||||
|
[
|
||||||
{
|
{
|
||||||
"count": 5,
|
"count": 5,
|
||||||
"date_added": "Fri, 08 Mar 2024 08:43:09 GMT",
|
"date_added": "Fri, 08 Mar 2024 08:43:09 GMT",
|
||||||
@ -34,10 +40,12 @@ show_cart_swagger = {
|
|||||||
|
|
||||||
add_to_cart_swagger ={
|
add_to_cart_swagger ={
|
||||||
"tags": ["Cart"],
|
"tags": ["Cart"],
|
||||||
"security": [
|
"security":
|
||||||
|
[
|
||||||
{"JWT": []}
|
{"JWT": []}
|
||||||
],
|
],
|
||||||
"parameters": [
|
"parameters":
|
||||||
|
[
|
||||||
{
|
{
|
||||||
"name": "product_id",
|
"name": "product_id",
|
||||||
"description": "ID of product to add to cart.",
|
"description": "ID of product to add to cart.",
|
||||||
@ -54,65 +62,15 @@ add_to_cart_swagger ={
|
|||||||
"required": False
|
"required": False
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses":
|
||||||
"200": {"description": "Successfully added a product to cart"},
|
|
||||||
"400": {"description": "Causes:\n- Count is < 1"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
remove_from_cart_swagger = {
|
|
||||||
"tags": ["Cart"],
|
|
||||||
"security": [{"JWT": []}],
|
|
||||||
"parameters": [
|
|
||||||
{
|
{
|
||||||
"name": "product_id",
|
"200":
|
||||||
"in": "path",
|
|
||||||
"type": "integer",
|
|
||||||
"description": "ID of the product to be removed from the cart",
|
|
||||||
"required": True
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {"description": "Successfully removed item from the cart"},
|
|
||||||
"400": {"description": "Bad Request - Invalid input"},
|
|
||||||
"500": {"description": "Internal Server Error"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
update_count_in_cart_swagger = {
|
|
||||||
"tags": ["Cart"],
|
|
||||||
"security": [{"JWT": []}],
|
|
||||||
"description": "Updates the count of products in the user's cart. If the count is less than or equal to 0, the product will be removed from the cart.",
|
|
||||||
"parameters": [
|
|
||||||
{
|
{
|
||||||
"name": "product_id",
|
"description": "Successfully added a product to cart"
|
||||||
"in": "path",
|
|
||||||
"type": "integer",
|
|
||||||
"description": "ID of the product to update in the cart",
|
|
||||||
"required": True
|
|
||||||
},
|
},
|
||||||
|
"400":
|
||||||
{
|
{
|
||||||
"name": "count",
|
"description": "Causes:\n- Count is < 1"
|
||||||
"in": "query",
|
|
||||||
"type": "integer",
|
|
||||||
"description": "New count of the product in the cart",
|
|
||||||
"required": True
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {"description": "Successfully updated item count in the cart"},
|
|
||||||
"400": {"description": "Bad Request - Invalid input"},
|
|
||||||
"500": {"description": "Internal Server Error"}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
purchase_swagger = {
|
|
||||||
"tags": ["Cart"],
|
|
||||||
"security": [{"JWT": []}],
|
|
||||||
"description": "Purchases the contents of the user's cart. This action creates a new purchase, moves items from the cart to the purchase history, and clears the cart.",
|
|
||||||
"responses": {
|
|
||||||
"200": {"description": "Successfully completed the purchase"},
|
|
||||||
"400": {"description": "Bad Request - Invalid input or cart is empty"},
|
|
||||||
"500": {"description": "Internal Server Error"}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -19,28 +19,3 @@ get_products_swagger = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get_product_info_swagger = {
|
|
||||||
"tags": ["Products"],
|
|
||||||
"parameters": [
|
|
||||||
{
|
|
||||||
"name": "id",
|
|
||||||
"in": "path",
|
|
||||||
"type": "integer",
|
|
||||||
"description": "ID of the product to fetch information for",
|
|
||||||
"required": True
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "fields",
|
|
||||||
"in": "query",
|
|
||||||
"type": "string",
|
|
||||||
"description": "Comma-separated list of fields to include in the response",
|
|
||||||
"required": False
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"responses": {
|
|
||||||
"200": {"description": "Successfully fetched product information"},
|
|
||||||
"400": {"description": "Bad Request - Invalid input or product doesn't exist"},
|
|
||||||
"500": {"description": "Internal Server Error"}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -62,7 +62,7 @@ class CartService:
|
|||||||
return {"Success": "Successfully added to cart"}, 200
|
return {"Success": "Successfully added to cart"}, 200
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to update item count in cart. Reason: {e}"}, 500
|
return {"Failed": f"Failed to update item count in cart. Reason: {e}"}, 400
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def delete_from_cart(user_id: str, product_id: int) -> Tuple[Union[dict, str], int]:
|
def delete_from_cart(user_id: str, product_id: int) -> Tuple[Union[dict, str], int]:
|
||||||
@ -85,7 +85,7 @@ class CartService:
|
|||||||
|
|
||||||
return {"Success": "Successfully removed item from cart"}, 200
|
return {"Success": "Successfully removed item from cart"}, 200
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to remove item from cart. Reason: {e}"}, 500
|
return {"Failed": f"Failed to remove item from cart. Reason: {e}"}, 400
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -119,7 +119,7 @@ class CartService:
|
|||||||
return results, 200
|
return results, 200
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to load cart. Reason: {e}"}, 500
|
return {"Failed": f"Failed to load cart. Reason: {e}"}, 400
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@ -176,7 +176,7 @@ class CartService:
|
|||||||
|
|
||||||
# clear cart
|
# clear cart
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to load cart. Reason: {e}"}, 500
|
return {"Failed": f"Failed to load cart. Reason: {e}"}, 400
|
||||||
|
|
||||||
return {"Success": "Successfully purchased"}, 200
|
return {"Success": "Successfully purchased"}, 200
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user