-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.py
More file actions
37 lines (31 loc) · 1.09 KB
/
Copy pathtodo.py
File metadata and controls
37 lines (31 loc) · 1.09 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
from fastapi import APIRouter, Path, HTTPException
from model import Todo
todo_router = APIRouter()
todo_list = []
current_id = 1
@todo_router.post("/todo")
async def add_todo(todo: Todo) -> dict:
global current_id
todo_with_id = {**todo.dict(), "id": current_id}
todo_list.append(todo_with_id)
current_id += 1
return {
"msg": "todo added successfully",
"todo": todo_with_id
}
@todo_router.get("/todo")
async def retrieve_todos() -> list:
return todo_list
@todo_router.get("/todo/{todo_id}")
async def get_single_todo(todo_id: int = Path(..., title="The ID of the todo to retrieve")) -> dict:
for todo in todo_list:
if todo["id"] == todo_id:
return {"todo": todo}
raise HTTPException(status_code=404, detail="Todo with supplied ID doesn't exist")
@todo_router.delete("/todo/{todo_id}")
async def delete_todo(todo_id: int = Path(..., title="The ID of the todo to delete")) -> dict:
global todo_list
todo_list = [todo for todo in todo_list if todo["id"] != todo_id]
return {
"msg": "Todo deleted successfully"
}