-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
339 lines (301 loc) · 10.4 KB
/
Copy pathdb.py
File metadata and controls
339 lines (301 loc) · 10.4 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
import sqlite3
from sqlite3 import Error
from datetime import datetime
def create_connection():
try:
conn = sqlite3.connect('database.db')
return conn
except Error as e:
print(e)
return None
def create_tables():
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT NOT NULL,
balance REAL DEFAULT 0.0
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS transactions (
id INTEGER PRIMARY KEY,
user_id INTEGER,
service TEXT NOT NULL,
amount REAL NOT NULL,
commission REAL NOT NULL,
date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY,
user_id INTEGER,
username TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
likes INTEGER DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES users (id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY,
post_id INTEGER,
user_id INTEGER,
username TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (post_id) REFERENCES posts (id),
FOREIGN KEY (user_id) REFERENCES users (id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS post_likes (
id INTEGER PRIMARY KEY,
post_id INTEGER,
user_id INTEGER,
FOREIGN KEY (post_id) REFERENCES posts (id),
FOREIGN KEY (user_id) REFERENCES users (id)
);
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS bonus_claims (
id INTEGER PRIMARY KEY,
user_id INTEGER,
claimed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id)
);
""")
initial_users = [
(1, 'admin', 'admin123', 'admin', 0.0),
(2, 'user1', 'password123', 'user', 0.0),
(3, 'user2', 'password123', 'user', 0.0),
(4, 'user3', 'password123', 'user', 0.0),
(5, 'user4', 'password123', 'user', 0.0)
]
for user in initial_users:
cursor.execute("""
INSERT OR IGNORE INTO users (id, username, password, role, balance)
VALUES (?, ?, ?, ?, ?)
""", user)
conn.commit()
except Error as e:
print(e)
finally:
conn.close()
def add_user(username, password):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute(f"SELECT MAX(id) FROM users")
max_id = cursor.fetchone()[0] or 0
next_id = max_id + 1
query = f"INSERT INTO users (id, username, password, role, balance) VALUES ({next_id}, '{username}', '{password}', 'user', 0.0)"
cursor.execute(query)
conn.commit()
return True
finally:
conn.close()
return False
def verify_user(username, password):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = f"SELECT * FROM users WHERE username = '{username}' AND password = '{password}'"
cursor.execute(query)
return cursor.fetchone()
finally:
conn.close()
return None
def get_user_balance(user_id):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute("SELECT balance FROM users WHERE id = ?", (user_id,))
result = cursor.fetchone()
return result[0] if result else 0.0
finally:
conn.close()
return 0.0
def update_balance(user_id, new_balance):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute(f"UPDATE users SET balance = {new_balance} WHERE id = {user_id}")
conn.commit()
return True
finally:
conn.close()
return False
def add_transaction(user_id, service, amount, commission):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = f"INSERT INTO transactions (user_id, service, amount, commission, date) VALUES ({user_id}, '{service}', {amount}, {commission}, '{datetime.now()}')"
cursor.execute(query)
conn.commit()
return True
finally:
conn.close()
return False
def get_admin_stats():
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM users WHERE role = 'user'")
total_users = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM transactions")
total_orders = cursor.fetchone()[0]
cursor.execute("SELECT SUM(amount) FROM transactions")
total_revenue = cursor.fetchone()[0] or 0.0
return {
'total_users': total_users,
'total_orders': total_orders,
'total_revenue': total_revenue
}
finally:
conn.close()
return {'total_users': 0, 'total_orders': 0, 'total_revenue': 0.0}
def get_recent_transactions():
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = "SELECT t.*, u.username FROM transactions t JOIN users u ON t.user_id = u.id ORDER BY t.date DESC LIMIT 10"
cursor.execute(query)
return cursor.fetchall()
finally:
conn.close()
return []
def add_balance_column():
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute("ALTER TABLE users ADD COLUMN balance REAL DEFAULT 0.0")
conn.commit()
return True
except Error as e:
print(e)
return False
finally:
conn.close()
return False
def add_post(user_id, username, content):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = """
INSERT INTO posts (user_id, username, content)
VALUES (?, ?, ?)
"""
cursor.execute(query, (user_id, username, content))
conn.commit()
return True
finally:
conn.close()
return False
def get_all_posts():
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = "SELECT * FROM posts ORDER BY created_at DESC"
cursor.execute(query)
return cursor.fetchall()
finally:
conn.close()
return []
def add_comment(post_id, user_id, username, content):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = """
INSERT INTO comments (post_id, user_id, username, content)
VALUES (?, ?, ?, ?)
"""
cursor.execute(query, (post_id, user_id, username, content))
conn.commit()
return True
finally:
conn.close()
return False
def get_comments(post_id):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
query = "SELECT * FROM comments WHERE post_id = ? ORDER BY created_at ASC"
cursor.execute(query, (post_id,))
return cursor.fetchall()
finally:
conn.close()
return []
def toggle_like(post_id, user_id):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute("""
SELECT id FROM post_likes
WHERE post_id = ? AND user_id = ?
""", (post_id, user_id))
existing_like = cursor.fetchone()
if existing_like:
return False
cursor.execute("""
INSERT INTO post_likes (post_id, user_id)
VALUES (?, ?)
""", (post_id, user_id))
cursor.execute("""
UPDATE posts
SET likes = (SELECT COUNT(*) FROM post_likes WHERE post_id = ?)
WHERE id = ?
""", (post_id, post_id))
conn.commit()
cursor.execute("SELECT likes FROM posts WHERE id = ?", (post_id,))
return cursor.fetchone()[0]
finally:
conn.close()
return 0
def add_bonus_claim(user_id):
conn = create_connection()
if conn is not None:
try:
cursor = conn.cursor()
cursor.execute("""
SELECT id FROM bonus_claims
WHERE user_id = ?
""", (user_id,))
if cursor.fetchone():
return False
cursor.execute("""
INSERT INTO bonus_claims (user_id, claimed_at)
VALUES (?, CURRENT_TIMESTAMP)
""", (user_id,))
cursor.execute("""
UPDATE users
SET balance = balance + 100
WHERE id = ?
""", (user_id,))
conn.commit()
cursor.execute("SELECT balance FROM users WHERE id = ?", (user_id,))
return cursor.fetchone()[0]
finally:
conn.close()
return None