-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
投票数を記録
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |