-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
56 lines (43 loc) · 1.35 KB
/
main.py
File metadata and controls
56 lines (43 loc) · 1.35 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
from pydantic import BaseModel,Field
from typing import List,Optional
from fastapi import FastAPI, HTTPException
import uvicorn
app = FastAPI()
class AddTodoSchema(BaseModel):
text: str = Field(max_length=150)
class TodoSchema(AddTodoSchema):
id: int
todos:Optional[List[TodoSchema]] = []
@app.get('/todos',tags=['Todo management'])
def get_todos():
return todos
@app.get('/todos/{todo_id}',tags=['Todo management'])
def get_todo_by_id(todo_id:int):
for todo in todos:
if todo.id == todo_id :
return todo
raise HTTPException(status_code=404)
@app.post('/todos',tags=['Todo management'])
def add_todo(new_todo:AddTodoSchema):
todo = TodoSchema(
id=len(todos) + 1,
text=new_todo.text
)
todos.append(todo)
return todos
@app.patch('/todos/{todo_id}',tags=['Todo management'])
def update_todo(todo_id:int,updated_todo:AddTodoSchema):
for todo in todos:
if todo.id == todo_id:
todo.text = updated_todo.text
return todo
raise HTTPException(status_code=404)
@app.delete('/todos/{todo_id}',tags=['Todo management'])
def delete_todo(todo_id:int):
for todo in todos:
if todo.id == todo_id:
todos.remove(todo)
return todos
raise HTTPException(status_code=404)
if __name__ == "__main__":
uvicorn.run("main:app",reload=True)