WIP, added Services for endpoint to use, taking a short break
This commit is contained in:
parent
7a451097f2
commit
a883ae42c9
|
|
@ -4,15 +4,16 @@ from flask_restful_swagger_3 import Api
|
|||
from json import dumps
|
||||
import logging
|
||||
|
||||
import ShyBadger.endpoints.BasketEndpoint as BasketEndpoint
|
||||
import endpoints.BasketEndpoint as BasketEndpoint
|
||||
|
||||
app = Flask(__name__)
|
||||
api = Api(app)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
api.add_resource(BasketEndpoint.Basket,'/api/v1/recipe/<string:id>/image')
|
||||
api.add_resource(BasketEndpoint.Basket,'/api/v1/basket/')
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
"""in lieu of a UI redirect to the API doc"""
|
||||
app.logger.info(f"request from {request.remote_addr}")
|
||||
return redirect("/api/doc/swagger.json", code=302)
|
||||
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
@dataclass
|
||||
class Item:
|
||||
id: str
|
||||
price: str
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
from ShyBadger.main import app
|
||||
from main import app
|
||||
|
||||
# disabled threading for easier session handleing in this demo project
|
||||
# if threaded was True, as session store would be needed
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
|
||||
from collections import defaultdict
|
||||
from DealsService import DealsService
|
||||
from ProductService import ProductService
|
||||
|
||||
|
||||
class BasketService:
|
||||
baskets = None
|
||||
dealsService = None
|
||||
productService = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.baskets = defaultdict()
|
||||
self.dealsService = DealsService()
|
||||
self.productService = ProductService()
|
||||
|
||||
def total(self, sessionID: str) -> int:
|
||||
items = self.baskets[sessionID]
|
||||
prices = self.dealsService.getPrices()
|
||||
return sum(prices)
|
||||
|
||||
def scan(self, session_id: str, itemId) -> None:
|
||||
item = self.productService.get_item_from_id(itemId)
|
||||
self.baskets[session_id].append(item)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
|
||||
from collections import defaultdict
|
||||
import math
|
||||
|
||||
|
||||
class DealsService:
|
||||
deals = []
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def get_prices(self) -> list:
|
||||
return [0]
|
||||
|
||||
def _round_up(self, val: float):
|
||||
return math.ceil(val*100)/100
|
||||
|
||||
def two_for_one_deal(self, items) -> list:
|
||||
new_items = []
|
||||
groups = defaultdict(list)
|
||||
|
||||
for obj in items:
|
||||
groups[obj.id].append(obj)
|
||||
|
||||
for group in groups.values():
|
||||
for deal in self.deals:
|
||||
# the deals do not stack, the first deal in the list of deals is taken,
|
||||
# ideally for the customer all deals would be calculated and the maximum value selected
|
||||
# not implemented for time reasons
|
||||
if deal(group):
|
||||
break
|
||||
|
||||
return [item for group in groups.values() for item in group]
|
||||
|
||||
def two_for_one_deal(self, group):
|
||||
'''' applies deal onto item group in place '''
|
||||
for i in range(len(group)//2):
|
||||
group[i].price = self._round_up(group[i].price / 2)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
from collections import defaultdict
|
||||
|
||||
from src.models import Item
|
||||
|
||||
|
||||
class ProductService:
|
||||
prices = None
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.prices = defaultdict()
|
||||
|
||||
def get_item_from_id(self, item_id: str) -> Item:
|
||||
return Item(id=item_id, price=self.prices.get(item_id))
|
||||
Loading…
Reference in New Issue