-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrollers.py
More file actions
219 lines (173 loc) · 6.64 KB
/
controllers.py
File metadata and controls
219 lines (173 loc) · 6.64 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
from models import db, Account, Transaction, User, validate_email, validate_transaction_action, get_account_or_404
from flask import jsonify
def get_account(email):
account = get_account_or_404(email)
if not account:
return {'message': 'Account not found'}, 404
return {
'account_id': account.account_id,
'name': account.name,
'email': email,
'balance': account.balance
}, 200
def create_login_account(data):
if not validate_email(data):
return {'message': 'Email already exists'}, 400
print(data)
user_account = User(
username=data['username'],
email=data['email'],
password=data['password']
)
db.session.add(user_account)
try:
db.session.commit()
return jsonify({
'account_id': user_account.account_id,
'username': user_account.username,
'email': user_account.email,
'message': 'Account created successfully'
}, 201)
except Exception as e:
db.session.rollback()
return jsonify({'message': 'An error occurred while saving the account'}, 500)
def login_validation(data):
if not data or 'username' not in data or 'password' not in data:
return {'message': 'Missing required fields: username and password required'}, 400
user = User.query.filter_by(username=data['username']).first()
if not user:
return {'message': 'Invalid username'}, 401
return user.password
def get_user_id_by_email(email):
user = User.query.filter_by(email=email).first()
if user:
return user.user_id
return None
def get_email_by_username(username):
user = User.query.filter_by(username=username).first()
if user:
return user.email
return None
def get_account_id_by_email(email):
account = Account.query.filter_by(email=email).first()
if account:
return account.account_id
return None
def create_account(data):
if not data or 'name' not in data or 'email' not in data:
return {'message': 'Missing required fields: name and email required'}, 400
initial_balance = data.get('balance', 0.0)
if not isinstance(initial_balance, (int, float)) or initial_balance < 0:
return {'message': 'Balance must be a non-negative number'}, 400
user_id = get_user_id_by_email(data['email'])
account = Account(
name=data['name'],
user_id=user_id,
account_type=data['account_type'],
balance=initial_balance
)
print(account.name,account.account_id)
db.session.add(account)
try:
db.session.commit()
return {
'account_id': account.account_id,
'name': account.name,
'balance': account.balance,
'account_type': account.account_type,
'message': 'Account created successfully'
}, 201
except Exception as e:
db.session.rollback()
print(e)
return {'message': f'An error occurred while saving the account'}, 500
def update_account(email, data):
account = get_account_or_404(email)
if 'name' in data:
if data["name"].strip() != "":
account.name = data['name']
# if 'email' in data:
# if not validate_email(data) and data['email'].strip() != "":
# return {'message': 'Email already exists'}, 400
if data['email'].strip() != "":
account.email = data['email']
try:
db.session.commit()
return {
'account_id': account.account_id,
'name': account.name,
'email': email,
'balance': account.balance,
'message': 'Account updated successfully'
}, 201
except Exception:
db.session.rollback()
return {'message': 'An error occurred while updating the account'}, 500
def update_login_account(email, data):
user = User.query.filter_by(email=email).first()
if not user:
return {'message': 'User not found'}, 404
if 'username' in data:
user.username = data['username']
if 'email' in data:
user.email = data['email']
try:
db.session.commit()
return {'message': 'User updated successfully'}, 201
except Exception:
db.session.rollback()
return {'message': 'An error occurred while updating the user'}, 500
def delete_account(account_id):
# Retrieve the account or raise a 404 error if not found
account = Account.query.filter_by(account_id=account_id).first()
# Attempt to delete the account
db.session.delete(account)
try:
# Commit the changes to the database
db.session.commit()
return {'message': 'Account deleted successfully'}, 204
except Exception as e:
# Rollback the session in case of an error
db.session.rollback()
return {'message': f'An error occurred while deleting the account: {str(e)} {account}'}, 500
def process_transaction(action, email, amount, account_id, date):
account = get_account_or_404(email)
if not account:
return {'message': 'Account not found'}, 404
try:
if not validate_transaction_action(action, amount, account.balance):
return {'message': f'Invalid transaction action {amount}'}, 400
transaction = Transaction(account_id=account_id, amount=amount, action=action, date=date)
db.session.add(transaction)
if action == 'deposit':
account.balance += amount
elif action == 'withdraw':
account.balance -= amount
db.session.commit()
return jsonify([{
"message": f"{action.capitalize()} successful",
"account_id": account.account_id,
"balance": account.balance
}, 200])
except Exception as e:
db.session.rollback()
return {'message': f'An error occurred while processing transaction: {str(e)}'}, 500
def get_transactions(email):
account = get_account_or_404(email)
if not account:
return {'message': 'Account not found'}, 404
try:
transactions = Transaction.query.filter_by(account_id=account.account_id)\
.order_by(Transaction.date.desc())\
.all()
# Serialize transactions
transactions_list = [{
'transaction_id': t.transaction_id,
'account_id': t.account_id,
'amount': t.amount,
'action': t.action,
'date': t.date
} for t in transactions]
return jsonify(transactions_list)
except Exception as e:
return {'message': f'Error fetching transactions: {str(e)}'}, 500