from flask import Flask, request
import mysql.connector

app = Flask(__name__)

@app.route('/')
def index():
    node = request.args.get('node', default = None)
    dataType = request.args.get('dataType', default = None)
    return getData(node, dataType)

def getData(node, dataType):
    try:
        mydb = mysql.connector.connect(
            host="localhost",
            user="root",
            password="Dingleberries69!",
            database="NodeData"
        )
#turn this into a switch statement
        cursor = mydb.cursor()
        if node:
          query = f"SELECT * FROM Measurement WHERE NodeID = {node}"
        elif dataType:
          query = f"SELECT * FROM Measurement WHERE Type = '{dataType}'"
        else:
          query = "SELECT * FROM `Measurement`"
        cursor.execute(query)
        result = cursor.fetchall()  # Fetch the results

        # Convert the results to a string for display
        result_str = ', '.join([str(row) for row in result])

        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')