-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (58 loc) · 2.37 KB
/
app.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
from flask import Flask, request, jsonify, render_template
import mysql.connector
app = Flask(__name__, static_url_path='/static')
# Database connection
def get_db_connection():
return mysql.connector.connect(
host="localhost",
user="root",
password="keeru2004",
database="new"
)
# Function to get customers
def get_customers():
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute("SELECT email, name, current_balance FROM bank")
customers = cursor.fetchall()
conn.close()
return customers
@app.route('/')
def index():
customers = get_customers()
return render_template('index1.html', customers=customers)
@app.route('/transfer', methods=['POST'])
def transfer():
data = request.get_json()
from_email = data['fromEmail']
to_email = data['toEmail']
amount = float(data['amount'])
conn = get_db_connection()
cursor = conn.cursor()
# Check if from_email exists
cursor.execute("SELECT current_balance FROM bank WHERE email = %s", (from_email,))
from_balance = cursor.fetchone()
if from_balance is None:
app.logger.error(f"Sender email not found: {from_email}")
return jsonify({'message': 'Invalid email address for sender.'}), 400
# Check if to_email exists
cursor.execute("SELECT current_balance FROM bank WHERE email = %s", (to_email,))
to_balance = cursor.fetchone()
if to_balance is None:
app.logger.error(f"Recipient email not found: {to_email}")
return jsonify({'message': 'Invalid email address for recipient.'}), 400
# Check for sufficient balance
if from_balance[0] < amount:
app.logger.error(f"Insufficient balance for email: {from_email}")
return jsonify({'message': 'Insufficient balance.'}), 400
# Perform the transfer
new_from_balance = from_balance[0] - amount
new_to_balance = to_balance[0] + amount
cursor.execute("UPDATE bank SET current_balance = %s WHERE email = %s", (new_from_balance, from_email))
cursor.execute("UPDATE bank SET current_balance = %s WHERE email = %s", (new_to_balance, to_email))
conn.commit()
cursor.close()
conn.close()
return jsonify({'message': f'{amount} has been transferred from {from_email} to {to_email}.'})
if __name__ == '__main__':
app.run(debug=True)