-
Notifications
You must be signed in to change notification settings - Fork 0
/
Airport.py
executable file
·60 lines (46 loc) · 2.11 KB
/
Airport.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
from flask import Flask, jsonify, abort, request
from Server import *
import ApiFunctions
class Airport:
def __init__(self, id, airport_short, airport_full):
self.id = id
self.airport_short = airport_short
self.airport_full = airport_full
def to_json(self):
return {
'id': self.id,
'airport_short': self.airport_short,
'airport_full': self.airport_full
}
@app.route('/goshna/api/airports', methods=['GET'])
def get_airports():
airports = []
results = ApiFunctions.query_db("SELECT * FROM airports")
for row in results:
airport = Airport(row['id'], row['airport_short'],
row['airport_full'])
airports.append(airport.to_json())
return jsonify({'airports': airports})
@app.route('/goshna/api/airports/<int:airport_id>', methods=['GET'])
def get_airport(airport_id):
row = ApiFunctions.query_db("SELECT * FROM airports WHERE id = ?",
[airport_id], one=True)
if row is None:
abort(404)
airport = Airport(row['id'], row['airport_short'], row['airport_full'])
return jsonify({'airport': airport.to_json()})
@app.route('/goshna/api/airports', methods=['POST'])
def create_airport():
if not request.json or not 'airport_short' in request.json or not 'airport_full' in request.json:
abort(400)
airport_short = request.json['airport_short']
airport_full = request.json['airport_full']
result = ApiFunctions.post_db("INSERT INTO airports VALUES (NULL, ?, ?)", [airport_short, airport_full]);
inserted_id = c.lastrowid
print u'Inserted new airport at row ' + str(inserted_id)
return jsonify({'id': str(inserted_id)}), 201
@app.route('/goshna/api/airports/<int:airport_id>', methods=['DELETE'])
def delete_airport(airport_id):
ApiFunctions.post_db("DELETE FROM airports WHERE id=?", [airport_id])
print u'Deleted airport with ID ' + str(inserted_id)
return jsonify({'result': True})