-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
319 lines (272 loc) · 8.73 KB
/
api.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import sqlite3 as db
from datetime import datetime
conn = db.connect("mycart.db")
cur = conn.cursor()
tmp_cart=[]
def init():
sql1='''
create table if not exists product(
p_id INTEGER PRIMARY KEY,
amount number,
category string,
product_name string,
description string,
date string
)
'''
sql2='''
create table if not exists category(
c_id INTEGER PRIMARY KEY,
category_name string,
date string
)
'''
sql3='''
create table if not exists cart(
cart_id INTEGER PRIMARY KEY,
product_name string,
amount number,
quantity number,
date string
)
'''
sql4 = '''
create table if not exists bill(
b_id INTEGER PRIMARY KEY,
items string,
actual_amount number,
discount number,
final_amount number,
date string
)
'''
cur.execute(sql1)
cur.execute(sql2)
cur.execute(sql3)
cur.execute(sql4)
conn.commit()
print("database created successfully")
def add_prod(prod_name,description,amount,category): #function to add new product to database by admin
date = str(datetime.now())
mssg = ""
sql='''
select product_name from product
'''.format(prod_name)
cur.execute(sql)
conn.commit()
result = cur.fetchall()
if result:
return "Product already present."
try:
sql='''
select * from category where category_name = '{}'
'''.format(category)
cur.execute(sql)
result = cur.fetchone()
if not result:
add_cat(category)
sql='''
insert into product (amount,category,product_name, description, date) values('{}','{}','{}','{}','{}')
'''.format(amount,category,prod_name,description,date)
cur.execute(sql)
conn.commit()
mssg = "success fully added"
except OSError as err:
mssg = "something went wrong: {}".format(err)
return {'status':mssg}
# add_prod("pencil box","a pencil box",120, "stationary")
def add_cat(category_name): #function to add new category to database by admin
date = str(datetime.now())
mssg = ""
sql='''
select category_name from category
'''.format(category_name)
cur.execute(sql)
conn.commit()
result = cur.fetchall()
if result:
return "category already present."
try:
sql='''
insert into category (category_name,date) values('{}','{}')
'''.format(category_name,date)
cur.execute(sql)
conn.commit()
mssg = "success fully added"
except OSError as err:
mssg = "something went wrong: {}".formart(err)
return {'status':mssg}
def cart(product_name): #function to add product to cart for checkout by user
date = str(datetime.now())
mssg = ""
amount = 0
try:
sql='''
select product_name,amount from product where product_name= '{}'
'''.format(product_name)
cur.execute(sql)
result = cur.fetchall()
# print('here is list of item: {}'.format(result[0][0]))
if not result:
return "Not a valid product name. Please try again."
amount = int(result[0][1])
sql='''
select product_name,quantity from cart where product_name = '{}'
'''.format(product_name)
cur.execute(sql)
qty = cur.fetchall()
if not qty:
sql='''
insert into cart (product_name,amount,quantity,date) values('{}','{}','{}','{}')
'''.format(product_name,amount,1,date)
cur.execute(sql)
conn.commit()
else:
# print(qty[0][1])
sql='''
update cart set quantity = '{}' where product_name = '{}'
'''.format(int(qty[0][1])+1,product_name)
cur.execute(sql)
conn.commit()
mssg = "success fully added"
except OSError as err:
mssg = "something went wrong: {}".formart(err)
return {'status':mssg,'result':view_cart()}
# cart("box")
def remove_cart(product_name): #function to remove product from cart by user
mssg = ""
try:
sql='''
select product_name,quantity from cart where product_name = '{}'
'''.format(product_name)
cur.execute(sql)
result = cur.fetchall()
# print(result)
if result:
if result[0][1]<=1:
sql='''
delete from cart where product_name = '{}'
'''.format(product_name)
cur.execute(sql)
conn.commit()
mssg = "success"
elif result[0][1]>1:
sql='''
update cart set quantity = '{}' where product_name = '{}'
'''.format(result[0][1]-1,product_name)
cur.execute(sql)
conn.commit()
mssg = "success"
else:
mssg = "no product found with the given name"
return {'status':mssg}
except OSError as err:
mssg = "something went wrong: {}".formart(err)
return {'status':mssg, 'result': view_cart()}
# remove_cart("box")
def checkout(): # final checkout function to calculate the amount and discount on prouct purchased by user.
date = str(datetime.now())
discount = 0
final_amount = 0
actual_amount = 0
bill_info = ""
mssg = "Thanks for visiting...."
sql='''
select * from cart
'''
cur.execute(sql)
cart_value = cur.fetchall()
if not cart_value:
return "Add something in cart first..."
for item in cart_value:
# print(item)
actual_amount += item[2]*item[3]
if actual_amount > 10000 :
discount = 500
final_amount = actual_amount - discount
else:
final_amount = actual_amount
try:
for item in cart_value:
for i in range(0,len(item)-2):
bill_info+=str(item[i])+" "
bill_info+=str(item[len(item)-2])
bill_info+=";"
sql = '''
insert into bill (items,actual_amount,discount,final_amount,date) values('{}','{}','{}','{}','{}')
'''.format(bill_info,actual_amount,discount,final_amount,date)
cur.execute(sql)
conn.commit()
except OSError as err:
print('something went wrong: {}'.format(err))
cart_value.append({'final_amount':final_amount,'actual_amount':actual_amount,'discount':discount})
sql='''
delete from cart
'''
cur.execute(sql)
conn.commit()
return {'status':mssg,'result':cart_value}
# checkout()
def view_cart(): #function to view cart items by user as well as by admin
sql='''
select * from cart
'''
cur.execute(sql)
result = cur.fetchall()
if result:
return result
else:
return 'Add something in cart.'
# view_cart()
def view_category(): #function to all view_category by user
sql='''
select * from category
'''
cur.execute(sql)
result = cur.fetchall()
return result
# view_cart()
def view_prod(category=None): #function to product by category or all product by user
if not category:
sql='''
select * from product
'''
else:
sql='''
select * from product where category = '{}'
'''.format(category)
cur.execute(sql)
result = cur.fetchall()
if not result:
return "Currently product not available under this category."
return result
# view_cart()
def view_bill(): #function to product by category or all product by user
sql='''
select * from bill
'''
cur.execute(sql)
result = cur.fetchall()
if not result:
return "No bill information for today."
return {'status':'OK','result':result}
# if __name__ == "__main__":
# init()
# print(add_cat("stationary"))
# print(add_cat("grocery"))
# print(add_cat("electronic"))
# print(add_cat("fashion"))
# print(view_category())
# print(add_prod("pencil box","a pencil box",120, "stationary"))
# print(add_prod("tube light","a tube light 25Watt",300, "electronic"))
# print(add_prod("mustard oil","250ml mustard oil bottle",110, "grocery"))
# print(add_prod("T-shirt unisex","a white T-shirt size-M unisex",499, "fashion"))
# print(add_prod("T-shirt mens","a black T-shirt size-M mens",499, "fashion"))
# print(add_prod("sofa","a pylwood 4 seater sofa",1499, "wooden"))
# print(view_prod())
# print(view_prod("fashion"))
# print(view_category())
# print(cart("tube light"))
# print(cart("pencil box"))
# print(checkout())
# print(remove_cart("pencil box"))