diff --git a/server.py b/server.py index 0a0b278..9d048af 100644 --- a/server.py +++ b/server.py @@ -14,6 +14,7 @@ from flask import send_from_directory from flask import url_for from config import database +import top db = MySQLdb.connect(database["host"], database["user"], @@ -28,7 +29,7 @@ app.config.update( @app.route('/api/connection/', methods=["GET"]) -def hello_world(): +def getConnection(): if not request.args \ or 'from' not in request.args or 'to' not in request.args \ or not request.args["from"].isdigit() or not request.args["to"].isdigit(): @@ -70,6 +71,14 @@ def get_connection(connection_id): return jsonify(geojson) +@app.route('/api/top/', methods=["GET"]) +def get_top(): + if not request.args \ + or 'type' not in request.args: + abort(400) + return jsonify(top.helloworld(cur, request.args["type"])) + + @app.route('/') def send_main_html(): return send_file('www/map.html') diff --git a/top.py b/top.py new file mode 100644 index 0000000..de3c552 --- /dev/null +++ b/top.py @@ -0,0 +1,35 @@ +topSQL = { + "shortestConnections": """ +SELECT + id, + s.name, + g.name, + length + +FROM connections + LEFT JOIN stationen AS s ON start = s.ref + LEFT JOIN stationen AS g ON goal = g.ref + +WHERE start != goal +ORDER BY length ASC +LIMIT 10 +""", + "farAway": """ +SELECT * +FROM (SELECT + name, + length, + ref + FROM connections + LEFT JOIN stationen ON (start = ref OR goal = ref) + WHERE start != goal + ORDER BY length ASC) AS x +GROUP BY ref +ORDER BY length DESC""" +} + + +def helloworld(cursor, top): + cursor.execute(topSQL[top]) + + return cursor.fetchall() diff --git a/www/map.html b/www/map.html index ffb5c1c..3e39725 100644 --- a/www/map.html +++ b/www/map.html @@ -6,6 +6,8 @@ + -