diff --git a/app/api/routes/product_routes.py b/app/api/routes/product_routes.py index b174dc5..7c8568d 100644 --- a/app/api/routes/product_routes.py +++ b/app/api/routes/product_routes.py @@ -20,10 +20,14 @@ def get_product_info(id: int): fields = ['name', 'price', 'image', 'image_name', 'seller'] fields_param = request.args.get('fields') - requested_fields = [] - - return abort(501) + 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']) def create_product_listing(): diff --git a/app/services/product_service.py b/app/services/product_service.py index 8450c7f..fd050e3 100644 --- a/app/services/product_service.py +++ b/app/services/product_service.py @@ -33,73 +33,47 @@ class ProductService: return result_obj, 200 except Error as e: - return {"Failed": f"Failed to fetch products. Reason: {e}"}, 400 + return {"Failed": f"Failed to fetch products. Error: {e}"}, 500 @staticmethod - def get_name(product_id: int): - db_cursor.execute(f"select name from product where product.id = {product_id}") + def get_product_info(fields: list[str], product_id: int): + try: + with db_connection.cursor(dictionary=True) as cursor: + fields_to_sql = { + "name": "product.name", + "price": "product.price_pc as price", + "image": "product.image", + "image_name": "product.image_name", + "seller": "user.displayname as seller" + } + field_sql = [] - if db_cursor.rowcount != 1: - return abort(404) + for field in fields: + field_sql.append(fields_to_sql[field]) - result = db_cursor.fetchone() - return result['name'] + select_params = ", ".join(field_sql) - @staticmethod - def get_manufacturer(product_id: int): - db_cursor.execute(f"select user.displayname as seller from product inner join user on product.seller_id = user.id where product.id = {product_id}") + if "seller" in fields: + cursor.execute(f"select {select_params} from product inner join user on user.id = product.seller_id where product.id = %s", (product_id,)) + else: + cursor.execute(f"select {select_params} from product where id = %s", (product_id,)) - if db_cursor.rowcount != 1: - return abort(404) - - result = db_cursor.fetchone() - return result['seller'] + result = cursor.fetchone() - @staticmethod - def get_price(product_id: int): - db_cursor.execute(f"select price_pc from product where product.id = {product_id}") + if cursor.rowcount != 1: + return {"Failed": "Failed to fetch product. Product likely doesn't exist"}, 400 + + result_obj = {} - if db_cursor.rowcount != 1: - return abort(404) + for field in fields: + if field == "image": # Encode image into base64 + result_obj[field] = base64.b64encode(result[field]).decode('utf-8') + else: + result_obj[field] = result[field] - result = db_cursor.fetchone() - return result['price_pc'] - - @staticmethod - def get_image(product_id: int): - db_cursor.execute(f"select image from product where product.id = {product_id}") - - if db_cursor.rowcount != 1: - return abort(404) - - result = db_cursor.fetchone() - return base64.b64encode(result['image']).decode('utf-8') - - @staticmethod - def get_image_name(product_id: int): - db_cursor.execute(f"select image_name from product where product.id = {product_id}") - - if db_cursor.rowcount != 1: - return abort(404) - - result = db_cursor.fetchone() - return result['image_name'] - - @staticmethod - def get_all_info(product_id: int): - db_cursor.execute(f"select name, user.displayname as seller, price_pc, image_name, image from product inner join user on product.seller_id = user.id where product.id = {product_id}") - - if db_cursor.rowcount != 1: - return abort(404) - - result = db_cursor.fetchone() - return { - "name": result['name'], - "seller": result['seller'], - "price": result['price_pc'], - "image_name": result['image_name'], - "image": base64.b64encode(result['image']).decode('utf-8') - } + return result_obj, 200 + except Error as e: + return {"Failed": f"Failed to fetch product info. Error: {e}"}, 500 @staticmethod def create_listing(name: str, seller_id: str, price: float, image_name: str, image):