Skip to content
Snippets Groups Projects
Commit 6d82378f authored by Torbjørn Halland's avatar Torbjørn Halland
Browse files

Merge branch 'main' of git.gvk.idi.ntnu.no:steffemar/idatg2204-project

parents cfdffb5f 11a168ce
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.order import get_order #from routes.order import get_order
from routes.home import get_home from routes.home import get_home
#from routes.cart import get_cart #from routes.cart import get_cart
...@@ -14,24 +14,24 @@ def home(): ...@@ -14,24 +14,24 @@ def home():
#def order(order_id): #def order(order_id):
# return get_order(order_id) # return get_order(order_id)
#@app.route('/category', methods=['GET']) @app.route('/category/', methods=['GET'])
#@app.route('/category/<string:category_name>', methods=['GET']) @app.route('/category/<string:category_name>', methods=['GET'])
#def category(category_name): def category(category_name=None):
# return routes.category.get_category(category_name) return get_category(category_name)
#@app.route('/cart', methods=['GET']) #@app.route('/cart', methods=['GET'])
#def cart(): #def cart():
# return get_cart() # return get_cart()
@app.route('/logout', methods=['POST']) @app.route('/logout/', methods=['POST'])
def logout(): def logout():
return post_logout() return post_logout()
@app.route('/login', methods=['POST']) @app.route('/login/', methods=['POST'])
def login(): def login():
return post_login() return post_login()
@app.route('/register', methods=['POST']) @app.route('/register/', methods=['POST'])
def register(): def register():
return post_register() return post_register()
......
from main import mysql from utils.application import mysql
from flask import jsonify from flask import jsonify
def get_category(category_name=None): def get_category(category_name=None):
if category_name is 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:
cur = mysql.connection.cursor() cur = mysql.connection.cursor()
cur.execute('''SELECT category.name FROM category''') cur.execute('''
SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
LEFT JOIN sub_category_in_category ON sub_category_in_category.parent_category_id = category.category_id
LEFT JOIN category AS sub_category ON sub_category.category_id = sub_category_in_category.sub_category_id
''')
data = cur.fetchall() data = cur.fetchall()
cur.close() cur.close()
return jsonify(data)
else: # If no data is found, return an empty list
categories = []
for row in data:
#Extract data from row
categoryName, categoryDescription, subCategoryName, subCategoryDescription = row
# Check if category exists already in category
category = next((x for x in categories if x["categoryName"] == categoryName), None)
# If it does not exist, create it
if category is None:
category = {
"categoryName": categoryName,
"categoryDescription": categoryDescription,
"subCategories": []
}
categories.append(category)
#Always append subcategories extracted from row
if (subCategoryName is not None):
category["subCategories"].append({
"subCategoryName": subCategoryName,
"subCategoryDescription": subCategoryDescription
})
return jsonify(categories)
def get_category_by_name(category_name):
cur = mysql.connection.cursor() cur = mysql.connection.cursor()
cur.execute('''SELECT category.name FROM category WHERE name = %s''', (category_name,)) cur.execute('''
data = cur.fetchall() SELECT category.name, category.description, sub_category.name, sub_category.description FROM category
LEFT JOIN sub_category_in_category ON sub_category_in_category.parent_category_id = category.category_id
LEFT JOIN category AS sub_category ON sub_category.category_id = sub_category_in_category.sub_category_id
WHERE category.name = %s
''', (category_name,))
categoryData = cur.fetchall()
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
WHERE category.name = %s
''', (category_name,))
productData = cur.fetchall()
cur.close() cur.close()
return jsonify(data)
\ No newline at end of file if len(categoryData) == 0:
return jsonify({"error": "Category not found"}), 404
# Extract data from first row to create category object
categoryName, categoryDescription, subCategoryName, subCategoryDescription = categoryData[0]
category = {
"categoryName": categoryName,
"categoryDescription": categoryDescription,
"subCategories": [],
"products": []
}
for row in categoryData:
#Extract data from row to create subcategory object
categoryName, categoryDescription, subCategoryName, subCategoryDescription = row
if (subCategoryName is not None):
category["subCategories"].append({
"subCategoryName": subCategoryName,
"subCategoryDescription": subCategoryDescription
})
for row in productData:
productId, productName, productDescription, productPrice, productStockQuantity = row
category["products"].append({
"productId": productId,
"productName": productName,
"productDescription": productDescription,
"productPrice": productPrice,
"productStockQuantity": productStockQuantity
})
return jsonify(category)
\ 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