swag-shop/app/api/routes/cart_routes.py

80 lines
1.9 KiB
Python
Raw Permalink Normal View History

2024-03-08 14:04:40 +01:00
from flask import jsonify, abort, request
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,
)
2024-03-12 20:12:19 +01:00
from flasgger import swag_from
2024-03-08 14:04:40 +01:00
from app.api import bp_cart
from app.services.cart_service import CartService
@bp_cart.route("", methods=["GET"])
2024-03-10 16:24:36 +01:00
@jwt_required()
2024-03-12 20:12:19 +01:00
@swag_from(show_cart_swagger)
2024-03-10 16:24:36 +01:00
def show_cart():
user_id = get_jwt_identity()
result, status_code = CartService.show_cart(user_id)
2024-03-10 16:24:36 +01:00
return result, status_code
2024-03-10 16:24:36 +01:00
@bp_cart.route("/add/<int:product_id>", methods=["PUT"])
2024-03-08 14:04:40 +01:00
@jwt_required()
2024-03-12 20:12:19 +01:00
@swag_from(add_to_cart_swagger)
2024-03-08 14:04:40 +01:00
def add_to_cart(product_id: int):
user_id = get_jwt_identity()
count = request.args.get("count", default=1, type=int)
if count < 1:
return abort(400)
2024-03-10 16:24:36 +01:00
result, status_code = CartService.add_to_cart(user_id, product_id, count)
2024-03-08 14:04:40 +01:00
return result, status_code
2024-03-10 16:24:36 +01:00
@bp_cart.route("/remove/<int:product_id>", methods=["DELETE"])
2024-03-10 16:24:36 +01:00
@jwt_required()
2024-03-13 21:40:55 +01:00
@swag_from(remove_from_cart_swagger)
2024-03-10 16:24:36 +01:00
def remove_from_cart(product_id: int):
user_id = get_jwt_identity()
result, status_code = CartService.delete_from_cart(user_id, product_id)
2024-03-10 16:24:36 +01:00
return result, status_code
2024-03-10 16:24:36 +01:00
@bp_cart.route("/update/<int:product_id>", methods=["PUT"])
2024-03-10 16:24:36 +01:00
@jwt_required()
2024-03-13 21:40:55 +01:00
@swag_from(update_count_in_cart_swagger)
2024-03-10 16:24:36 +01:00
def update_count_in_cart(product_id: int):
user_id = get_jwt_identity()
count = request.args.get("count", type=int)
if not count:
return abort(400)
2024-03-10 16:24:36 +01:00
result, status_code = CartService.update_count(user_id, product_id, count)
2024-03-10 16:24:36 +01:00
return result, status_code
2024-03-10 16:24:36 +01:00
@bp_cart.route("/purchase", methods=["GET"])
2024-03-10 16:24:36 +01:00
@jwt_required()
2024-03-13 21:40:55 +01:00
@swag_from(purchase_swagger)
2024-03-10 16:24:36 +01:00
def purchase():
user_id = get_jwt_identity()
2024-03-10 16:24:36 +01:00
result, status_code = CartService.purchase(user_id)
2024-03-10 16:24:36 +01:00
return result, status_code