Skip to content

Commit

Permalink
Create app.py
Browse files Browse the repository at this point in the history
投票数を記録
  • Loading branch information
tamachika authored Sep 28, 2023
1 parent 5b925ad commit 137a08c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app.py
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()

0 comments on commit 137a08c

Please sign in to comment.