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