formatting
This commit is contained in:
parent
f94c87c06f
commit
12b58e7bf1
|
|
@ -4,6 +4,7 @@ import uuid
|
|||
|
||||
from src.services import BasketService
|
||||
|
||||
|
||||
class Basket(Resource):
|
||||
basket_service = BasketService()
|
||||
|
||||
|
|
@ -15,7 +16,9 @@ class Basket(Resource):
|
|||
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")
|
||||
return Response(
|
||||
{"total": self.basket_service.total()}, mimetype="application/json"
|
||||
)
|
||||
|
||||
def post(self):
|
||||
# expects something similar: {"items": [{"item_id": abc002}]}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,14 @@ import logging
|
|||
import endpoints.BasketEndpoint as BasketEndpoint
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = 'SUPER_DUPER_BAD_SECRET_KEY'
|
||||
app.secret_key = "SUPER_DUPER_BAD_SECRET_KEY"
|
||||
api = Api(app)
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
api.add_resource(BasketEndpoint.Basket,'/api/v1/basket/')
|
||||
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)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class Item:
|
||||
id: str
|
||||
|
|
|
|||
|
|
@ -2,4 +2,4 @@ from main import app
|
|||
|
||||
# disabled threading for easier session handleing in this demo project
|
||||
# if threaded was True, as session store would be needed
|
||||
app.run(host="0.0.0.0", port='80', debug=False, threaded=False)
|
||||
app.run(host="0.0.0.0", port="80", debug=False, threaded=False)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
from collections import defaultdict
|
||||
from DealsService import DealsService
|
||||
from ProductService import ProductService
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
from collections import defaultdict
|
||||
import math
|
||||
|
||||
|
|
@ -18,7 +17,6 @@ class DealsService:
|
|||
# changed with the rollout of a new version,
|
||||
self.deals["two_for_one"] = DealsService.two_for_one_deal
|
||||
|
||||
|
||||
def get_items_with_final_prices(self, items) -> list:
|
||||
groups = defaultdict(list)
|
||||
|
||||
|
|
@ -43,11 +41,12 @@ class DealsService:
|
|||
|
||||
@staticmethod
|
||||
def two_for_one_deal(group):
|
||||
'''' applies deal onto item group in place '''
|
||||
"""' 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
|
||||
return math.ceil(val * 100) / 100
|
||||
|
||||
# could be compressed
|
||||
# not done since this would be the 2 different functions in the above mentioned interface
|
||||
|
|
@ -55,18 +54,19 @@ class DealsService:
|
|||
if len(group) >= 2:
|
||||
deal_is_applicable = True
|
||||
|
||||
for i in range(len(group)//2):
|
||||
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 '''
|
||||
"""' 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
|
||||
return math.ceil(val * 100) / 100
|
||||
|
||||
deal_is_applicable = True
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,5 @@ class ProductService:
|
|||
self.prices["A0001"] = 12.99
|
||||
self.prices["A0002"] = 3.99
|
||||
|
||||
|
||||
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