65 mins
This commit is contained in:
parent
ec0d3f33c4
commit
08bd89302b
|
|
@ -1,8 +1,28 @@
|
||||||
from flask_restful import Resource, reqparse
|
from flask_restful import Resource
|
||||||
import flask
|
from flask import session, Response, request
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from src.services import BasketService
|
||||||
|
|
||||||
class Basket(Resource):
|
class Basket(Resource):
|
||||||
def get(self, id = None):
|
basket_service = BasketService()
|
||||||
if id is None:
|
|
||||||
flask.make_response(flask.jsonify({'error': "No ID supplied"}), 401)
|
def get_session_id():
|
||||||
return flask.Response({"data": {}}, mimetype='image/png')
|
# 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)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import logging
|
||||||
import endpoints.BasketEndpoint as BasketEndpoint
|
import endpoints.BasketEndpoint as BasketEndpoint
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
app.secret_key = 'SUPER_DUPER_BAD_SECRET_KEY'
|
||||||
api = Api(app)
|
api = Api(app)
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
api.add_resource(BasketEndpoint.Basket,'/api/v1/basket/')
|
api.add_resource(BasketEndpoint.Basket,'/api/v1/basket/')
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,13 @@ class DealsService:
|
||||||
promotions = defaultdict(list)
|
promotions = defaultdict(list)
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
|
# placeholder data
|
||||||
|
# promiootion can be read dznamically from db
|
||||||
self.promotions["A0001"].append("two_for_one")
|
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
|
self.deals["two_for_one"] = DealsService.two_for_one_deal
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -52,3 +58,18 @@ class DealsService:
|
||||||
for i in range(len(group)//2):
|
for i in range(len(group)//2):
|
||||||
group[i].price = _round_up(group[i].price / 2)
|
group[i].price = _round_up(group[i].price / 2)
|
||||||
return deal_is_applicable
|
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
|
||||||
Loading…
Reference in New Issue