-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_service.py
More file actions
376 lines (318 loc) · 11.5 KB
/
db_service.py
File metadata and controls
376 lines (318 loc) · 11.5 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import sqlite3
import os
from flask import current_app
# Global database connection
db = None
db_path = None
def get_db():
global db, db_path
if db is None:
try:
# Try to get path from Flask config
db_path = current_app.config['DATABASE']
except RuntimeError:
# If outside Flask context, use default path
db_path = 'data/store.db'
# Ensure parent folder exists
parent_dir = os.path.dirname(db_path)
if parent_dir and not os.path.exists(parent_dir):
os.makedirs(parent_dir, exist_ok=True)
db = sqlite3.connect(
db_path,
detect_types=sqlite3.PARSE_DECLTYPES
)
db.row_factory = sqlite3.Row
return db
def close_db(e=None):
global db
if db is not None:
db.close()
db = None
def init_app(app):
# Register close_db to be called when the application shuts down
app.teardown_appcontext(close_db)
def get_user_by_id(user_id):
db = get_db()
return db.execute('SELECT * FROM user WHERE id = ?', (user_id,)).fetchone()
def get_user_by_username(username):
db = get_db()
return db.execute('SELECT * FROM user WHERE username = ?', (username,)).fetchone()
def create_user(username, password_hash):
db = get_db()
db.execute('INSERT INTO user (username, password_hash) VALUES (?, ?)', (username, password_hash))
db.commit()
def get_all_products():
db = get_db()
return db.execute('SELECT * FROM product').fetchall()
def get_all_products_ordered():
db = get_db()
return db.execute('SELECT * FROM product ORDER BY name').fetchall()
def get_featured_products(limit=8):
db = get_db()
return db.execute('SELECT * FROM product WHERE stock > 0 ORDER BY RANDOM() LIMIT ?', (limit,)).fetchall()
def get_product_by_id(product_id):
db = get_db()
return db.execute('SELECT * FROM product WHERE id = ?', (product_id,)).fetchone()
def get_cart(user_id):
db = get_db()
cart = db.execute('SELECT * FROM cart WHERE user_id = ?', (user_id,)).fetchone()
if not cart:
db.execute('INSERT INTO cart (user_id) VALUES (?)', (user_id,))
db.commit()
cart = db.execute('SELECT * FROM cart WHERE user_id = ?', (user_id,)).fetchone()
return cart
def get_cart_items(cart_id):
db = get_db()
return db.execute('''
SELECT ci.*, p.name, p.price, p.image_url, p.description
FROM cart_item ci
JOIN product p ON ci.product_id = p.id
WHERE ci.cart_id = ?
''', (cart_id,)).fetchall()
def add_to_cart(cart_id, product_id, quantity):
db = get_db()
cart_item = db.execute('''
SELECT * FROM cart_item
WHERE cart_id = ? AND product_id = ?
''', (cart_id, product_id)).fetchone()
if cart_item:
db.execute('''
UPDATE cart_item
SET quantity = quantity + ?
WHERE id = ?
''', (quantity, cart_item['id']))
else:
db.execute('''
INSERT INTO cart_item (cart_id, product_id, quantity)
VALUES (?, ?, ?)
''', (cart_id, product_id, quantity))
db.commit()
def update_cart_item(item_id, quantity):
db = get_db()
if quantity > 0:
db.execute('UPDATE cart_item SET quantity = ? WHERE id = ?', (quantity, item_id))
else:
db.execute('DELETE FROM cart_item WHERE id = ?', (item_id,))
db.commit()
def remove_cart_item(item_id):
db = get_db()
db.execute('DELETE FROM cart_item WHERE id = ?', (item_id,))
db.commit()
def clear_cart(cart_id):
db = get_db()
db.execute('DELETE FROM cart_item WHERE cart_id = ?', (cart_id,))
db.commit()
def create_order(user_id, total_amount, cart_items, shipping_name, shipping_email, shipping_address):
db = get_db()
# Create order
db.execute('''
INSERT INTO orders (user_id, total_amount, created_at, shipping_name, shipping_email, shipping_address)
VALUES (?, ?, datetime('now'), ?, ?, ?)
''', (user_id, total_amount, shipping_name, shipping_email, shipping_address))
order_id = db.execute('SELECT last_insert_rowid()').fetchone()[0]
# Add order items
for item in cart_items:
db.execute('''
INSERT INTO order_item (order_id, product_id, quantity, price)
VALUES (?, ?, ?, ?)
''', (order_id, item['product_id'], item['quantity'], item['price']))
db.commit()
return order_id
def get_user_order_history(user_id, search=None):
db = get_db()
cursor = db.cursor()
if search:
query = f'''
SELECT o.*,
GROUP_CONCAT(p.name || ' (' || oi.quantity || ')') as product_names
FROM orders o
LEFT JOIN order_item oi ON o.id = oi.order_id
LEFT JOIN product p ON oi.product_id = p.id
WHERE p.name LIKE '%{search}%' AND o.user_id = {user_id}
GROUP BY o.id
ORDER BY o.created_at DESC
'''
else:
query = f'''
SELECT o.*,
GROUP_CONCAT(p.name || ' (' || oi.quantity || ')') as product_names
FROM orders o
LEFT JOIN order_item oi ON o.id = oi.order_id
LEFT JOIN product p ON oi.product_id = p.id
WHERE o.user_id = {user_id}
GROUP BY o.id
ORDER BY o.created_at DESC
'''
cursor.execute(query)
return cursor.fetchall()
def get_products_by_category(category):
db = get_db()
return db.execute('SELECT * FROM product WHERE category = ? AND stock > 0', (category,)).fetchall()
def get_all_orders_with_details():
db = get_db()
cursor = db.cursor()
# Get orders with user info and items
cursor.execute('''
SELECT o.*, u.username,
GROUP_CONCAT(p.name || ' (' || oi.quantity || ')') as items_list,
GROUP_CONCAT(oi.quantity) as quantities,
GROUP_CONCAT(p.name) as product_names
FROM orders o
JOIN user u ON o.user_id = u.id
LEFT JOIN order_item oi ON o.id = oi.order_id
LEFT JOIN product p ON oi.product_id = p.id
GROUP BY o.id
ORDER BY o.created_at DESC
''')
orders_raw = cursor.fetchall()
# Convert SQLite Row objects to dictionaries and process items
orders = []
for order in orders_raw:
order_dict = dict(order)
if order_dict['items_list']:
quantities = order_dict['quantities'].split(',')
product_names = order_dict['product_names'].split(',')
order_dict['items'] = [{'quantity': int(q), 'name': n} for q, n in zip(quantities, product_names)]
else:
order_dict['items'] = []
orders.append(order_dict)
return orders
def get_order_details(order_id):
db = get_db()
cursor = db.cursor()
cursor.execute('''
SELECT o.*, u.username
FROM orders o
JOIN user u ON o.user_id = u.id
WHERE o.id = ?
''', (order_id,))
return cursor.fetchone()
def get_order_items(order_id):
db = get_db()
cursor = db.cursor()
cursor.execute('''
SELECT oi.*, p.name, p.image_url
FROM order_item oi
JOIN product p ON oi.product_id = p.id
WHERE oi.order_id = ?
''', (order_id,))
return cursor.fetchall()
def get_all_users():
db = get_db()
return db.execute('SELECT * FROM user ORDER BY username').fetchall()
def get_total_orders():
db = get_db()
return db.execute('SELECT COUNT(*) FROM orders WHERE status != "refunded"').fetchone()[0]
def get_total_revenue():
db = get_db()
return db.execute('SELECT COALESCE(SUM(total_amount), 0) FROM orders WHERE status != "refunded"').fetchone()[0]
def get_total_users():
db = get_db()
return db.execute('SELECT COUNT(*) FROM user').fetchone()[0]
def get_recent_orders(limit=5):
db = get_db()
return db.execute('''
SELECT o.id, o.total_amount, o.created_at, u.username
FROM orders o
JOIN user u ON o.user_id = u.id
WHERE o.status != "refunded"
ORDER BY o.created_at DESC
LIMIT ?
''', (limit,)).fetchall()
def get_top_products(limit=5):
db = get_db()
return db.execute('''
SELECT p.name, COUNT(oi.id) as sales, SUM(oi.quantity * oi.price) as revenue
FROM order_item oi
JOIN product p ON oi.product_id = p.id
JOIN orders o ON oi.order_id = o.id
WHERE o.status != "refunded"
GROUP BY p.id
ORDER BY sales DESC
LIMIT ?
''', (limit,)).fetchall()
def update_order_status(order_id, status):
db = get_db()
db.execute('UPDATE orders SET status = ? WHERE id = ?', (status, order_id))
db.commit()
def update_user_admin_status(user_id, is_admin):
db = get_db()
db.execute('UPDATE user SET is_admin = ? WHERE id = ?', (is_admin, user_id))
db.commit()
def add_product(name, description, price, stock, category, image_url):
db = get_db()
cursor = db.cursor()
cursor.execute(
'INSERT INTO product (name, description, price, stock, category, image_url) VALUES (?, ?, ?, ?, ?, ?)',
(name, description, price, stock, category, image_url)
)
db.commit()
return cursor.lastrowid
def update_product(product_id, name, description, price, stock, category, image_url=None):
db = get_db()
cursor = db.cursor()
if image_url:
cursor.execute(
'UPDATE product SET name = ?, description = ?, price = ?, stock = ?, category = ?, image_url = ? WHERE id = ?',
(name, description, price, stock, category, image_url, product_id)
)
else:
cursor.execute(
'UPDATE product SET name = ?, description = ?, price = ?, stock = ?, category = ? WHERE id = ?',
(name, description, price, stock, category, product_id)
)
db.commit()
def delete_product(product_id):
db = get_db()
cursor = db.cursor()
# Get product image path before deletion
cursor.execute('SELECT image_url FROM product WHERE id = ?', (product_id,))
product = cursor.fetchone()
# Delete product
cursor.execute('DELETE FROM product WHERE id = ?', (product_id,))
db.commit()
return product
# Review functions
def add_review(user_id, product_id, rating, comment):
db = get_db()
db.execute(
'INSERT INTO review (user_id, product_id, rating, comment, created_at) VALUES (?, ?, ?, ?, datetime("now"))',
(user_id, product_id, rating, comment)
)
db.commit()
def get_product_reviews(product_id):
db = get_db()
return db.execute('''
SELECT r.*, u.username
FROM review r
JOIN user u ON r.user_id = u.id
WHERE r.product_id = ?
ORDER BY r.created_at DESC
''', (product_id,)).fetchall()
def get_review_by_id(review_id):
db = get_db()
return db.execute('SELECT * FROM review WHERE id = ?', (review_id,)).fetchone()
def update_review(review_id, rating, comment):
db = get_db()
db.execute(
'UPDATE review SET rating = ?, comment = ? WHERE id = ?',
(rating, comment, review_id)
)
db.commit()
def delete_review(review_id):
db = get_db()
db.execute('DELETE FROM review WHERE id = ?', (review_id,))
db.commit()
def get_user_review_for_product(user_id, product_id):
db = get_db()
return db.execute(
'SELECT * FROM review WHERE user_id = ? AND product_id = ?',
(user_id, product_id)
).fetchone()
def get_avg_product_rating(product_id):
db = get_db()
result = db.execute(
'SELECT AVG(rating) as avg_rating, COUNT(*) as review_count FROM review WHERE product_id = ?',
(product_id,)
).fetchone()
return result