-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
443 lines (392 loc) · 10.4 KB
/
main.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# Python
from typing import List
from datetime import date
# Pydantic
from pydantic import EmailStr
# FastApi
from fastapi import FastAPI
from fastapi import HTTPException
from fastapi import Depends
from fastapi import status
from fastapi import Form
from fastapi import Body
from fastapi import Path
# Sqlalchemy
from sqlalchemy.orm import Session
# Dependence
from schema import User, UserRegister, Tweet, TweetPost
from models import Tweets, Users
from database import SessionLocal, engine
app = FastAPI()
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Path operations
## Users
### Resgister a user
@app.post(
path="/signup",
response_model=User,
status_code=status.HTTP_201_CREATED,
summary="Register a User",
tags=["Users"]
)
def signup(
user: UserRegister = Body(...),
db: Session = Depends(get_db)
):
"""
Signup
This path operation register a user in the app
Parameters:
- Request body parameter
- user: UserRegister
Return a dict with the basic user information
- first_name: str
- last_name: str
- birth_date: date
"""
db_user = Users(
id=user.user_id,
first_name=user.first_name,
last_name=user.last_name,
email=user.email,
hashed_password=user.password+"notreallyhashed",
birth_date=user.birth_date
)
if db_user is None:
raise HTTPException(status_code=400, detail="Email or ID already registered")
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
### Login a user
@app.post(
path="/login",
response_model=User,
status_code=status.HTTP_200_OK,
summary="Login a User",
tags=["Users"]
)
def login(
user_email: EmailStr = Form(...),
user_password:str = Form(...),
db: Session = Depends(get_db)
):
"""
Login a user
This path operation login a user in the app
Parameters:
- Form parameter
- user_email: This is the user email from user
- Form parameter
- user_password: This is the password from user
Returns a dict with a user in the app, with the following keys:
- first_name: str
- last_name: str
- birth_date: datetime
"""
login = db.query(Users).filter(Users.email == user_email, Users.hashed_password == user_password).first()
if login is None:
raise HTTPException(status_code=404, detail="Login denied")
return login
### Show all users
@app.get( # obteniendo informacion
path="/users",
response_model=List[User],
status_code=status.HTTP_200_OK,
summary="Show all users",
tags=["Users"]
)
def show_all_users(
db: Session = Depends(get_db)
):
"""
Show all Users
This path operation shows all users in the app
Parameters:
-
Returns a list of dicts with all users in the app, with the following keys:
- first_name: str
- last_name: str
- birth_date: datetime
"""
users = db.query(Users).all()
return users
### Show a user
@app.get( # obteniendo informacion
path="/users/{user_id}",
response_model=User,
status_code=status.HTTP_200_OK,
summary="Show a User",
tags=["Users"]
)
def show_a_user(
user_id: str = Path(
...,
min_length=36,
max_length=36,
title="User ID",
description="This is the user ID. Its required",
example="3fa15f64-5717-4562-b3fc-2c963f66afa6"
),
db: Session = Depends(get_db)
):
"""
This path operation shows a user in the app
Parameters:
Path parameter
- user_id: specify id for user
Returns a dict with a user in the app, with the following keys:
- first_name: str
- last_name: str
- birth_date: datetime
"""
user = db.query(Users).filter(Users.id == user_id).first()
if user is None:
raise HTTPException(status_code=404, detail="User not found")
return user
### Delete a user
@app.delete(
path="/users/{user_id}/delete",
status_code=status.HTTP_200_OK,
summary="Delete a User",
tags=["Users"]
)
def delete_a_user(
user_id: str = Path(
...,
min_length=36,
max_length=36,
title="User ID",
description="This is the user ID. Its required",
example="3fa85f64-5717-4562-b3fc-2c963f66afa7"
),
db: Session = Depends(get_db)
):
"""
This path operation delete a user in the app
Parameters:
Path parameter
- user_id: specify id for user
Returns a dict with user id deleted in the app:
"""
db.query(Users).filter(Users.id == user_id).delete(synchronize_session="fetch")
db.commit()
return {"user id deleted":user_id}
### Update a user
@app.put(
path="/users/{user_id}/update",
response_model=User,
status_code=status.HTTP_200_OK,
summary="Update a User",
tags=["Users"]
)
def update_a_user(
user_id: str = Path(
...,
min_length=36,
max_length=36,
title="User ID",
description="This is the user ID. Its required",
example="3fa85f64-5717-4562-b3fc-2c963f68afa6"
),
hashed_password_up: str = Body(
...,
min_length=8,
max_length=64),
birth_date_up: date = Body(...),
db: Session = Depends(get_db)
):
"""
This path operation update the password and birth date in the app
Parameters:
Path parameter
- user_id: specify id for user
Body parameter
- hashed_password_up: new password
- birth_date_up: new birth date
Returns a dict with a updated user in the app, with the following keys:
- first_name: str
- last_name: str
- birth_date: datetime
"""
db.query(Users).filter(Users.id == user_id).update(
{"hashed_password":hashed_password_up,
"birth_date":birth_date_up
}, synchronize_session="fetch"
)
db.commit()
user_up = show_a_user(user_id, db)
return user_up
## Tweet
### Show all tweets
@app.get(
path="/",
response_model=List[Tweet],
status_code=status.HTTP_200_OK,
summary="Show all Tweets",
tags=["Tweets"]
)
def home(
db: Session = Depends(get_db)
):
"""
Home
This path operation shows all tweets in the app
Parameters
-
Returns a json list with all tweets in the app, with the following keys:
- content: str
- created_at: datetime
- updated_at: Optional[datetime]
- user_id: UUID
"""
tweets = db.query(Tweets).all()
return tweets
### Post a tweet
@app.post(
path="/post",
response_model=Tweet,
status_code=status.HTTP_201_CREATED,
summary="Post a tweet",
tags=["Tweets"]
)
def post(
tweet: TweetPost = Body(...),
db: Session = Depends(get_db)
):
"""
Post a tweet
This path operation post a tweet in the app
Parameters:
- Request body parameter
- tweet: Tweet
Return a json with the basic tweet information
- content: str
- created_at: datetime
- updated_at: Optional[datetime]
- user_id: UUID
"""
db_tweet = Tweets(
id=tweet.tweet_id,
content=tweet.content,
time_created=tweet.created_at,
time_updated=tweet.updated_at,
user_id=tweet.user_id
)
if db_tweet is None:
raise HTTPException(status_code=400, detail="User not exist")
db.add(db_tweet)
db.commit()
db.refresh(db_tweet)
return db_tweet
### Show a tweet
@app.get(
path="/tweets/{tweet_id}",
response_model=Tweet,
status_code=status.HTTP_200_OK,
summary="Show a tweet",
tags=["Tweets"]
)
def show_a_tweet(
tweet_id: str = Path(
...,
min_length=36,
max_length=36,
title="Tweet ID",
description="This is the user ID. Its required",
example="1fa25f64-5717-4562-b3fc-2c963f66afa5"
),
db: Session = Depends(get_db)
):
"""
This path operation shows a tweet in the app
Parameters:
Path parameter
- tweet_id: tweet id
Return a json with the basic tweet information
- content: str
- created_at: datetime
- updated_at: Optional[datetime]
- user_id: UUID
"""
tweet = db.query(Tweets).filter(Tweets.id == tweet_id).first()
if tweet is None:
raise HTTPException(status_code=404, detail="Tweet not found")
return tweet
### Delete a tweet
@app.delete(
path="/tweets/{tweet_id}/delete",
status_code=status.HTTP_200_OK,
summary="Delete a tweet",
tags=["Tweets"]
)
def delete_a_tweet(
tweet_id: str = Path(
...,
min_length=36,
max_length=36,
title="Tweet ID",
description="This is the user ID. Its required",
example="3fa85f64-5717-4562-b3fc-2c963f66afa6"
),
db: Session = Depends(get_db)
):
"""
This path operation delete a tweet in the app
Parameters:
Path parameter
- tweet_id: specify id for user
Returns a dict with tweet id deleted:
"""
db.query(Tweets).filter(Tweets.id == tweet_id).delete(synchronize_session="fetch")
db.commit()
return {"tweet id deleted":tweet_id}
### Update a tweet
@app.put( # put para actualizar
path="/tweets/{tweet_id}/update",
response_model=Tweet,
status_code=status.HTTP_200_OK,
summary="Update a tweet",
tags=["Tweets"]
)
def update_a_tweet(
tweet_id: str = Path(
...,
min_length=36,
max_length=36,
title="Tweet ID",
description="This is the user ID. Its required",
example="3fa85f64-5717-4562-b3fc-2c963f66afa6"
),
db: Session = Depends(get_db),
content_up: str = Body(
...,
max_length=256,
min_length=1),
):
"""
This path operation update the content in a tweet
Parameters:
Path parameter
- tweet_id: specify id for user
Body parameter
- content_up: new content for the tweet
Returns a dict with a updated tweet in the app, with the following keys:
- content: str
- created_at: datetime
- updated_at: Optional[datetime]
- user_id: UUID
"""
db.query(Tweets).filter(Tweets.id == tweet_id).update(
{"content":content_up
}, synchronize_session="fetch"
)
db.commit()
tweet_up = show_a_tweet(tweet_id, db)
return tweet_up