diff --git a/app/ShyBadger/src/endpoints/BasketEndpoint.py b/app/ShyBadger/src/endpoints/BasketEndpoint.py index f441aa9..fba12c6 100644 --- a/app/ShyBadger/src/endpoints/BasketEndpoint.py +++ b/app/ShyBadger/src/endpoints/BasketEndpoint.py @@ -1,8 +1,28 @@ -from flask_restful import Resource, reqparse -import flask +from flask_restful import Resource +from flask import session, Response, request +import uuid + +from src.services import BasketService class Basket(Resource): - def get(self, id = None): - if id is None: - flask.make_response(flask.jsonify({'error': "No ID supplied"}), 401) - return flask.Response({"data": {}}, mimetype='image/png') \ No newline at end of file + basket_service = BasketService() + + def get_session_id(): + # first interaction with server sets session_id + # would need to research if this is ideal + return str(uuid.uuid4()) + + def get(self): + if "session_id" not in session: + session["session_id"] = self.get_session_id() + return Response({"total": self.basket_service.total()}, mimetype="application/json") + + def post(self): + # expects something similar: {"items": [{"item_id": abc002}]} + + if "session_id" not in session: + session["session_id"] = self.get_session_id() + jsonData = request.get_json(force=True) + for entry in jsonData["items"]: + self.basket_service.scan(entry["item_id"]) + return Response(HTTPStatus=201) diff --git a/app/ShyBadger/src/main.py b/app/ShyBadger/src/main.py index 9953114..0c41ee4 100644 --- a/app/ShyBadger/src/main.py +++ b/app/ShyBadger/src/main.py @@ -7,6 +7,7 @@ import logging import endpoints.BasketEndpoint as BasketEndpoint app = Flask(__name__) +app.secret_key = 'SUPER_DUPER_BAD_SECRET_KEY' api = Api(app) logging.basicConfig(level=logging.DEBUG) api.add_resource(BasketEndpoint.Basket,'/api/v1/basket/') diff --git a/app/ShyBadger/src/services/DealsService.py b/app/ShyBadger/src/services/DealsService.py index 7ffa568..275cce0 100644 --- a/app/ShyBadger/src/services/DealsService.py +++ b/app/ShyBadger/src/services/DealsService.py @@ -9,7 +9,13 @@ class DealsService: promotions = defaultdict(list) def __init__(self) -> None: + # placeholder data + # promiootion can be read dznamically from db self.promotions["A0001"].append("two_for_one") + self.promotions["A0002"].append("ten_percent_off") + + # deal behavior needs to be programmed an can only be + # changed with the rollout of a new version, self.deals["two_for_one"] = DealsService.two_for_one_deal @@ -51,4 +57,19 @@ class DealsService: for i in range(len(group)//2): group[i].price = _round_up(group[i].price / 2) + return deal_is_applicable + + @staticmethod + def ten_percent_off(group): + # the percentage discount could be added to function signature + '''' applies deal onto item group in place ''' + # ideally would be an object implementing a "Deal"-interface, + # which has 2 functions, deal_is_applicable() and apply_deal() + def _round_up(self, val: float): + return math.ceil(val*100)/100 + + deal_is_applicable = True + + for i in range(len(group)): + group[i].price = _round_up(group[i].price * 0.9) return deal_is_applicable \ No newline at end of file