From 7a0fa9ab9559d7841c5115d5966712aae820cc17 Mon Sep 17 00:00:00 2001 From: Knut Fineid <knutfine@stud.ntnu.no> Date: Tue, 7 May 2024 19:30:06 +0200 Subject: [PATCH] add brand to rest of product queries --- Backend/main.py | 2 +- Backend/routes/product.py | 10 +++++++--- Backend/utils/helpers.py | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Backend/main.py b/Backend/main.py index 29014f4..b43a2c6 100644 --- a/Backend/main.py +++ b/Backend/main.py @@ -1,7 +1,7 @@ from utils.application import app from routes.category import get_category from routes.home import get_home -from routes.order import post_pay_products +#from routes.order import post_pay_products #from routes.cart import get_cart from routes.login import post_login, post_logout, post_register from routes.product import get_product_by_id, get_product_all, get_products_by_search diff --git a/Backend/routes/product.py b/Backend/routes/product.py index 392c554..db2ee10 100644 --- a/Backend/routes/product.py +++ b/Backend/routes/product.py @@ -22,8 +22,10 @@ def get_product_by_id(product_id): if len(products) > 1: return jsonify({"message": "Database error"}), 500 + print(products) + # Use function to turn it into a json style object - jsonProduct = sql_product_to_json(products)[0] + jsonProduct = sql_product_to_json(products) return jsonify(jsonProduct) @@ -31,7 +33,8 @@ 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 + 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 ''') products = cur.fetchall() @@ -46,7 +49,8 @@ def get_products_by_search(search): cur = mysql.connection.cursor() # Search in product name and description 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.name LIKE %s OR product.description LIKE %s ''', ('%' + search + '%', '%' + search + '%')) diff --git a/Backend/utils/helpers.py b/Backend/utils/helpers.py index d999bf8..15b4c2e 100644 --- a/Backend/utils/helpers.py +++ b/Backend/utils/helpers.py @@ -1,13 +1,15 @@ def sql_product_to_json(products): jsonProducts = [] for product in products: - product_id, name, description, price, stock_quantity = product + product_id, name, description, price, stock_quantity, brand_name, brand_desc = product jsonProducts.append({ "product_id": product_id, "name": name, "description": description, "price": price, - "stock_quantity": stock_quantity + "stock_quantity": stock_quantity, + "brand_name": brand_name, + "brand_description": brand_desc }) return jsonProducts \ No newline at end of file -- GitLab