-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbmanager.py
251 lines (175 loc) · 5.74 KB
/
dbmanager.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
import sqlite3
conn = sqlite3.connect('trader.db')
c = conn.cursor()
from get_lastprice import Markit
class UserDBManager:
def check_username(self, username):
c.execute("""SELECT username
FROM users;""")
usernames = c.fetchall()
username_list = [username[0] for username in usernames]
if username in username_list:
return False
else:
return True
def check_password(self, username, password):
querey_list = ["SELECT username, password FROM users WHERE username = '", username, "';"]
querey_string = "".join(querey_list)
c.execute(querey_string)
password_in_tuple = c.fetchall()[0][1]
if password == password_in_tuple:
return True
else:
return False
def create_client(self, username, password):
c.execute("""INSERT INTO users
(username, password, permission_level)
VALUES
(?, ?, ?)
""",
(
username, password, 'client'
)
)
conn.commit()
def create_account (self, username):
query = "SELECT id FROM users WHERE username = '" + str(username) + "';"
c.execute(query)
client_id = (c.fetchall())[0][0]
c.execute("""INSERT INTO accounts
(cash, portfolio_worth, user_id)
VALUES
(?, ?, ?)
""",
(
100000, 0, client_id
)
)
conn.commit()
def create_admin(self, username, password):
c.execute("""INSERT INTO users
(username, password, permission_level)
VALUES
(?, ?, ?)
""",
(
username, password, 'admin'
)
)
conn.commit()
def find_permission_level(self, username):
query = "SELECT permission_level FROM users WHERE username = '" + str (username) + "' ;"
c.execute(query)
#get class id
permission_level = (c.fetchall())[0][0]
return permission_level
def get_user_id(self,username):
query = "SELECT id FROM users WHERE username = '" + str (username) + "' ;"
c.execute(query)
#get class id
client_id = (c.fetchall())[0][0]
return client_id
def view_account(self, user_id):
query = "SELECT * FROM accounts WHERE user_id = " + str (user_id) + " ;"
c.execute(query)
#get class id
#list with tuple (id, balance, account_number, user_id )
return c.fetchall()
def view_dashboard(self, username):
query = "SELECT id FROM users WHERE username ='" + str(username) + "' ;"
c.execute(query)
user_id = (c.fetchall())[0][0]
query_ = "SELECT cash, portfolio_worth FROM accounts WHERE user_id = " + str (user_id) + " ;"
c.execute(query_)
info = c.fetchall()
cash = info[0][0]
portfolio_worth = info[0][1]
initial_investment = 100000 - cash
amount = portfolio_worth - initial_investment
return {"Cash": cash, "Portfolio Worth": portfolio_worth, "Amount earned/lost":amount}
def view_leaderboard(self):
query = "SELECT users.username, accounts.portfolio_worth from users INNER JOIN accounts ON users.id = accounts.user_id ORDER BY portfolio_worth DESC LIMIT 10;"
c.execute(query)
return c.fetchall
class TransactionsDBManager:
def buy(self, username, num_shares, ticker, last_price):
#get user_id
query = "SELECT id FROM users WHERE username ='" + str(username) + "' ;"
c.execute(query)
info = c.fetchall()
user_id = info[0][0]
#get cash, portfolio worth
query_ = "SELECT cash, portfolio_worth, id from accounts WHERE user_id = " + str(user_id) + " ;"
c.execute(query_)
info = c.fetchall()
cash = info[0][0]
portfolio_worth = info[0][1]
account_id = info[0][2]
cost = float(num_shares) * float(last_price)
new_cash = float(cash) - float(cost)
#check if user has enough money
if new_cash < 0:
return False
new_portfolio_worth = float(portfolio_worth) + float(cost)
#transaction
c.execute("""INSERT INTO transactions
(type, account_id, ticker, num_shares)
VALUES
(?, ?, ?, ?)
""",
(
"buy", account_id, ticker, num_shares
)
)
conn.commit()
#update cash in account
query2 = "UPDATE accounts SET cash = " + str(new_cash) + " WHERE id = " + str(account_id) + " ;"
c.execute(query2)
#update portfolio worth in account
query3 = "UPDATE accounts SET portfolio_worth = " + str(new_portfolio_worth) + " WHERE id = " + str(account_id) + " ;"
c.execute(query3)
# portfolio_list = []
# total_invested = (number * get_info())
# if account_total - total_invested > 0:
# portfolio_list.append(total_invested)
# account_total = account_total - total_invested
# else:
# return("You do not have enough money in your account to complete the transaction.")
# #number of shares*current price
#get_info()
#Buying should subtract from their funds and not let them buy more than they can afford,
return True
def sell(self, username, num_shares, ticker, last_price):
#get user_id
query = "SELECT id FROM users WHERE username ='" + str(username) + "' ;"
c.execute(query)
info = c.fetchall()
print(info)
user_id = info[0][0]
#get cash, portfolio worth
query_ = "SELECT cash, portfolio_worth, id from accounts WHERE user_id = " + str(user_id) + " ;"
c.execute(query_)
info = c.fetchall()
cash = info[0][0]
portfolio_worth = info[0][1]
account_id = info[0][2]
cost = float(num_shares) * float(last_price)
new_cash = float(cash) + float(cost)
new_portfolio_worth = float(portfolio_worth) - float(cost)
#transaction
c.execute("""INSERT INTO transactions
(type, account_id, ticker, num_shares)
VALUES
(?, ?, ?, ?)
""",
(
"sell", account_id, ticker, num_shares
)
)
conn.commit()
#update cash in account
query2 = "UPDATE accounts SET cash = " + str(new_cash) + " WHERE id = " + str(account_id) + " ;"
c.execute(query2)
#update portfolio worth in account
query3 = "UPDATE accounts SET portfolio_worth = " + str(new_portfolio_worth) + " WHERE id = " + str(account_id) + " ;"
c.execute(query3)