Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (55 loc) · 1.67 KB
/
Copy pathmain.py
File metadata and controls
74 lines (55 loc) · 1.67 KB
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Text, Optional
from datetime import datetime
from uuid import uuid4 as uuid
import json
app = FastAPI()
# Post Model
class Post(BaseModel):
id: Optional[int or str]
name: str
username: str
email: str
phone: str
created_at: datetime = datetime.now()
published_at: Optional[datetime]
published: bool = False
with open('mockInfo.json') as mockInfo:
data = json.load(mockInfo)
@app.get('/')
def read_root():
return {'Welcome': 'que pasa ps'}
@app.get('/posts')
def get_posts():
print(len(data))
return data
@app.post('/posts')
def save_user(post: Post):
post.id = uuid()
data.append(post.dict())
return data[-1]
@app.get('/posts/{post_id}')
def get_post(post_id):
print(post_id)
for post in data:
if str(post['id']) == post_id:
return post
raise HTTPException(status_code=404, detail="Nada se encuentra")
@app.delete('/posts/{post_id}')
def delete_post(post_id):
for index, post in enumerate(data):
print(post, '<------🟠')
if str(post['id']) == post_id:
data.pop(index)
return {'message': 'Se pudo borrar'}
raise HTTPException(status_code=404, detail="Nada se encuentra")
@app.post('/posts/{post_id}')
def update_post(post_id, update_post: Post):
for index, post in enumerate(data):
if str(post['id']) == post_id:
update_post.id = uuid()
data.insert(index, update_post.dict())
data.pop(index+1)
return {'message': 'Se actualizar'}
raise HTTPException(status_code=404, detail="Nada se encuentra")