diff --git a/server/Flask/main.py b/server/Flask/main.py index 7da2986..c434884 100644 --- a/server/Flask/main.py +++ b/server/Flask/main.py @@ -1,4 +1,4 @@ -from flask import Flask, request +from flask import Flask, request, jsonify import mysql.connector from queries import * app = Flask(__name__) @@ -10,27 +10,68 @@ def index(): MAC = request.args.get('MAC', default = None) return getData(node, dataType, MAC) -def getData(node, dataType, MAC): - try: +@app.route('/putData', methods=['PUT']) +def putData(): + node_id = request.json.get('node_id', None) + new_name = request.json.get('new_name', None) + new_location = request.json.get('new_location', None) + + if node_id is None: + return jsonify({"error": "node_id is required"}), 400 + + mydb = mysql.connector.connect( + host="localhost", + user="root", + password="Dingleberries69!", + database="NodeData" + ) + + cursor = mydb.cursor() + + if new_name is not None: + cursor.execute("UPDATE Node SET Name = %s WHERE NodeID = %s", (new_name, node_id)) + mydb.commit() + + if new_location is not None: + cursor.execute("UPDATE Node SET Location = %s WHERE NodeID = %s", (new_location, node_id)) + mydb.commit() + + cursor.close() + mydb.close() + + return jsonify({"message": "Node updated successfully"}), 200 + +def putData(node, name, location, MAC): + mydb = loginDB() + query = get_query(node, name, location, MAC) + cursor = mydb.cursor(dictionary=True) # Enable dictionary output + cursor.execute(query) + result = cursor.fetchall() # Fetch the results + cursor.close() + mydb.close() + + return result + +def loginDB(): mydb = mysql.connector.connect( host="localhost", user="root", password="Dingleberries69!", database="NodeData" ) + return mydb +def getData(node, dataType, MAC): + mydb = loginDB() query = get_query(node, dataType, MAC) cursor = mydb.cursor(dictionary=True) # Enable dictionary output cursor.execute(query) result = cursor.fetchall() # Fetch the results - cursor.close() mydb.close() return result - except mysql.connector.Error as err: - print("MySQL Error:", err) - return "MySQL Error: " + str(err) + if __name__ == '__main__': - app.run(debug=True, host='localhost') \ No newline at end of file + app.run(debug=True, host='localhost')