forked from martinsson/Refactoring-Kata-Lift-Pass-Pricing
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprices.py
87 lines (78 loc) · 3.54 KB
/
prices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import math
from flask import Flask
from flask import request
from datetime import datetime
from db import create_lift_pass_db_connection
app = Flask("lift-pass-pricing")
connection_options = {
"host": 'localhost',
"user": 'root',
"database": 'lift_pass',
"password": 'mysql'}
connection = None
connection_name = None
@app.route("/prices", methods=['GET', 'PUT'])
def prices():
res = {}
global connection, connection_name
if connection is None:
connection, connection_name = create_lift_pass_db_connection(connection_options)
if request.method == 'PUT':
lift_pass_cost = request.args["cost"]
lift_pass_type = request.args["type"]
cursor = connection.cursor()
if connection_name != "sqlite3":
cursor.execute('INSERT INTO `base_price` (type, cost) VALUES (?, ?) ' +
'ON DUPLICATE KEY UPDATE cost = ?', (lift_pass_type, lift_pass_cost, lift_pass_cost))
else:
cursor.execute('INSERT INTO `base_price` (type, cost) VALUES (?, ?) ', (lift_pass_type, lift_pass_cost))
return {}
elif request.method == 'GET':
cursor = connection.cursor()
cursor.execute(f'SELECT cost FROM base_price '
+ 'WHERE type = ? ', (request.args['type'],))
row = cursor.fetchone()
result = {"cost": row[0]}
if 'age' in request.args and request.args.get('age', type=int) < 6:
res["cost"] = 0
else:
if "type" in request.args and request.args["type"] != "night":
cursor = connection.cursor()
cursor.execute('SELECT * FROM holidays')
is_holiday = False
reduction = 0
for row in cursor.fetchall():
holiday = row[0]
if connection_name == "sqlite3":
holiday = datetime.fromisoformat(holiday)
if "date" in request.args:
d = datetime.fromisoformat(request.args["date"])
if d.year == holiday.year and d.month == holiday.month and holiday.day == d.day:
is_holiday = True
if not is_holiday and "date" in request.args and datetime.fromisoformat(request.args["date"]).weekday() == 0:
reduction = 35
# TODO: apply reduction for others
if 'age' in request.args and request.args.get('age', type=int) < 15:
res['cost'] = math.ceil(result["cost"]*.7)
else:
if 'age' not in request.args:
cost = result['cost'] * (1 - reduction/100)
res['cost'] = math.ceil(cost)
else:
if 'age' in request.args and request.args.get('age', type=int) > 64:
cost = result['cost'] * .75 * (1 - reduction / 100)
res['cost'] = math.ceil(cost)
elif 'age' in request.args:
cost = result['cost'] * (1 - reduction / 100)
res['cost'] = math.ceil(cost)
else:
if 'age' in request.args and request.args.get('age', type=int) >= 6:
if request.args.get('age', type=int) > 64:
res['cost'] = math.ceil(result['cost'] * .4)
else:
res.update(result)
else:
res['cost'] = 0
return res
if __name__ == "__main__":
app.run(port=3005)