-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
65 lines (42 loc) · 1.19 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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Optional, List
class Todo(BaseModel):
name: str
due_date: str
description: str
app = FastAPI(title="Todo API")
# Create, Read, Update, Delete
store_todo = []
@app.get('/')
async def home():
return {"Hello": "World"}
@app.post('/todo/')
async def create_todo(todo: Todo):
store_todo.append(todo)
return todo
@app.get('/todo/', response_model=List[Todo])
async def get_all_todos():
return store_todo
@app.get('/todo/{id}')
async def get_todo(id: int):
print(store_todo)
try:
return store_todo[id]
except:
raise HTTPException(status_code=404, detail="Todo Not Found")
@app.put('/todo/{id}')
async def update_todo(id: int, todo: Todo):
try:
store_todo[id] = todo
return store_todo[id]
except:
raise HTTPException(status_code=404, detail="Todo Not Found")
@app.delete('/todo/{id}')
async def delete_todo(id: int):
try:
obj = store_todo[id]
store_todo.pop(id)
return obj
except:
raise HTTPException(status_code=404, detail="Todo Not Found")