formatting
This commit is contained in:
parent
f94c87c06f
commit
12b58e7bf1
|
|
@ -4,6 +4,7 @@ import uuid
|
||||||
|
|
||||||
from src.services import BasketService
|
from src.services import BasketService
|
||||||
|
|
||||||
|
|
||||||
class Basket(Resource):
|
class Basket(Resource):
|
||||||
basket_service = BasketService()
|
basket_service = BasketService()
|
||||||
|
|
||||||
|
|
@ -15,7 +16,9 @@ class Basket(Resource):
|
||||||
def get(self):
|
def get(self):
|
||||||
if "session_id" not in session:
|
if "session_id" not in session:
|
||||||
session["session_id"] = self.get_session_id()
|
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):
|
def post(self):
|
||||||
# expects something similar: {"items": [{"item_id": abc002}]}
|
# expects something similar: {"items": [{"item_id": abc002}]}
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,14 @@ 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'
|
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/")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
"""in lieu of a UI redirect to the API doc"""
|
"""in lieu of a UI redirect to the API doc"""
|
||||||
app.logger.info(f"request from {request.remote_addr}")
|
app.logger.info(f"request from {request.remote_addr}")
|
||||||
return redirect("/api/doc/swagger.json", code=302)
|
return redirect("/api/doc/swagger.json", code=302)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Item:
|
class Item:
|
||||||
id: str
|
id: str
|
||||||
|
|
|
||||||
|
|
@ -2,4 +2,4 @@ from main import app
|
||||||
|
|
||||||
# disabled threading for easier session handleing in this demo project
|
# disabled threading for easier session handleing in this demo project
|
||||||
# if threaded was True, as session store would be needed
|
# 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 collections import defaultdict
|
||||||
from DealsService import DealsService
|
from DealsService import DealsService
|
||||||
from ProductService import ProductService
|
from ProductService import ProductService
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
@ -18,7 +17,6 @@ class DealsService:
|
||||||
# changed with the rollout of a new version,
|
# 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
|
||||||
|
|
||||||
|
|
||||||
def get_items_with_final_prices(self, items) -> list:
|
def get_items_with_final_prices(self, items) -> list:
|
||||||
groups = defaultdict(list)
|
groups = defaultdict(list)
|
||||||
|
|
||||||
|
|
@ -43,11 +41,12 @@ class DealsService:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def two_for_one_deal(group):
|
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,
|
# ideally would be an object implementing a "Deal"-interface,
|
||||||
# which has 2 functions, deal_is_applicable() and apply_deal()
|
# which has 2 functions, deal_is_applicable() and apply_deal()
|
||||||
def _round_up(self, val: float):
|
def _round_up(self, val: float):
|
||||||
return math.ceil(val*100)/100
|
return math.ceil(val * 100) / 100
|
||||||
|
|
||||||
# could be compressed
|
# could be compressed
|
||||||
# not done since this would be the 2 different functions in the above mentioned interface
|
# 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:
|
if len(group) >= 2:
|
||||||
deal_is_applicable = True
|
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)
|
group[i].price = _round_up(group[i].price / 2)
|
||||||
return deal_is_applicable
|
return deal_is_applicable
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def ten_percent_off(group):
|
def ten_percent_off(group):
|
||||||
# the percentage discount could be added to function signature
|
# 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,
|
# ideally would be an object implementing a "Deal"-interface,
|
||||||
# which has 2 functions, deal_is_applicable() and apply_deal()
|
# which has 2 functions, deal_is_applicable() and apply_deal()
|
||||||
def _round_up(self, val: float):
|
def _round_up(self, val: float):
|
||||||
return math.ceil(val*100)/100
|
return math.ceil(val * 100) / 100
|
||||||
|
|
||||||
deal_is_applicable = True
|
deal_is_applicable = True
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,5 @@ class ProductService:
|
||||||
self.prices["A0001"] = 12.99
|
self.prices["A0001"] = 12.99
|
||||||
self.prices["A0002"] = 3.99
|
self.prices["A0002"] = 3.99
|
||||||
|
|
||||||
|
|
||||||
def get_item_from_id(self, item_id: str) -> Item:
|
def get_item_from_id(self, item_id: str) -> Item:
|
||||||
return Item(id=item_id, price=self.prices.get(item_id))
|
return Item(id=item_id, price=self.prices.get(item_id))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue