Skip to content

Commit 2133ad3

Browse files
do not return relationship details when fetching a resource to reduce db load and provide separate routes for doing so
1 parent 9b9d1ce commit 2133ad3

File tree

2 files changed

+107
-9
lines changed

2 files changed

+107
-9
lines changed

app/models.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def to_json(self):
2929
'id': self.id,
3030
'name': self.name,
3131
'created_at': self.created_at,
32-
'subcategories': [subcategory.id for subcategory in self.subcategories]
3332
}
3433

3534

@@ -46,8 +45,6 @@ def to_json(self):
4645
'id': self.id,
4746
'name': self.name,
4847
'created_at': self.created_at,
49-
'categories': [c.id for c in self.categories],
50-
'products': [p.id for p in self.products]
5148
}
5249

5350

@@ -65,5 +62,4 @@ def to_json(self):
6562
'name': self.name,
6663
'description': self.description,
6764
'created_at': self.created_at,
68-
'subcategories': [s.id for s in self.subcategories]
6965
}

app/routes.py

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,40 @@ def delete_category(c_id):
185185
return "Error occured", 500
186186

187187

188+
@app.route('/category/<int:c_id>/subcategories', methods=['GET'])
189+
def get_category_subcategories(c_id):
190+
"""
191+
Get Subcategories within a Category.
192+
---
193+
tags:
194+
- Category
195+
description: Get Subcategories within a Category.
196+
parameters:
197+
- in: path
198+
name: c_id
199+
required: true
200+
type: integer
201+
description: Category ID
202+
responses:
203+
200:
204+
description: Subcategories retrieved successfully.
205+
404:
206+
description: Category not found.
207+
500:
208+
description: Error occurred.
209+
"""
210+
category = Category.query.get(c_id)
211+
if category is None:
212+
abort(404)
213+
214+
try:
215+
return {
216+
"subcategories": [sc.to_json() for sc in category.subcategories]
217+
}, 200
218+
except:
219+
return "Error occured", 500
220+
221+
188222
@app.route('/subcategory/create', methods=['POST'])
189223
def create_subcategory():
190224
"""
@@ -384,6 +418,40 @@ def delete_subcategory(sc_id):
384418
return "Error occured", 500
385419

386420

421+
@app.route('/subcategory/<int:sc_id>/categories', methods=['GET'])
422+
def get_subcategory_categories(sc_id):
423+
"""
424+
Get Categories related to a Subcategory.
425+
---
426+
tags:
427+
- Subcategory
428+
description: Get Categories related to a Subcategory.
429+
parameters:
430+
- in: path
431+
name: sc_id
432+
required: true
433+
type: integer
434+
description: Subcategory ID
435+
responses:
436+
200:
437+
description: Categories retrieved successfully.
438+
404:
439+
description: Subcategory not found.
440+
500:
441+
description: Error occurred.
442+
"""
443+
subcategory = Subcategory.query.get(sc_id)
444+
if subcategory is None:
445+
abort(404)
446+
447+
try:
448+
return {
449+
"categories": [c.to_json() for c in subcategory.categories]
450+
}, 200
451+
except:
452+
return "Error occured", 500
453+
454+
387455
@app.route('/product/create', methods=['POST'])
388456
def create_product():
389457
"""
@@ -613,13 +681,47 @@ def get_product_by_name(name):
613681
return "Error occured", 500
614682

615683

684+
@app.route('/product/<int:p_id>/subcategories', methods=['GET'])
685+
def get_product_subcategories(p_id):
686+
"""
687+
Get Subcategories related to a Product.
688+
---
689+
tags:
690+
- Product
691+
description: Get Subcategories related to a Product.
692+
parameters:
693+
- in: path
694+
name: p_id
695+
required: true
696+
type: integer
697+
description: Product ID
698+
responses:
699+
200:
700+
description: Subcategories retrieved successfully.
701+
404:
702+
description: Product not found.
703+
500:
704+
description: Error occurred.
705+
"""
706+
product = Product.query.get(p_id)
707+
if product is None:
708+
abort(404)
709+
710+
try:
711+
return {
712+
"subcategories": [sc.to_json() for sc in product.subcategories]
713+
}, 200
714+
except:
715+
return "Error occured", 500
716+
717+
616718
@app.route('/subcategory/<int:sc_id>/products', methods=['GET'])
617719
def get_subcategory_products(sc_id):
618720
"""
619-
Get Subcategory Products
721+
Get Products within a Subcategory.
620722
---
621723
tags:
622-
- Product
724+
- Subcategory
623725
description: Get products for a subcategory.
624726
parameters:
625727
- in: path
@@ -657,11 +759,11 @@ def get_subcategory_products(sc_id):
657759
@app.route('/category/<int:c_id>/products', methods=['GET'])
658760
def get_category_products(c_id):
659761
"""
660-
Get Category Products
762+
Get Products within a Category.
661763
---
662764
tags:
663-
- Product
664-
description: Get products for a category.
765+
- Category
766+
description: Get Products for a Category.
665767
parameters:
666768
- in: path
667769
name: c_id

0 commit comments

Comments
 (0)