diff --git a/Backend/main.py b/Backend/main.py index 73e4271536a5659f7cbf6342a86d4b8989c0eba1..6553b3b0ef65a6c616c48b984dccb3a82b7a0fd1 100644 --- a/Backend/main.py +++ b/Backend/main.py @@ -1,5 +1,5 @@ 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.home import get_home #from routes.cart import get_cart @@ -14,24 +14,24 @@ def home(): #def order(order_id): # return get_order(order_id) -#@app.route('/category', methods=['GET']) -#@app.route('/category/<string:category_name>', methods=['GET']) -#def category(category_name): -# return routes.category.get_category(category_name) +@app.route('/category/', methods=['GET']) +@app.route('/category/<string:category_name>', methods=['GET']) +def category(category_name=None): + return get_category(category_name) #@app.route('/cart', methods=['GET']) #def cart(): # return get_cart() -@app.route('/logout', methods=['POST']) +@app.route('/logout/', methods=['POST']) def logout(): return post_logout() -@app.route('/login', methods=['POST']) +@app.route('/login/', methods=['POST']) def login(): return post_login() -@app.route('/register', methods=['POST']) +@app.route('/register/', methods=['POST']) def register(): return post_register() diff --git a/Backend/routes/category.py b/Backend/routes/category.py index db9e1b3fc4c3c2a90256375d0403434c4b0e67b7..5c7fcf5e9c7e86d208d9428154756cc7f2b97118 100644 --- a/Backend/routes/category.py +++ b/Backend/routes/category.py @@ -1,17 +1,78 @@ -from main import mysql +from utils.application import mysql from flask import jsonify - def get_category(category_name=None): - if category_name is None: - cur = mysql.connection.cursor() - cur.execute('''SELECT category.name FROM category''') - data = cur.fetchall() - cur.close() - return jsonify(data) + # 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.execute('''SELECT category.name FROM category WHERE name = %s''', (category_name,)) + 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() cur.close() - return jsonify(data) \ No newline at end of file + + # 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.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 + WHERE category.name = %s + ''', (category_name,)) + + data = cur.fetchall() + cur.close() + + if len(data) == 0: + return jsonify({"error": "Category not found"}), 404 + + + # Extract data from first row to create category object + categoryName, categoryDescription, subCategoryName, subCategoryDescription = data[0] + category = { + "categoryName": categoryName, + "categoryDescription": categoryDescription, + "subCategories": [] + } + for row in data: + #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 + }) + + return jsonify(category) \ No newline at end of file