forked from tobiaswittekindt/clean-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadSmell.py
165 lines (119 loc) · 4.19 KB
/
badSmell.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# This File Takes an order via POST request and stores it into the DB
from enum import Enum
from random import randint
from typing import List
tax = 1.16
lastDBId = 0
class ExceptionTypes(Enum):
DATABASE = 'DATABASE'
LOGIC = 'LOGIC'
class DBConn:
def storeUserData(self, user):
# Karl what are you doing here???
return self.sqlStore('127.0.0.1', 'username', 'pass', user)
def storeOrderData(self, order):
return self.sqlStore('127.0.0.1', 'username', 'pass', order)
def sqlStore(self, server, username, password, data):
global lastDBId
# Not Implemented
print('Write to DB:')
success = True
Error_description = ''
# Check if an id is duplicated
if data['id'] == lastDBId:
success = False
Error_description = 'Write operation failed: duplicated key error'
return success, Error_description
lastDBId = data['id']
return success, Error_description
# Returns the last stored orderId
def get_order_id(self):
return lastDBId
class OrderItem:
id: int
name: str
price: float
priceWithTax: float
def __init__(self, name: str, price: float, id: int):
self.name = name
self.price = price
self.id = id
def createSpecialOrder(special, name, items, tax):
# loop over all order items and add a tax
[addTaxToOrderItem(i, tax) for i in items]
# remove tax from tax free products
for i in range(len(items)):
if is_item_vat_free(items[i]):
items[i].priceWithTax = items[i].price
dbConn = DBConn()
order = {'name': name, 'items': items, 'special': special, 'id': dbConn.get_order_id() + 1}
is_order_valid(order)
success, description = dbConn.storeOrderData(order)
if not success:
raise Exception(ExceptionTypes.DATABASE, description)
return order
def create_new_order_and_print(name, items: List[OrderItem], buyerId, tax):
# loop over all order items and add a tax
[addTaxToOrderItem(i, tax) for i in items]
# remove tax from tax free products
for i in range(len(items)):
if is_item_vat_free(items[i]):
items[i].priceWithTax = items[i].price
# 20% Bonus
if randint(1, 1000) == 1:
items[i].price = items[i].price * 0.8
dbConn = DBConn()
order = {'name': name, 'items': items, 'buyerId': buyerId, 'id': dbConn.get_order_id() + 1}
is_order_valid(order)
success, description = dbConn.storeOrderData(order)
if not success:
raise Exception(ExceptionTypes.DATABASE, description)
print('New Order: ' + order['name'] + ' [' + str(order['id']) + ']')
total = 0
for i in order['items']:
print('Item: ' + i.name + ' ' + str(i.priceWithTax) + '€')
total += i.priceWithTax
print('Total: ', total, '€')
return order
# Check if an order is plausible
def is_order_valid(o):
total = 0
for i in o['items']:
total += i.priceWithTax
if total <= 0:
raise Exception(ExceptionTypes.LOGIC, 'Total Order price < 0')
def calc_total_order_price(order) -> float:
total = 0
for i in order['items']:
total += i.priceWithTax
return total
def addTaxToOrderItem(item: OrderItem, tax) -> OrderItem:
item.priceWithTax = item.price * tax
return item
def is_item_vat_free(item):
if item.name == 'post_stamps':
return True
elif item.name == 'medicine':
return True
elif item.name == 'super_special_vat_free_item':
return True
else:
return False
def handlePostRequest(body):
""" Gets the data from a client """
orderName = body['name']
buyerId = body['buyerId']
items = []
i = 0
for item in body['items']:
i += 1
item1 = OrderItem(item['name'], float(item['price']), i)
items.append(item1)
try:
return create_new_order_and_print(orderName, items, buyerId, tax)
except Exception as inst:
type, desc = inst.args
if type == ExceptionTypes.DATABASE:
print('There was a problem with the DB', desc)
if type == ExceptionTypes.LOGIC:
print('There was a logic error', desc)