Skip to content
Snippets Groups Projects
Commit 0381bb41 authored by Gisli Nielsen's avatar Gisli Nielsen
Browse files

Added comments

parent 788bd077
No related branches found
No related tags found
No related merge requests found
...@@ -3,26 +3,32 @@ from flask import jsonify ...@@ -3,26 +3,32 @@ from flask import jsonify
from utils.helpers import sql_product_to_json from utils.helpers import sql_product_to_json
def get_product_by_id(product_id): def get_product_by_id(product_id):
# Get the specific product ID
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
WHERE product.product_id = %s WHERE product.product_id = %s
''', (product_id,)) ''', (product_id,))
products = cur.fetchall() products = cur.fetchall()
cur.close() cur.close()
# Check that the product exists
if len(products) <= 0: if len(products) <= 0:
return jsonify({"message": "Product not found"}), 404 return jsonify({"message": "Product not found"}), 404
# Check that we only have one product.
if len(products) > 1: if len(products) > 1:
return jsonify({"message": "Database error"}), 500 return jsonify({"message": "Database error"}), 500
# Use function to turn it into a json style object
jsonProduct = sql_product_to_json(products)[0] jsonProduct = sql_product_to_json(products)[0]
return jsonify(jsonProduct) return jsonify(jsonProduct)
def get_product_all(): def get_product_all():
# 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 FROM product
...@@ -31,6 +37,7 @@ def get_product_all(): ...@@ -31,6 +37,7 @@ def get_product_all():
products = cur.fetchall() products = cur.fetchall()
cur.close() cur.close()
# Turn them all into json products
jsonProducts = sql_product_to_json(products) jsonProducts = sql_product_to_json(products)
return jsonProducts return jsonProducts
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment