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 @@ + -
@@ -224,7 +225,7 @@ return false; } console.info(fromId + "->" + toId); - $.getJSON("http://127.0.0.1:5000/api/connection/", { + $.getJSON("/api/connection/", { from: fromId, to: toId }).done(function (data) { @@ -245,7 +246,6 @@ } ) }) - - + \ No newline at end of file diff --git a/www/sidebar.js b/www/sidebar.js new file mode 100644 index 0000000..4e3f3b1 --- /dev/null +++ b/www/sidebar.js @@ -0,0 +1,43 @@ +var sidebar = L.Control.extend({ + options: { + position: 'bottomright' + }, + onAdd: function (map) { + var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom'); + container.style.backgroundColor = "white"; + container.style.height = '600px'; + container.style.width = '400px'; + var table = L.DomUtil.create("table", "sidebarTable", container); + var button = document.createElement("button"); + button.textContent = "Topliste"; + container.appendChild(button); + + return container; + } +}); +map.addControl(new sidebar); + +$(".leaflet-control-custom button").on("click", function () { + loadSidebar(); +}); + +function loadSidebar() { + $.getJSON("/api/top/", { + type: "shortestConnections" + }).done(function (data) { + var table = $(".sidebarTable"); + for (var i = 0; i < data.length; i++) { + var line = document.createElement("tr"); + var single = data[i]; + console.log(single); + for (var j = 0; j < single.length; j++) { + var td = document.createElement("td"); + td.textContent = single[j]; + line.appendChild(td); + } + table.append(line); + } + + }); + +} \ No newline at end of file