Skip to content
Snippets Groups Projects
Commit 7a0fa9ab authored by Knut Fineid's avatar Knut Fineid
Browse files

add brand to rest of product queries

parent 26996619
No related branches found
No related tags found
No related merge requests found
from utils.application import app from utils.application import app
from routes.category import get_category from routes.category import get_category
from routes.home import get_home 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.cart import get_cart
from routes.login import post_login, post_logout, post_register 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 from routes.product import get_product_by_id, get_product_all, get_products_by_search
......
...@@ -22,8 +22,10 @@ def get_product_by_id(product_id): ...@@ -22,8 +22,10 @@ def get_product_by_id(product_id):
if len(products) > 1: if len(products) > 1:
return jsonify({"message": "Database error"}), 500 return jsonify({"message": "Database error"}), 500
print(products)
# Use function to turn it into a json style object # 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) return jsonify(jsonProduct)
...@@ -31,7 +33,8 @@ def get_product_all(): ...@@ -31,7 +33,8 @@ def get_product_all():
# Get all the products # Get all the products
cur = mysql.connection.cursor() cur = mysql.connection.cursor()
cur.execute(''' 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() products = cur.fetchall()
...@@ -46,7 +49,8 @@ def get_products_by_search(search): ...@@ -46,7 +49,8 @@ def get_products_by_search(search):
cur = mysql.connection.cursor() cur = mysql.connection.cursor()
# Search in product name and description # Search in product name and description
cur.execute(''' 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 WHERE product.name LIKE %s OR product.description LIKE %s
''', ('%' + search + '%', '%' + search + '%')) ''', ('%' + search + '%', '%' + search + '%'))
......
def sql_product_to_json(products): def sql_product_to_json(products):
jsonProducts = [] jsonProducts = []
for product in products: 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({ jsonProducts.append({
"product_id": product_id, "product_id": product_id,
"name": name, "name": name,
"description": description, "description": description,
"price": price, "price": price,
"stock_quantity": stock_quantity "stock_quantity": stock_quantity,
"brand_name": brand_name,
"brand_description": brand_desc
}) })
return jsonProducts return jsonProducts
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment