forked from Taker-Academy/KeDuBak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
358 lines (336 loc) · 12.8 KB
/
api.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import pymongo
from fastapi import FastAPI
from flask import Flask, request, jsonify
from flask_cors import CORS
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
from bson.objectid import ObjectId
import datetime
import jwt
from bson.json_util import dumps
uri = "mongodb+srv://milokowalska:[email protected]/?retryWrites=true&w=majority&appName=KeDuBak"
app = Flask(__name__)
CORS(app, origins="http://localhost:3000")
client = MongoClient(uri, server_api=ServerApi('1'))
db = client["Taker"]
user_db = db["User"]
post_db = db["Post"]
app.config['SECRET_KEY'] = 'milokow'
@app.route('/', methods=['GET'])
def get_items():
items = list(user_db.find({}, {'_id': 0}))
return jsonify(items)
@app.route('/:<item_id>', methods=['GET'])
def get_item(item_id):
item = user_db.find_one({'id': item_id}, {'_id': 0})
if item:
return jsonify(item)
else:
return jsonify({'message': 'Item not found'}), 404
@app.route('/auth/register', methods=['POST'])
def add_item():
data = request.json
if not data:
return jsonify({'ok': False, 'message': 'Mauvaise requête, paramètres manquants ou invalides.'}), 400
data['createdAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data['lastUpVote'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
existing_user = user_db.find_one({'email': data.get('email')})
if existing_user:
return jsonify({'ok': False, 'message': 'Cet utilisateur existe déjà'}), 400
new_user = {
'createdAt': data.get('createdAt'),
'email': data.get('email'),
'lastUpVote': data.get('lastUpVote'),
'firstName': data.get('firstName'),
'lastName': data.get('lastName'),
'password': data.get('password')
}
insert_user = user_db.insert_one(new_user)
user_id = str(insert_user.inserted_id)
token_payload = {
'id': user_id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24)
}
token = jwt.encode(token_payload, app.config['SECRET_KEY'], algorithm='HS256')
response_data = {
'ok': True,
'data': {
'token': 'Bearer ' + token,
'user': {
'createdAt': new_user['createdAt'],
'email': new_user['email'],
'lastUpVote': new_user['lastUpVote'],
'firstName': new_user['firstName'],
'lastName': new_user['lastName']
}
}
}
return jsonify(response_data), 201
@app.route('/auth/login', methods=['POST'])
def login():
data = request.json
if not data:
return jsonify({'ok': False, 'message': 'Mauvaise requête, paramètres manquants ou invalides.'}), 400
resultats = user_db.find(data)
if resultats:
user = {
'id': resultats[0]["_id"],
'email': resultats[0]['email'],
'firstName': resultats[0]['firstName'],
'lastName': resultats[0]['lastName']
}
user_id = str(user['id'])
token_payload = {
'id': user_id,
'exp': datetime.datetime.utcnow() + datetime.timedelta(hours=24)
}
token = jwt.encode(token_payload, app.config['SECRET_KEY'], algorithm='HS256')
response_data = {
'ok': True,
'data': {
'token': 'Bearer ' + token,
'user': {
'email': user['email'],
'firstName': user['firstName'],
'lastName': user['lastName']
}
}
}
return jsonify(response_data), 200
else:
return jsonify({'error': 'L\'utilisateur n\'existe pas'}), 401
def decode_token(token):
try:
parts = token.split()
token = parts[2]
payload = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])
return payload
except jwt.InvalidTokenError:
return None
@app.route('/user/me', methods=['GET'])
def find_user():
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
object_id = ObjectId(user_info['id'])
user_document = user_db.find_one({'_id': object_id})
if user_document:
response_data = {
'ok': True,
'data': {
'user': {
'email': user_document['email'],
'firstName': user_document['firstName'],
'lastName': user_document['lastName']
}
}
}
return jsonify(response_data), 200
else :
return jsonify({'ok': False, 'message': 'Utilisateur n\'existe pas'}), 401
@app.route('/user/edit', methods=['PUT'])
def edit_profile():
data = request.json
if not data:
return jsonify({'ok': False, 'message': 'Mauvaise requête, paramètres manquants ou invalides.'}), 400
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
object_id = ObjectId(user_info['id'])
user_document = user_db.find_one({'_id': object_id})
if user_document:
update_data = {}
update_data['firstName'] = data['firstName']
update_data['lastName'] = data['lastName']
update_data['email'] = data['email']
update_data['password'] = data['password']
result = user_db.update_one({'_id': object_id}, {'$set': update_data})
response_data = {
'ok': True,
'data': {
'user': {
'email': update_data['email'],
'firstName': update_data['firstName'],
'lastName': update_data['lastName']
}
}
}
return jsonify(response_data), 200
else :
return jsonify({'ok': False, 'message': 'Utilisateur n\'existe pas'}), 401
@app.route('/user/remove', methods=['DELETE'])
def delete_profile():
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
object_id = ObjectId(user_info['id'])
user_document = user_db.find_one({'_id': object_id})
if user_document:
response_data = {
'ok': True,
'data': {
'user': {
'email': user_document['email'],
'firstName': user_document['firstName'],
'lastName': user_document['lastName'],
'removed': True
}
}
}
result = user_db.delete_one({'_id': object_id})
return jsonify(response_data), 200
##############################################################################POST#############################################################################
@app.route('/post', methods=['GET'])
def get_posts():
items = post_db.find({}, {'_id': 0})
list_items = list(items)
response_data = {
'ok': True,
'data': list_items
}
return jsonify(response_data), 201
@app.route('/post', methods=['POST'])
def add_post():
data = request.json
if not data:
return jsonify({'ok': False, 'message': 'Mauvaise requête, paramètres manquants ou invalides.'}), 400
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
object_id = ObjectId(user_info['id'])
data['createdAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
data['lastUpVote'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
user_document = user_db.find_one({'_id': object_id})
new_post = {
'createdAt': data.get('createdAt'),
'userId': str(object_id),
'firstName': user_document['firstName'],
'title': data.get('title'),
'content': data.get('content'),
'comments': [],
'upVotes': []
}
insert_post = post_db.insert_one(new_post)
new_id = insert_post.inserted_id
str_new_id = str(new_id)
response_data = {
'ok': True,
'data': {
'_id': str_new_id,
'createdAt': new_post['createdAt'],
'userId': new_post['userId'],
'firstName': new_post['firstName'],
'title': new_post['title'],
'content': new_post['content'],
'comments': [],
'upVotes': []
}
}
return jsonify(response_data), 200
@app.route('/post/me', methods=['GET'])
def get_post_user():
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
user_id = user_info['id']
user_post = post_db.find({'userId': user_id})
if user_post :
list_post = list(user_post)
response_data = {
'ok': True,
'data': list_post
}
return dumps(response_data), 200
else:
return jsonify({'ok': False, 'message': 'Post introuvable'}), 404
@app.route('/post/<id>', methods=['GET'])
def get_post_id(id):
data = id
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
user_id = user_info['id']
post_id = ObjectId(data)
user_post = post_db.find_one({'_id': post_id})
if user_post:
response_data = {
'ok': True,
'data': user_post
}
return dumps(response_data), 200
else:
return jsonify({'ok': False, 'message': 'Post introuvable'}), 404
@app.route('/post/<id>', methods=['DELETE'])
def del_post_id(id):
data = id
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
user_id = user_info['id']
post_id = ObjectId(data)
user_post = post_db.find_one({'_id': post_id})
if user_post:
result = post_db.delete_one({'_id': post_id})
response_data = {
'ok': True,
'data': user_post
}
return dumps(response_data), 200
@app.route('/post/vote/<id>', methods=['POST'])
def up_vote(id):
data = id
if 'Authorization' not in request.headers:
return jsonify({'error': 'Authorization manquant'}), 401
token = request.headers['Authorization']
if not token:
return jsonify({'ok': False, 'message': 'Token JWT manquant'}), 401
user_info = decode_token(token)
if not user_info:
return jsonify({'ok': False, 'message': 'Token JWT invalide ou expiré'}), 401
user_id = user_info['id']
post_id = ObjectId(data)
user_post = post_db.find_one({'_id': post_id})
if user_post:
if user_id not in user_post['upVotes']:
user_post['upVotes'].append(user_id)
post_db.update_one({'_id': ObjectId(user_id)}, {'$set': {'upVotes': user_post['upVotes']}})
return jsonify({'ok': True, 'message': 'post upvoted'}), 200
else :
return jsonify({'ok': False, 'message': 'Conflict: Vous avez déjà voté pour ce post'}), 409
if __name__ == '__main__':
app.run(port=8080, debug=True)