-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
130 lines (96 loc) · 3.35 KB
/
main.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
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
from flask import Flask, jsonify, request
import json
import os
import psycopg2
import psycopg2.extras
# Create a Flask server.
app = Flask(__name__)
# Create a cursor and initialize psycopg
pg_conn_string = os.environ["PG_CONN_STRING"]
connection = psycopg2.connect(pg_conn_string)
# Set to automatically commit each statement
connection.set_session(autocommit=True)
cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
"""
CREATE TABLE airbnbs (
id SERIAL PRIMARY KEY,
title STRING,
neighbourhood_group STRING,
neighbourhood STRING,
host_name STRING,
verified BOOL,
year INT
)"
"""
def db_get_all():
cursor.execute('SELECT * FROM airbnbs')
results = cursor.fetchall()
return results
def db_get_by_id(id):
cursor.execute('SELECT * FROM airbnbs WHERE id = %s', (id, ))
result = cursor.fetchone()
return result
def db_filter_listings(min_year, group):
cursor.execute(
'SELECT * FROM airbnbs WHERE neighbourhood_group = %s AND year >= %s',
(group, min_year))
result = cursor.fetchall()
return result
def db_create_airbnb(title, name, neighbourhood, neighbourhood_group, verified,
year):
cursor.execute(
"INSERT INTO airbnbs (title, host_name, neighbourhood, neighbourhood_group, verified, year) VALUES (%s, %s, %s, %s, %s, %s) RETURNING id",
(title, name, neighbourhood, neighbourhood_group, verified, year))
result = cursor.fetchall()
return result
def db_update_title(id, new_title):
cursor.execute("UPDATE airbnbs SET title = %s WHERE id = %s RETURNING id",
(new_title, id))
result = cursor.fetchall()
return result
def db_delete_listing(id):
cursor.execute("DELETE FROM airbnbs WHERE id = %s RETURNING id", (id, ))
result = cursor.fetchall()
return result
# Routes!
@app.route('/', methods=['GET'])
def index():
return jsonify(db_get_all())
@app.route("/<id>", methods=['GET'])
def get_by_id(id):
airbnb = db_get_by_id(id)
if not airbnb:
return jsonify({"error": "invalid id", "code": 404})
return jsonify(airbnb)
@app.route("/search", methods=['GET'])
def filter_listings():
result = db_filter_listings(int(request.args.get('min_year')),
request.args.get('group'))
return jsonify(result)
@app.route("/", methods=['POST'])
def create_airbnb():
new_airbnb = request.json
try:
res = db_create_airbnb(new_airbnb['title'], new_airbnb['name'],
new_airbnb['neighbourhood'],
new_airbnb['neighbourhood_group'],
new_airbnb['verified'], new_airbnb['year'])
return jsonify(res)
except Exception as e:
return jsonify({"error": str(e)})
@app.route("/<id>", methods=['PUT'])
def update_title(id):
try:
title = request.json['title']
return jsonify(db_update_title(id, title))
except Exception as e:
return jsonify({"error": str(e)})
@app.route("/<id>", methods=['DELETE'])
def delete_book(id):
try:
return jsonify(db_delete_listing(id))
except Exception as e:
return jsonify({"error": str(e)})
# Runs the API and exposes it on https://<repl name>.<replit username>.repl.co
# ex. Mine deploys to https://htn-api.jayantsh.repl.co.
app.run(host="0.0.0.0", debug=True)