Skip to content

Commit 646f7f9

Browse files
committed
add python code working with local data, dictionary
1 parent 82f8dfd commit 646f7f9

1 file changed

Lines changed: 74 additions & 77 deletions

File tree

main.py

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -12,92 +12,89 @@ def handler(event, context):
1212

1313
app = FastAPI()
1414

15-
@app.get("/")
16-
def read_root():
17-
return {"Hello": "World"}
18-
15+
class Priority(IntEnum):
16+
LOW = 3
17+
MEDIUM = 2
18+
HIGH = 1
1919

20-
@app.get("/items/{item_id}")
21-
def read_item(item_id: int, q: str = None):
22-
return {"item_id": item_id, "q": q}
20+
class TodoBase(BaseModel):
21+
title: str = Field(..., min_length=3, max_length=50, description="Título da tarefa")
22+
done: bool = Field(default=False, description="Tarefa concluída ou não")
23+
priority: Priority = Field(default=Priority.LOW, description="Prioridade da tarefa")
2324

24-
handler = Mangum(app, lifespan="off")
25-
# class Priority(IntEnum):
26-
# LOW = 3
27-
# MEDIUM = 2
28-
# HIGH = 1
25+
class TodoCreate(TodoBase):
26+
pass
2927

30-
# class TodoBase(BaseModel):
31-
# title: str = Field(..., min_length=3, max_length=50, description="Título da tarefa")
32-
# done: bool = Field(default=False, description="Tarefa concluída ou não")
33-
# priority: Priority = Field(default=Priority.LOW, description="Prioridade da tarefa")
28+
class Todo(TodoBase):
29+
id: int = Field(..., description="Identificador da tarefa")
3430

35-
# class TodoCreate(TodoBase):
36-
# pass
31+
class TodoUpdate(BaseModel):
32+
title: Optional[str] = Field(None, min_length=3, max_length=50, description="Título da tarefa")
33+
done: Optional[bool] = Field(None, description="Tarefa concluída ou não")
34+
priority: Optional[Priority] = Field(None, description="Prioridade da tarefa")
3735

38-
# class Todo(TodoBase):
39-
# id: int = Field(..., description="Identificador da tarefa")
4036

41-
# class TodoUpdate(BaseModel):
42-
# title: Optional[str] = Field(None, min_length=3, max_length=50, description="Título da tarefa")
43-
# done: Optional[bool] = Field(None, description="Tarefa concluída ou não")
44-
# priority: Optional[Priority] = Field(None, description="Prioridade da tarefa")
37+
all_todos = [
38+
Todo(id=1, title="Fazer compras", done=False, priority=Priority.HIGH),
39+
Todo(id=2, title="Estudar FastAPI", done=False, priority=Priority.MEDIUM),
40+
Todo(id=3, title="Estudar Python", done=False, priority=Priority.MEDIUM),
41+
Todo(id=4, title="Estudar Django", done=False, priority=Priority.HIGH),
42+
Todo(id=5, title="Estudar Flask", done=False, priority=Priority.LOW),
43+
]
4544

46-
# all_todos = [
47-
# Todo(id=1, title="Fazer compras", done=False, priority=Priority.HIGH),
48-
# Todo(id=2, title="Estudar FastAPI", done=False, priority=Priority.MEDIUM),
49-
# Todo(id=3, title="Estudar Python", done=False, priority=Priority.MEDIUM),
50-
# Todo(id=4, title="Estudar Django", done=False, priority=Priority.HIGH),
51-
# Todo(id=5, title="Estudar Flask", done=False, priority=Priority.LOW),
52-
# ]
5345

46+
@app.get("/")
47+
def read_root():
48+
return {"Olá": "Ascanianos"}
5449

55-
# @api.get("/")
56-
# def index():
57-
# return { "message": "Olá ascanianos"}
50+
@app.get('/todos',response_model=List[Todo])
51+
def get_todos():
52+
return all_todos
5853

59-
# @api.get('/todos',response_model=List[Todo])
60-
# def get_todos():
61-
# return all_todos
54+
# @app.get("/items/{item_id}")
55+
# def read_item(item_id: int, q: str = None):
56+
# return {"item_id": item_id, "q": q}
6257

63-
# @api.get('/todos/{todo_id}', response_model=Todo)
64-
# def get_todo( todo_id: int ):
65-
# for todo in all_todos:
66-
# if todo.todo_id == todo_id:
67-
# return todo
58+
@app.get('/todos/{todo_id}', response_model=Todo)
59+
def get_todo( todo_id: int ):
60+
for todo in all_todos:
61+
if todo.todo_id == todo_id:
62+
return todo
6863

69-
# @api.post('/todos', response_model=List[Todo])
70-
# def create_todo(todo: TodoCreate):
71-
72-
# new_todo_id = max( todo.id for todo in all_todos ) + 1
73-
74-
# new_todo = Todo(
75-
# id=new_todo_id,
76-
# title=todo.title,
77-
# done=todo.done,
78-
# priority=todo.priority
79-
# )
80-
81-
# all_todos.append(new_todo)
82-
# return all_todos
83-
84-
# @api.put('/todos/{id}', response_model=Todo)
85-
# def update_todo( id: int, update_todo: TodoUpdate):
86-
# for todo in all_todos:
87-
# if todo.id == id:
88-
# if update_todo.title is not None:
89-
# todo.title = update_todo.title
90-
# if update_todo.done is not None:
91-
# todo.done = update_todo.done
92-
# if update_todo.priority is not None:
93-
# todo.priority = update_todo.priority
94-
# return todo
95-
# raise HTTPException(status_code=404, detail="Todo não encontrado")
96-
97-
# @api.delete('/todos/{id}')
98-
# def delete_todo( id: int ):
99-
# for todo in all_todos:
100-
# if todo.id == id:
101-
# all_todos.remove(todo)
102-
# return all_todos
103-
# raise HTTPException(status_code=404, detail="Todo não encontrado")
64+
@app.post('/todos', response_model=List[Todo])
65+
def create_todo(todo: TodoCreate):
66+
67+
new_todo_id = max( todo.id for todo in all_todos ) + 1
68+
69+
new_todo = Todo(
70+
id=new_todo_id,
71+
title=todo.title,
72+
done=todo.done,
73+
priority=todo.priority
74+
)
75+
76+
all_todos.append(new_todo)
77+
return all_todos
78+
79+
@app.put('/todos/{id}', response_model=Todo)
80+
def update_todo( id: int, update_todo: TodoUpdate):
81+
for todo in all_todos:
82+
if todo.id == id:
83+
if update_todo.title is not None:
84+
todo.title = update_todo.title
85+
if update_todo.done is not None:
86+
todo.done = update_todo.done
87+
if update_todo.priority is not None:
88+
todo.priority = update_todo.priority
89+
return todo
90+
raise HTTPException(status_code=404, detail="Todo não encontrado")
91+
92+
@app.delete('/todos/{id}')
93+
def delete_todo( id: int ):
94+
for todo in all_todos:
95+
if todo.id == id:
96+
all_todos.remove(todo)
97+
return all_todos
98+
raise HTTPException(status_code=404, detail="Todo não encontrado")
99+
100+
handler = Mangum(app, lifespan="off")

0 commit comments

Comments
 (0)