From 137a08cd851d03b13c56cbfe6f4794f43b3cb0ed Mon Sep 17 00:00:00 2001 From: tamachika <62124334+tamachika@users.noreply.github.com> Date: Thu, 28 Sep 2023 13:11:50 +0900 Subject: [PATCH] Create app.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 投票数を記録 --- app.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 app.py diff --git a/app.py b/app.py new file mode 100644 index 0000000..eacde64 --- /dev/null +++ b/app.py @@ -0,0 +1,41 @@ +# app.py + +from flask import Flask, request, jsonify +import sqlite3 + +app = Flask(__name__) + +# SQLiteデータベースのセットアップ +conn = sqlite3.connect('votes.db') +cursor = conn.cursor() +cursor.execute('''CREATE TABLE IF NOT EXISTS votes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + userId TEXT NOT NULL, + vote TEXT NOT NULL, + voteTimestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )''') +conn.commit() +conn.close() + +@app.route('/record-vote', methods=['POST']) +def record_vote(): + data = request.json + + if 'userId' in data and 'vote' in data: + userId = data['userId'] + vote = data['vote'] + + conn = sqlite3.connect('votes.db') + cursor = conn.cursor() + cursor.execute("INSERT INTO votes (userId, vote) VALUES (?, ?)", (userId, vote)) + conn.commit() + conn.close() + + response = {"success": True} + else: + response = {"success": False, "error": "Invalid data"} + + return jsonify(response) + +if __name__ == '__main__': + app.run()