From 073e11528d6619d0d0b9f6a22b22d796ab8bb58e Mon Sep 17 00:00:00 2001 From: Pavel Senchanka Date: Sat, 16 Nov 2019 23:54:56 +0200 Subject: [PATCH] Add new controllers and start work on suggestions endpoint. --- server/app.py | 19 +++++++++++++++---- server/controllers/product.py | 18 ++++++++++++++++++ ...suggestion_controller.py => suggestion.py} | 10 +++++++--- 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 server/controllers/product.py rename server/controllers/{suggestion_controller.py => suggestion.py} (76%) diff --git a/server/app.py b/server/app.py index 27a1a2f..8bc7de7 100644 --- a/server/app.py +++ b/server/app.py @@ -1,7 +1,9 @@ import json -from flask import Flask, request +from flask import Flask, abort, request from flask_restful import Api, Resource from controllers.challenge import ChallengeController +from controllers.suggestion import SuggestionController +from controllers.product import ProductController app = Flask(__name__) @@ -41,11 +43,20 @@ def post(self): class ProductSuggestions(Resource): - def get(self): + def get(self, product_id): try: - pass + session = Session() + product = ProductController().get_product(product_id, session) + if not product: + abort(404) + suggestions = SuggestionController().get_suggestions( + product, session) + + except Exception as e: pass + finally: + session.close() class Purchase(Resource): @@ -58,7 +69,7 @@ def post(self): api.add_resource(Challenge, '/challenge') api.add_resource(GreenList, '/greenList') -api.add_resource(ProductSuggestions, '/productSuggestions') +api.add_resource(ProductSuggestions, '/productSuggestions/') api.add_resource(Purchase, '/purchases') if __name__ == '__main__': diff --git a/server/controllers/product.py b/server/controllers/product.py new file mode 100644 index 0000000..cbee786 --- /dev/null +++ b/server/controllers/product.py @@ -0,0 +1,18 @@ +from model import Product, Session + + +class ProductController: + def get_product(self, product_id, session=None): + own_session = False + if not session: + session = Session() + own_session = True + + product = (session + .query(Product) + .filter(Product.id == product_id) + .first()) + + if own_session: + session.close() + return product diff --git a/server/controllers/suggestion_controller.py b/server/controllers/suggestion.py similarity index 76% rename from server/controllers/suggestion_controller.py rename to server/controllers/suggestion.py index 25c1b4b..946cf21 100644 --- a/server/controllers/suggestion_controller.py +++ b/server/controllers/suggestion.py @@ -2,8 +2,11 @@ class SuggestionController: - def get_suggestions(self, product): - session = Session() + def get_suggestions(self, product, session=None): + own_session = False + if not session: + session = Session() + own_session = True bigger_sizes = (self._session .query(Product, ProductSize) @@ -18,5 +21,6 @@ def get_suggestions(self, product): .filter(Product.product_id != product.product_id) .all()) - session.close() + if own_session: + session.close() return bigger_sizes + same_class