This commit is contained in:
Askill 2024-08-15 18:01:17 +02:00
parent ec0d3f33c4
commit 08bd89302b
3 changed files with 48 additions and 6 deletions

View File

@ -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')
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)

View File

@ -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/')

View File

@ -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