Skip to content
Snippets Groups Projects
Commit a55010f2 authored by Gisli's avatar Gisli
Browse files

Added a category route

parent 618a3818
No related branches found
No related tags found
No related merge requests found
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()
......
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment