-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
147 lines (117 loc) · 4.64 KB
/
app.py
File metadata and controls
147 lines (117 loc) · 4.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
import os
from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
from sqlalchemy.sql import text
from dotenv import load_dotenv
import psycopg2
import uuid
import models
from load import Loader
load_dotenv()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DATABASE_URL')
db = SQLAlchemy(app)
print ('\nLoading data into db tables . . . ')
loader = Loader()
loader.load_data()
print ('Done!')
session = loader.session
POSTGRES_DB = os.getenv('DATABASE')
HOST = os.getenv('HOST')
PORT = os.getenv('PORT')
POSTGRES_USER = os.getenv('USER')
POSTRES_PW = os.getenv('PASSWORD')
connection = psycopg2.connect(
host=HOST,
port=PORT,
database=POSTGRES_DB,
user=POSTGRES_USER,
password=POSTRES_PW,
)
connection.set_session(autocommit=True)
print('\n\nFlask app is ready to use. \n\n')
GET_INVESTORS = ("""
SELECT c.investors FROM companies c
LEFT JOIN company_li_names ON c.company_name = company_li_names.company_name
WHERE
company_li_names.company_li_name =(%s) and c.investors is not null
GROUP BY
c.investors""")
GET_COMPANIES = ("""
SELECT people.company_name FROM people
WHERE person_id =(%s); """)
GET_AVG_FUNDING_BY_PERSON = ("""
SELECT
avg(companies.known_total_funding)
FROM
people
INNER JOIN company_li_names ON people.company_li_name = company_li_names.company_li_name
INNER JOIN companies ON company_li_names.company_name = companies.company_name
WHERE (companies.company_name = people.company_name
OR company_li_names.company_li_name = people.company_li_name)
AND person_id = (%s)
""")
@app.get('/')
def home():
return ('Hello World!')
@app.get('/companies-by-person/<given_person_id>')
def get_companies_by_person(given_person_id):
person_id = None
if given_person_id:
person_id = given_person_id
else:
data = request.get_json()
person_id = data['person_id']
try:
uuid.UUID(str(person_id))
except ValueError:
return {"statusCode": 400, "Errors" : {"message": 'Bad Request: Invalid uuid person_id: ' +person_id}}
exists = db.session.query(models.Person.person_id).filter_by(person_id=person_id).first() is not None
if not exists:
return { "investors": [], "statusCode":404, "Errors": {"Message": "Not found: Person_id does not exist in the db."}}
with connection:
with connection.cursor() as cursor:
cursor.execute(GET_COMPANIES,(person_id,))
companies = [item for sublist in cursor.fetchall() for item in sublist]
return {"statusCode":200, "Response":{"companies":companies}, "Errors": {}}
@app.get('/avg-funding-by-person/<given_person_id>')
def get_average_funding_by_person(given_person_id):
person_id = None
if given_person_id:
person_id = given_person_id
else:
try:
data = request.get_json()
person_id = data['person_id']
except:
return {"statusCode": 400, "message":"Bad Request: Invalid number of parameters."}
try:
uuid.UUID(str(person_id))
except ValueError:
return {"statusCode": 400, "Errors": { "message": 'Invalid uuid person_id: ' +person_id}}
exists = db.session.query(models.Person.person_id).filter_by(person_id=person_id).first() is not None
if not exists:
return { "investors": [], "statusCode":404, "Errors": {"Message": "Not Found: Person_id does not exist in the db."}}
with connection:
with connection.cursor() as cursor:
cursor.execute(GET_AVG_FUNDING_BY_PERSON,(person_id,))
avg_funding = cursor.fetchone()[0]
return {"statusCode":200, "Response":{"avg_funding":avg_funding}, "Errors":{}}
@app.get('/investors-by-company/<given_company_li_name>')
def get_investors_by_company_li_name(given_company_li_name):
company_li_name = None
if given_company_li_name:
company_li_name = given_company_li_name
exists = db.session.query(models.Company_Li_Mapping.company_name).filter_by(company_li_name=company_li_name).first() is not None
if exists is None:
return { "Response: ": [], "statusCode":404, "Errors": {"Message": "Not Found: Company_li_name does not exist in the db."}}
with connection:
with connection.cursor() as cursor:
cursor.execute(GET_INVESTORS, (company_li_name,))
investors = cursor.fetchone()
if investors is None:
investors = []
else:
investors = investors[0].replace('\n','').replace('"','').replace(' ','').lstrip('[').lstrip(']').split(',')
return { "Response": {"investors":investors}, "statusCode":200, "Errors": {}}