-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
85 lines (75 loc) · 2.51 KB
/
app.py
File metadata and controls
85 lines (75 loc) · 2.51 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
from flask import Flask, jsonify
import sqlite3
from qrgen import qr
# file_name = qr.QR("data to be stored within QR", "information")
# print(file_name)
app = Flask(__name__)
# conn = sqlite3.connect('db.sqlite')
# cur = conn.cursor()
# # cur.execute("CREATE TABLE parkings (p_id integer, u_id integer, filled boolean)")
# cur.close()
@app.route("/")
def hello():
return "Hi"
@app.route("/parking", methods = ['GET'])
def get_parking():
conn = sqlite3.connect('db.sqlite')
cur = conn.cursor()
cur.execute("select * from parkings")
data = cur.fetchall()
conn.close()
return jsonify({'data': data})
@app.route("/parking/add/<int:p>/<int:u>")
def add(p, u = -1):
conn = sqlite3.connect('db.sqlite')
cur = conn.cursor()
cur.execute("select * from parkings where p_id = ?", (p,))
data = cur.fetchone()
if data:
if len(data) > 0:
return "Already exists"
cur.execute("insert into parkings(u_id, p_id, filled) values(?,?,?)", (u, p, 0,))
conn.commit()
conn.close()
return "Success"
@app.route("/parking/fill/<int:parking_id>/<int:user_id>")
def fill_parking(parking_id, user_id):
conn = sqlite3.connect('db.sqlite')
cur = conn.cursor()
cur.execute("select * from parkings where u_id = ?", (user_id,))
data = cur.fetchone()
if data:
if len(data) > 0:
return "You have already parked your car"
cur.execute("select * from parkings where p_id = ?", (parking_id,))
data = cur.fetchone()
if data:
if data[2] == 1:
return "Already parked at this place"
cur.execute("update parkings set u_id = ?, filled = ? where p_id = ?", (user_id, 1, parking_id,))
conn.commit()
conn.close()
return "Success"
@app.route("/parking/delete/<int:user_id>", methods=['GET'])
def remove_parking(user_id):
conn = sqlite3.connect('db.sqlite')
cur = conn.cursor()
cur.execute("update parkings set u_id = ?, filled = ? where u_id = ?", (-1, 0, user_id,))
conn.commit()
conn.close()
return "Success"
@app.route("/parking/find/<int:user_id>", methods=['GET'])
def find_parking(user_id, current_position = ""):
conn = sqlite3.connect('db.sqlite')
cur = conn.cursor()
cur.execute("select * from parkings where u_id = ?", (user_id,))
conn.commit()
data = cur.fetchone()
conn.close()
if len(data) > 0:
return jsonify(data[0])
else:
return "Not found"
# navigate current_position to parking_id
if __name__ == '__main__':
app.run(debug=True, port=8080)