from flask import jsonify, abort, request from flask_jwt_extended import jwt_required, get_jwt_identity from app.doc.product_swag import get_products_swagger, get_product_info_swagger, create_product_swagger from flasgger import swag_from from app.api import bp_product from app.services.product_service import ProductService @bp_product.route('', methods=['GET']) @swag_from(get_products_swagger) def get_products(): page = request.args.get('page', default=0, type=int) if page < 0: return abort(400) result, status_code = ProductService.get_products(page) return result, status_code @bp_product.route('/', methods=['GET']) @swag_from(get_product_info_swagger) def get_product_info(id: int): fields = ['name', 'price', 'image', 'image_name', 'seller'] fields_param = request.args.get('fields') fields_param_list = fields_param.split(',') if fields_param else fields common_fields = list(set(fields) & set(fields_param_list)) result, status_code = ProductService.get_product_info(common_fields, id) return result, status_code @bp_product.route('/create', methods=['POST']) @swag_from(create_product_swagger) @jwt_required() def create_product_listing(): user_id = get_jwt_identity() name = request.json.get('name') price = request.json.get('price') if name is None or price is None: return abort(400) float_price = float(price) if not isinstance(float_price, float): return abort(400) result, status_code = ProductService.create_listing(user_id, name, float_price) return result, status_code