-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
264 lines (195 loc) · 6.88 KB
/
Copy pathapp.py
File metadata and controls
264 lines (195 loc) · 6.88 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
from flask import Flask, jsonify, request
from flask_restful import Api, Resource
from pymongo import MongoClient
import bcrypt
app = Flask(__name__)
api = Api(app)
client = MongoClient("mongodb://db:27017")
db = client.MoneyManagementDB
users = db["Users"]
def UserExist(username):
if users.find({"Username":username}).count() == 0:
return False
else:
return True
class Register(Resource):
def post(self):
#Step 1 is to get posted data by the user
postedData = request.get_json()
#Get the data
username = postedData["username"]
password = postedData["password"]
email = postedData["email"]
if UserExist(username):
retJson = {
'status':301,
'msg': 'Invalid Username'
}
return jsonify(retJson)
hashed_pw = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
#Store username and pw into the database
users.insert({
"Username": username,
"Password": hashed_pw,
"email":email,
"Own":0,
"Debt":0
})
retJson = {
"status": 200,
"msg": "You successfully signed up for the API"
}
return jsonify(retJson)
def verifyPw(username, password):
if not UserExist(username):
return False
hashed_pw = users.find({
"Username":username
})[0]["Password"]
if bcrypt.hashpw(password.encode('utf8'), hashed_pw) == hashed_pw:
return True
else:
return False
def cashWithUser(username):
cash = users.find({
"Username":username
})[0]["Own"]
return cash
def debtWithUser(username):
debt = users.find({
"Username":username
})[0]["Debt"]
return debt
def generateReturnDictionary(status, msg):
retJson = {
"status": status,
"msg": msg
}
return retJson
def verifyCredentials(username, password):
if not UserExist(username):
return generateReturnDictionary(301, "Invalid Username"), True
correct_pw = verifyPw(username, password)
if not correct_pw:
return generateReturnDictionary(302, "Incorrect Password"), True
return None, False
def updateAccount(username, balance):
users.update({
"Username": username
},{
"$set":{
"Own": balance
}
})
def updateDebt(username, balance):
users.update({
"Username": username
},{
"$set":{
"Debt": balance
}
})
class Add(Resource):
def post(self):
postedData = request.get_json()
username = postedData["username"]
password = postedData["password"]
money = postedData["amount"]
retJson, error = verifyCredentials(username, password)
if error:
return jsonify(retJson)
if money<=0:
return jsonify(generateReturnDictionary(304, "The money amount entered must be greater than 0"))
cash = cashWithUser(username)
money-= 1 #Transaction fee
#Add transaction fee to bank account
bank_cash = cashWithUser("BANK")
updateAccount("BANK", bank_cash+1)
#Add remaining to user
updateAccount(username, cash+money)
return jsonify(generateReturnDictionary(200, "Amount Added Successfully to account"))
class Transfer(Resource):
def post(self):
postedData = request.get_json()
username = postedData["username"]
password = postedData["password"]
to = postedData["to"]
money = postedData["amount"]
retJson, error = verifyCredentials(username, password)
if error:
return jsonify(retJson)
cash = cashWithUser(username)
if cash <= 0:
return jsonify(generateReturnDictionary(303, "You are out of money, please Add Cash or take a loan"))
if money<=0:
return jsonify(generateReturnDictionary(304, "The money amount entered must be greater than 0"))
if not UserExist(to):
return jsonify(generateReturnDictionary(301, "Recieved username is invalid"))
cash_from = cashWithUser(username)
cash_to = cashWithUser(to)
bank_cash = cashWithUser("BANK")
updateAccount("BANK", bank_cash+1)
updateAccount(to, cash_to+money-1)
updateAccount(username, cash_from - money)
retJson = {
"status":200,
"msg": "Amount added successfully to account"
}
return jsonify(generateReturnDictionary(200, "Amount added successfully to account"))
class Balance(Resource):
def post(self):
postedData = request.get_json()
username = postedData["username"]
password = postedData["password"]
retJson, error = verifyCredentials(username, password)
if error:
return jsonify(retJson)
retJson = users.find({
"Username": username
},{
"Password": 0, #projection
"_id":0
})[0]
return jsonify(retJson)
class TakeLoan(Resource):
def post(self):
postedData = request.get_json()
username = postedData["username"]
password = postedData["password"]
money = postedData["amount"]
retJson, error = verifyCredentials(username, password)
if error:
return jsonify(retJson)
cash = cashWithUser(username)
debt = debtWithUser(username)
updateAccount(username, cash+money)
updateDebt(username, debt + money)
return jsonify(generateReturnDictionary(200, "Loan Added to Your Account"))
class PayLoan(Resource):
def post(self):
postedData = request.get_json()
username = postedData["username"]
password = postedData["password"]
money = postedData["amount"]
retJson, error = verifyCredentials(username, password)
if error:
return jsonify(retJson)
cash = cashWithUser(username)
if cash < money:
return jsonify(generateReturnDictionary(303, "Not Enough Cash in your account"))
debt = debtWithUser(username)
updateAccount(username, cash-money)
updateDebt(username, debt - money)
return jsonify(generateReturnDictionary(200, "Loan Paid"))
class test(Resource):
def get(self):
return "works fine!!!"
api.add_resource(Register, '/register')
api.add_resource(Add, '/add')
api.add_resource(Transfer, '/transfer')
api.add_resource(Balance, '/balance')
api.add_resource(TakeLoan, '/takeloan')
api.add_resource(PayLoan, '/payloan')
api.add_resource(test, '/')
if __name__=="__main__":
app.run()