Spaces:
Sleeping
Sleeping
File size: 4,435 Bytes
19a6cbe 8f913fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
from flask import Flask, request, jsonify
from flask_cors import CORS
import pymongo
from bson.objectid import ObjectId
# Khởi tạo Flask app và cho phép CORS
app = Flask(__name__)
CORS(app)
# MongoDB connection
mongo_url = "mongodb+srv://ip6ofme:[email protected]/"
client = pymongo.MongoClient(mongo_url)
db = client["test"]
pdf_collection = db["PdfDetails"]
voter_collection = db["Voters"]
# API để upload file và lưu thông tin vào MongoDB
@app.route('/upload-files', methods=['POST'])
def upload_file():
# Chỉ lưu thông tin vào MongoDB
title = request.form.get('title')
group = request.form.get('group')
firebase_url = request.form.get('url')
new_pdf = {
'title': title,
'group': group,
'url': firebase_url,
'votes': 0
}
result = pdf_collection.insert_one(new_pdf)
return jsonify({'status': 'ok', 'id': str(result.inserted_id)})
# API để tăng số lượng vote của file dựa trên ID
@app.route('/vote', methods=['POST'])
def vote():
data = request.json
file_id = data.get('id')
try:
file = pdf_collection.find_one({'_id': ObjectId(file_id)})
if not file:
return jsonify({'status': 'error', 'message': 'File not found'}), 404
pdf_collection.update_one({'_id': ObjectId(file_id)}, {'$inc': {'votes': 1}})
return jsonify({'status': 'ok'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
# API để lấy số lượng votes của file theo ID
@app.route('/get-votes', methods=['GET'])
def get_votes():
file_id = request.args.get('id')
try:
file = pdf_collection.find_one({'_id': ObjectId(file_id)})
if not file:
return jsonify({'status': 'error', 'message': 'File not found'}), 404
return jsonify({'status': 'ok', 'votes': file.get('votes', 0)})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
# API để lấy danh sách tất cả các file
@app.route('/get-files', methods=['GET'])
def get_files():
try:
files = pdf_collection.find({})
file_list = []
for file in files:
file_list.append({
'id': str(file['_id']),
'title': file['title'],
'group': file['group'],
'url': file['url'],
'votes': file.get('votes', 0)
})
return jsonify({'status': 'ok', 'data': file_list})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
# API mới để đăng ký người bình chọn
@app.route('/register-voter', methods=['POST'])
def register_voter():
data = request.json
name = data.get('name')
group = data.get('group')
new_voter = {
'name': name,
'group': group,
'number_of_votes': 0
}
result = voter_collection.insert_one(new_voter)
return jsonify({'status': 'ok', 'id': str(result.inserted_id)})
# API mới để bình chọn
@app.route('/vote-by-voter', methods=['POST'])
def vote_by_voter():
data = request.json
voter_id = data.get('voter_id')
file_id = data.get('file_id')
voter = voter_collection.find_one({'_id': ObjectId(voter_id)})
if not voter:
return jsonify({'status': 'error', 'message': 'Voter not found'}), 404
if voter['number_of_votes'] >= 5:
return jsonify({'status': 'error', 'message': 'Maximum votes reached'}), 400
# Tăng số lượt bình chọn của người dùng
voter_collection.update_one({'_id': ObjectId(voter_id)}, {'$inc': {'number_of_votes': 1}})
# Tăng số lượt bình chọn cho file
pdf_collection.update_one({'_id': ObjectId(file_id)}, {'$inc': {'votes': 1}})
return jsonify({'status': 'ok', 'message': 'Vote recorded successfully'})
# API để lấy thông tin người bình chọn
@app.route('/get-voter', methods=['GET'])
def get_voter():
voter_id = request.args.get('id')
voter = voter_collection.find_one({'_id': ObjectId(voter_id)})
if not voter:
return jsonify({'status': 'error', 'message': 'Voter not found'}), 404
return jsonify({
'status': 'ok',
'name': voter['name'],
'group': voter['group'],
'number_of_votes': voter['number_of_votes']
})
app.run(port=7860, debug=True)
|