@@ -52,22 +52,6 @@ def create_category():
5252 return "Error occured" , 500
5353
5454
55- @app .route ('/categories' , methods = ['GET' ])
56- def get_all_categories ():
57- """
58- Get All Categories
59- ---
60- tags:
61- - Category
62- description: Get all categories.
63- responses:
64- 200:
65- description: A list of categories.
66- """
67- categories = Category .query .order_by (Category .name ).all ()
68- return jsonify ({"categories" : [category .to_json () for category in categories ]}), 200
69-
70-
7155@app .route ('/category/<int:c_id>' , methods = ['GET' ])
7256def get_category (c_id ):
7357 """
@@ -185,6 +169,22 @@ def delete_category(c_id):
185169 return "Error occured" , 500
186170
187171
172+ @app .route ('/categories' , methods = ['GET' ])
173+ def get_all_categories ():
174+ """
175+ Get All Categories
176+ ---
177+ tags:
178+ - Category
179+ description: Get all categories.
180+ responses:
181+ 200:
182+ description: A list of categories.
183+ """
184+ categories = Category .query .order_by (Category .name ).all ()
185+ return jsonify ({"categories" : [category .to_json () for category in categories ]}), 200
186+
187+
188188@app .route ('/category/<int:c_id>/subcategories' , methods = ['GET' ])
189189def get_category_subcategories (c_id ):
190190 """
@@ -219,6 +219,57 @@ def get_category_subcategories(c_id):
219219 return "Error occured" , 500
220220
221221
222+ @app .route ('/category/<int:c_id>/products' , methods = ['GET' ])
223+ def get_category_products (c_id ):
224+ """
225+ Get Products within a Category.
226+ ---
227+ tags:
228+ - Category
229+ description: Get Products for a Category.
230+ parameters:
231+ - in: path
232+ name: c_id
233+ required: true
234+ type: integer
235+ description: Category ID
236+ - in: query
237+ name: page
238+ type: integer
239+ default: 1
240+ description: Page number
241+ responses:
242+ 200:
243+ description: Products retrieved successfully.
244+ 404:
245+ description: Category not found.
246+ 500:
247+ description: Error occurred.
248+ """
249+ category_exists = db .session .query (Category .id ).filter_by (id = c_id ).first () is not None
250+ if not category_exists :
251+ abort (404 )
252+
253+ try :
254+ page = request .args .get ("page" , default = 1 , type = int )
255+
256+ products = (
257+ Product .query
258+ .join (subcategory_product )
259+ .join (category_subcategory , onclause = subcategory_product .c .subcategory_id == category_subcategory .c .subcategory_id )
260+ .filter (category_subcategory .c .category_id == c_id )
261+ .distinct ()
262+ .order_by (Product .id .asc ())
263+ .paginate (page = page , per_page = 2 , error_out = False )
264+ )
265+
266+ return {
267+ "products" : [p .id for p in products ]
268+ }, 200
269+ except :
270+ return "Error occured" , 500
271+
272+
222273@app .route ('/subcategory/create' , methods = ['POST' ])
223274def create_subcategory ():
224275 """
@@ -452,6 +503,47 @@ def get_subcategory_categories(sc_id):
452503 return "Error occured" , 500
453504
454505
506+ @app .route ('/subcategory/<int:sc_id>/products' , methods = ['GET' ])
507+ def get_subcategory_products (sc_id ):
508+ """
509+ Get Products within a Subcategory.
510+ ---
511+ tags:
512+ - Subcategory
513+ description: Get products for a subcategory.
514+ parameters:
515+ - in: path
516+ name: sc_id
517+ required: true
518+ type: integer
519+ description: Subcategory ID
520+ - in: query
521+ name: page
522+ type: integer
523+ default: 1
524+ description: Page number
525+ responses:
526+ 200:
527+ description: Products retrieved successfully.
528+ 404:
529+ description: Subcategory not found.
530+ 500:
531+ description: Error occurred.
532+ """
533+ subcategory = Subcategory .query .get (sc_id )
534+ if not subcategory :
535+ abort (404 )
536+
537+ try :
538+ page = request .args .get ("page" , default = 1 , type = int )
539+ products = subcategory .products .order_by (Product .id .asc ()).paginate (page = page , per_page = 2 , error_out = False )
540+ return {
541+ "products" : [p .id for p in products ]
542+ }, 200
543+ except :
544+ return "Error occured" , 500
545+
546+
455547@app .route ('/product/create' , methods = ['POST' ])
456548def create_product ():
457549 """
@@ -505,22 +597,6 @@ def create_product():
505597 return "Error occured" , 500
506598
507599
508- @app .route ('/products' , methods = ['GET' ])
509- def get_all_products ():
510- """
511- Get All Products
512- ---
513- tags:
514- - Product
515- description: Get all products.
516- responses:
517- 200:
518- description: A list of products.
519- """
520- products = Product .query .order_by (Product .name ).all ()
521- return jsonify ({"products" : [product .to_json () for product in products ]}), 200
522-
523-
524600@app .route ('/product/<int:p_id>' , methods = ['GET' ])
525601def get_product (p_id ):
526602 """
@@ -681,6 +757,22 @@ def get_product_by_name(name):
681757 return "Error occured" , 500
682758
683759
760+ @app .route ('/products' , methods = ['GET' ])
761+ def get_all_products ():
762+ """
763+ Get All Products
764+ ---
765+ tags:
766+ - Product
767+ description: Get all products.
768+ responses:
769+ 200:
770+ description: A list of products.
771+ """
772+ products = Product .query .order_by (Product .name ).all ()
773+ return jsonify ({"products" : [product .to_json () for product in products ]}), 200
774+
775+
684776@app .route ('/product/<int:p_id>/subcategories' , methods = ['GET' ])
685777def get_product_subcategories (p_id ):
686778 """
@@ -713,95 +805,3 @@ def get_product_subcategories(p_id):
713805 }, 200
714806 except :
715807 return "Error occured" , 500
716-
717-
718- @app .route ('/subcategory/<int:sc_id>/products' , methods = ['GET' ])
719- def get_subcategory_products (sc_id ):
720- """
721- Get Products within a Subcategory.
722- ---
723- tags:
724- - Subcategory
725- description: Get products for a subcategory.
726- parameters:
727- - in: path
728- name: sc_id
729- required: true
730- type: integer
731- description: Subcategory ID
732- - in: query
733- name: page
734- type: integer
735- default: 1
736- description: Page number
737- responses:
738- 200:
739- description: Products retrieved successfully.
740- 404:
741- description: Subcategory not found.
742- 500:
743- description: Error occurred.
744- """
745- subcategory = Subcategory .query .get (sc_id )
746- if not subcategory :
747- abort (404 )
748-
749- try :
750- page = request .args .get ("page" , default = 1 , type = int )
751- products = subcategory .products .order_by (Product .id .asc ()).paginate (page = page , per_page = 2 , error_out = False )
752- return {
753- "products" : [p .id for p in products ]
754- }, 200
755- except :
756- return "Error occured" , 500
757-
758-
759- @app .route ('/category/<int:c_id>/products' , methods = ['GET' ])
760- def get_category_products (c_id ):
761- """
762- Get Products within a Category.
763- ---
764- tags:
765- - Category
766- description: Get Products for a Category.
767- parameters:
768- - in: path
769- name: c_id
770- required: true
771- type: integer
772- description: Category ID
773- - in: query
774- name: page
775- type: integer
776- default: 1
777- description: Page number
778- responses:
779- 200:
780- description: Products retrieved successfully.
781- 404:
782- description: Category not found.
783- 500:
784- description: Error occurred.
785- """
786- category_exists = db .session .query (Category .id ).filter_by (id = c_id ).first () is not None
787- if not category_exists :
788- abort (404 )
789-
790- try :
791- page = request .args .get ("page" , default = 1 , type = int )
792-
793- products = (
794- Product .query
795- .join (subcategory_product )
796- .join (category_subcategory , onclause = subcategory_product .c .subcategory_id == category_subcategory .c .subcategory_id )
797- .filter (category_subcategory .c .category_id == c_id )
798- .distinct ()
799- .order_by (Product .id .asc ())
800- .paginate (page = page , per_page = 2 , error_out = False )
801- )
802-
803- return {
804- "products" : [p .id for p in products ]
805- }, 200
806- except :
807- return "Error occured" , 500
0 commit comments