diff --git a/Backend/routes/product.py b/Backend/routes/product.py index d4e90465bc3259352e7e91f2804603f8fd0c79c3..392c554df1b138a80e8d7e4a3734350d94a83356 100644 --- a/Backend/routes/product.py +++ b/Backend/routes/product.py @@ -3,26 +3,32 @@ from flask import jsonify from utils.helpers import sql_product_to_json def get_product_by_id(product_id): + # Get the specific product ID cur = mysql.connection.cursor() cur.execute(''' - SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product + SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity, brand.name, brand.description + FROM product INNER JOIN brand ON product.brand_id = brand.brand_id WHERE product.product_id = %s ''', (product_id,)) products = cur.fetchall() cur.close() + # Check that the product exists if len(products) <= 0: return jsonify({"message": "Product not found"}), 404 + # Check that we only have one product. if len(products) > 1: return jsonify({"message": "Database error"}), 500 + # Use function to turn it into a json style object jsonProduct = sql_product_to_json(products)[0] return jsonify(jsonProduct) def get_product_all(): + # Get all the products cur = mysql.connection.cursor() cur.execute(''' SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product @@ -31,6 +37,7 @@ def get_product_all(): products = cur.fetchall() cur.close() + # Turn them all into json products jsonProducts = sql_product_to_json(products) return jsonProducts