From 0381bb41eda12a4bd0b29a240af4c1c3eb279717 Mon Sep 17 00:00:00 2001 From: Gisli Nielsen <gislion@stud.ntnu.no> Date: Tue, 7 May 2024 16:28:01 +0200 Subject: [PATCH] Added comments --- Backend/routes/product.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Backend/routes/product.py b/Backend/routes/product.py index d4e9046..392c554 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 -- GitLab