-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
72 lines (52 loc) · 1.48 KB
/
app.py
File metadata and controls
72 lines (52 loc) · 1.48 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
import os
from dotenv import load_dotenv
from flask import Flask
import mysql.connector
load_dotenv()
MYSQL_HOST = os.getenv('MYSQL_HOST')
MYSQL_PORT = os.getenv('MYSQL_PORT')
MYSQL_USER = os.getenv('MYSQL_USER')
MYSQL_PASSWORD = os.getenv('MYSQL_PASSWORD')
MYSQL_DB = 'LoanLens'
# Set up paths
basedir = os.path.abspath(os.path.dirname(__file__))
# Initialize and configure app
app = Flask(__name__)
def get_db():
mydb = mysql.connector.connect(
host=MYSQL_HOST,
port=MYSQL_PORT,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
database=MYSQL_DB
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM Users")
myresult = mycursor.fetchall()
return myresult
def getAssets(user_id):
mydb = mysql.connector.connect(
host=MYSQL_HOST,
user=MYSQL_USER,
password=MYSQL_PASSWORD,
database=MYSQL_DB
)
mycursor = mydb.cursor()
mycursor.execute("""SELECT u.name, SUM(a.worth) AS total_assets
FROM Users u
LEFT JOIN AssetToOwner ao ON u.user_id = ao.user_id
LEFT JOIN Assets a ON ao.asset_id = a.asset_id
WHERE u.user_id = %u
GROUP BY u.user_id""", (user_id,))
myresult = mycursor.fetchall()
return myresult
@app.route("/")
def hello_world():
res = get_db()
return f"<p>Hello, World!</p> {str(res)}"
@app.route("/userAssets/<user_id>")
def userAssets(user_id):
res = getAssets(user_id)
return res
if __name__ == "__main__":
app.run(host="localhost", port=8000, debug=True)