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

Added products to categories

parent 405b6fd8
Branches
No related tags found
No related merge requests found
......@@ -52,21 +52,28 @@ def get_category_by_name(category_name):
WHERE category.name = %s
''', (category_name,))
data = cur.fetchall()
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()
if len(data) == 0:
if len(categoryData) == 0:
return jsonify({"error": "Category not found"}), 404
# Extract data from first row to create category object
categoryName, categoryDescription, subCategoryName, subCategoryDescription = data[0]
categoryName, categoryDescription, subCategoryName, subCategoryDescription = categoryData[0]
category = {
"categoryName": categoryName,
"categoryDescription": categoryDescription,
"subCategories": []
"subCategories": [],
"products": []
}
for row in data:
for row in categoryData:
#Extract data from row to create subcategory object
categoryName, categoryDescription, subCategoryName, subCategoryDescription = row
if (subCategoryName is not None):
......@@ -75,4 +82,15 @@ def get_category_by_name(category_name):
"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