-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (48 loc) · 1.47 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
from typing import Union
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
app = FastAPI()
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class Todo(BaseModel):
text: str
is_complete: bool
TODOS = []
TODOS.append(Todo(text="Learn about React", is_complete=False))
TODOS.append(Todo(text="Meet friend for lunch", is_complete=False))
TODOS.append(Todo(text="Build really cool todo app", is_complete=False))
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/todos")
def list_todo():
return TODOS
@app.post("/todos")
def new_todo(todo: Todo):
new_todo = Todo(text=todo.text, is_complete=False)
TODOS.append(new_todo)
return new_todo
@app.get("/todos/{index}")
def read_todo(index: int):
if index > (len(TODOS) - 1):
raise HTTPException(status_code=404, detail="TODO doesnt exist")
todo = TODOS[index]
return todo
@app.put("/todos/{index}")
def update_todo(index: int, todo: Todo):
if index > (len(TODOS) - 1):
raise HTTPException(status_code=404, detail="TODO doesnt exist")
TODOS[index] = todo
return TODOS[index]
@app.delete("/todos/{index}")
def delete_todo(index: int):
if index > (len(TODOS) - 1):
raise HTTPException(status_code=404, detail="TODO doesnt exist")
del TODOS[index]