diff --git a/test/database.py b/test/database.py new file mode 100644 index 0000000..ecece12 --- /dev/null +++ b/test/database.py @@ -0,0 +1,7 @@ +import sqlite3 + +conn = sqlite3.connect("chat.db") +c = conn.cursor() +c.execute("CREATE TABLE messages (username text, message text, timestamp text)") +conn.commit() +conn.close() diff --git a/test/templates/chat.html b/test/templates/chat.html deleted file mode 100644 index b7c8912..0000000 --- a/test/templates/chat.html +++ /dev/null @@ -1,70 +0,0 @@ - - - - - Chat - - - -
- -
-
- - -
- - diff --git a/test/templates/enter_chat.html b/test/templates/enter_chat.html deleted file mode 100644 index 35e6aa7..0000000 --- a/test/templates/enter_chat.html +++ /dev/null @@ -1,18 +0,0 @@ - - - - Chat Room - - -

Welcome to the Chat Room

-
- - -
- - -
- -
- - diff --git a/test/templates/index.html b/test/templates/index.html index 835e642..c7cbbde 100644 --- a/test/templates/index.html +++ b/test/templates/index.html @@ -1,15 +1,83 @@ - - - - Chat - - -
- - - -
- - + + + + + + +
+ + + + diff --git a/test/templates/messages.html b/test/templates/messages.html new file mode 100644 index 0000000..d64cdd8 --- /dev/null +++ b/test/templates/messages.html @@ -0,0 +1,3 @@ +{% for message in messages %} +

{{ message[0] }}: {{ message[1] }} ({{ message[2] }})

+{% endfor %} diff --git a/test/test.py b/test/test.py index b7f8abc..42d8303 100644 --- a/test/test.py +++ b/test/test.py @@ -1,52 +1,34 @@ -from flask import Flask, render_template, request, jsonify +from flask import Flask, request, render_template import sqlite3 app = Flask(__name__) -# Verbindung zur Datenbank herstellen -conn = sqlite3.connect('chat.db', check_same_thread=False) -cursor = conn.cursor() - -# Tabelle erstellen, falls sie nicht existiert -cursor.execute('''CREATE TABLE IF NOT EXISTS messages - (id INTEGER PRIMARY KEY, username TEXT, message TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP)''') -conn.commit() - -@app.route('/') +@app.route("/") def index(): - return render_template('index.html') + return render_template("index.html") -@app.route("/enter_chat", methods=["GET", "POST"]) -def enter_chat(): - if request.method == "POST": - # Handle POST request - pass - elif request.method == "GET": - # Handle GET request - return render_template("enter_chat.html") - - - -@app.route('/chat') -def chat(): - return render_template('chat.html') - -@app.route('/send_message', methods=['POST']) -def send_message(): - username = request.form.get('username') - message = request.form.get('message') - - # Nachricht in die Datenbank einfügen - cursor.execute("INSERT INTO messages (username, message) VALUES (?,?)", (username, message)) +@app.route("/send", methods=["POST"]) +def send(): + message = request.form["message"] + username = request.form["username"] + + conn = sqlite3.connect("chat.db") + c = conn.cursor() + c.execute("INSERT INTO messages (username, message) VALUES (?, ?)", (username, message)) conn.commit() + conn.close() + + return render_template("index.html") - return jsonify({'status': 'OK'}) +@app.route("/get") +def get(): + conn = sqlite3.connect("chat.db") + c = conn.cursor() + c.execute("SELECT username, message, timestamp FROM messages") + messages = c.fetchall() + conn.close() + + return render_template("messages.html", messages=messages) -@app.route('/get_messages') -def get_messages(): - cursor.execute("SELECT * FROM messages ORDER BY timestamp ASC") - messages = cursor.fetchall() - return jsonify({'messages': messages}) - -if __name__ == '__main__': - app.run() \ No newline at end of file +if __name__ == "__main__": + app.run()