-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtransaction_db.py
91 lines (73 loc) · 2.89 KB
/
transaction_db.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
import sqlite3
import time
import json
class TransactionDb:
def __init__(self, db_name="transactions.db"):
self.conn = sqlite3.connect(db_name)
self.create_tables()
self.seed_data()
def create_tables(self):
cursor = self.conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Users (
userId INTEGER PRIMARY KEY,
username TEXT NOT NULL,
password TEXT NOT NULL
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS Transactions (
transactionId INTEGER PRIMARY KEY,
userId INTEGER NOT NULL,
reference TEXT,
recipient TEXT,
amount REAL
)
''')
self.conn.commit()
def seed_data(self):
cursor = self.conn.cursor()
# Sample users
users = [
(1,"MartyMcFly", "Password1"),
(2,"DocBrown", "flux-capacitor-123"),
(3,"BiffTannen", "Password3"),
(4,"GeorgeMcFly", "Password4")
]
cursor.executemany("INSERT OR IGNORE INTO Users (userId, username, password) VALUES (?, ?, ?)", users)
# Sample transactions
transactions = [
(1, 1, "DeLoreanParts", "AutoShop", 1000.0),
(2, 1, "SkateboardUpgrade", "SportsStore", 150.0),
(3, 2, "PlutoniumPurchase", "FLAG:plutonium-256", 5000.0),
(4, 2, "FluxCapacitor", "InnovativeTech", 3000.0),
(5, 3, "SportsAlmanac", "RareBooks", 200.0),
(6, 4, "WritingSupplies", "OfficeStore", 40.0),
(7, 4, "SciFiNovels", "BookShop", 60.0)
]
cursor.executemany("INSERT OR IGNORE INTO Transactions (transactionId, userId, reference, recipient, amount) VALUES (?, ?, ?, ?, ?)", transactions)
self.conn.commit()
def get_user_transactions(self, userId):
cursor = self.conn.cursor()
cursor.execute(f"SELECT * FROM Transactions WHERE userId = '{str(userId)}'")
rows = cursor.fetchall()
# Get column names
columns = [column[0] for column in cursor.description]
# Convert rows to dictionaries with column names as keys
transactions = [dict(zip(columns, row)) for row in rows]
# Convert to JSON format
return json.dumps(transactions, indent=4)
def get_user(self, user_id):
cursor = self.conn.cursor()
cursor.execute(
f"SELECT userId,username FROM Users WHERE userId = {str(user_id)}"
)
rows = cursor.fetchall()
# Get column names
columns = [column[0] for column in cursor.description]
# Convert rows to dictionaries with column names as keys
users = [dict(zip(columns, row)) for row in rows]
# Convert to JSON format
return json.dumps(users, indent=4)
def close(self):
self.conn.close()