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

Added products to categories

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