-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (71 loc) · 2.41 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
from flask import Flask, jsonify, abort, make_response
from flask import request
import MySQLdb as mdb
app = Flask(__name__) # create the Flask app
db = mdb.connect("129.115.27.67", "iuq276", "kyYstgM5lpci8YaCxT4R", "iuq276")
curs = db.cursor()
app.config["JSON_SORT_KEYS"] = False
@app.route('/')
def hello_world():
return 'Hello World!'
@app.route('/hello')
def hello():
return '[{"message":"hello yourself"}]'
@app.route('/properties', methods=['GET', 'POST'])
def props():
if request.method == 'GET':
# curs = db.cursor()
try:
curs.execute("SELECT * FROM usprops")
rv = curs.fetchall()
l = []
for result in rv:
content = {'id': result[0], 'address': result[1], 'city': result[2], 'state': result[3], 'zip': result[4]}
l.append(content)
return jsonify(l)
except:
return "Error, unable to fetch properties"
curs.close()
db.close()
elif request.method == 'POST':
data = request.get_json()
addr = data['address']
city = data['city']
state = data['state']
zip_code = data['zip']
try:
query = "INSERT INTO usprops(address, city, state, zip) values (%s, %s, %s, %s)"
args = (addr, city, state, zip_code)
curs.execute(query, args)
db.commit()
return "added"
except:
return "something went wrong"
curs.close()
db.close()
else:
return "NOT GET OR POST"
@app.route('/properties/<req_id>', methods=['GET', 'DELETE'])
def get_id(req_id):
if request.method == 'GET':
try:
query = "SELECT * FROM usprops where id = %s"
curs.execute(query, req_id)
rv = curs.fetchall()
l = []
for result in rv:
content = {'id': result[0], 'address': result[1], 'city': result[2], 'state': result[3], 'zip': result[4]}
l.append(content)
return jsonify(l)
except:
return "Error, unable to fetch properties"
elif request.method == 'DELETE':
try:
query = "DELETE FROM usprops where id = %s"
curs.execute(query, req_id)
db.commit()
return "deleted successfully"
except:
return "something went wrong"
if __name__ == '__main__':
app.run()