-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAPI.py
More file actions
72 lines (52 loc) · 1.97 KB
/
API.py
File metadata and controls
72 lines (52 loc) · 1.97 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 flask import Flask, jsonify, send_from_directory
from flask_cors import CORS, cross_origin
import sqlite3
app = Flask(__name__, static_url_path='/', static_folder='./build')
CORS(app) # <- Set up CORS for the entire app
@app.route('/')
def index():
return app.send_static_file('index.html')
@app.errorhandler(404)
def not_found(err):
return app.send_static_file('index.html')
@app.route('/<path:path>')
def static_proxy(path):
# send all other requests to the React app to handle.
file_name = path.split('/')[-1]
dir_name = os.path.join(app.static_folder, '/'.join(path.split('/')[:-1]))
return send_from_directory(dir_name, file_name)
@app.route('/api/study_spots')
def get_study_spots():
# Connect to the SQLite database
conn = sqlite3.connect('my_database.db')
conn.row_factory = sqlite3.Row
cur = conn.cursor()
# Execute a query to get all study spots
cur.execute("SELECT * FROM study_spots")
# Fetch all results
rows = cur.fetchall()
# Convert the rows into a list of dicts to jsonify it later
study_spots = [dict(row) for row in rows]
# Close the connection
conn.close()
# Return the list of study spots as JSON
return jsonify(study_spots)
@app.route('/api/study_spots/building/<building_name>')
def get_study_spots_by_building(building_name):
# Connect to the SQLite database
conn = sqlite3.connect('my_database.db')
conn.row_factory = sqlite3.Row
cur = conn.cursor()
# Execute the query to get study spots by building name
cur.execute("SELECT * FROM study_spots WHERE building = ?", (building_name,))
# Fetch all results
rows = cur.fetchall()
# Convert the rows into a list of dicts to jsonify it later
study_spots_by_building = [dict(row) for row in rows]
# Close the connection
conn.close()
# Return the list of study spots by building as JSON
return jsonify(study_spots_by_building)
if __name__ == '__main__':
app.run(debug=True)