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

Added routes to get products and added comments

parent 190e05d3
No related branches found
No related tags found
No related merge requests found
......@@ -4,8 +4,11 @@ from routes.category import get_category
from routes.home import get_home
#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
# Routing
# Routing for homepage
@app.route('/', methods=['GET'])
def home():
return get_home()
......@@ -14,6 +17,7 @@ def home():
#def order(order_id):
# return get_order(order_id)
# Routes for getting the different categories
@app.route('/category/', methods=['GET'])
@app.route('/category/<string:category_name>', methods=['GET'])
def category(category_name=None):
......@@ -35,5 +39,19 @@ def login():
def register():
return post_register()
# Route for one specific product, or product page
@app.route('/product/<int:product_id>', methods=['GET'])
def get_product(product_id):
return get_product_by_id(product_id)
# Route for getting all products
@app.route('/product/', methods=['GET'])
def get_products():
return get_product_all()
@app.route('/product/search/<string:search>', methods=['GET'])
def search_products(search):
return get_products_by_search(search)
if __name__ == '__main__':
app.run(debug=True, port=8080)
\ No newline at end of file
from utils.application import mysql
from flask import jsonify
from utils.helpers import sql_product_to_json
def get_category(category_name=None):
# If category_name is not None, get one category by name
if (category_name is not None):
return get_category_by_name(category_name)
else:
# Construct the query to get the category
cur = mysql.connection.cursor()
cur.execute('''
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
......@@ -44,6 +46,7 @@ def get_category(category_name=None):
def get_category_by_name(category_name):
# Get the category by a name
cur = mysql.connection.cursor()
cur.execute('''
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
......@@ -54,6 +57,7 @@ def get_category_by_name(category_name):
categoryData = cur.fetchall()
# Get all of the products for that one category.
cur.execute('''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
INNER JOIN category ON category.category_id = product.category_id
......@@ -62,6 +66,7 @@ def get_category_by_name(category_name):
productData = cur.fetchall()
cur.close()
# Make sure we have a category
if len(categoryData) == 0:
return jsonify({"error": "Category not found"}), 404
......@@ -84,6 +89,7 @@ def get_category_by_name(category_name):
for row in productData:
# Extract product-info for each product
productId, productName, productDescription, productPrice, productStockQuantity = row
category["products"].append({
"productId": productId,
......
from utils.application import mysql
from flask import jsonify
from utils.helpers import sql_product_to_json
def get_product_by_id(product_id):
cur = mysql.connection.cursor()
cur.execute('''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
WHERE product.product_id = %s
''', (product_id,))
products = cur.fetchall()
cur.close()
if len(products) <= 0:
return jsonify({"message": "Product not found"}), 404
if len(products) > 1:
return jsonify({"message": "Database error"}), 500
jsonProduct = sql_product_to_json(products)[0]
return jsonify(jsonProduct)
def get_product_all():
cur = mysql.connection.cursor()
cur.execute('''
SELECT product.product_id, product.name, product.description, product.price, product.stock_quantity FROM product
''')
products = cur.fetchall()
cur.close()
jsonProducts = sql_product_to_json(products)
return jsonProducts
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
WHERE product.name LIKE %s OR product.description LIKE %s
''', ('%' + search + '%', '%' + search + '%'))
products = cur.fetchall()
cur.close()
jsonProducts = sql_product_to_json(products)
return jsonProducts
\ No newline at end of file
def sql_product_to_json(products):
jsonProducts = []
for product in products:
product_id, name, description, price, stock_quantity = product
jsonProducts.append({
"product_id": product_id,
"name": name,
"description": description,
"price": price,
"stock_quantity": stock_quantity
})
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