-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_tortoise.py
80 lines (56 loc) · 1.96 KB
/
app_tortoise.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from typing import List, Tuple
from fastapi import Depends, FastAPI, Query, status
from tortoise.contrib.fastapi import register_tortoise
from app_tortoise_models import (
PostDB,
PostCreate,
PostPartialUpdate,
PostTortoise,
)
app = FastAPI()
async def pagination(
skip: int = Query(0, ge=0),
limit: int = Query(10, ge=0),
) -> Tuple[int, int]:
capped_limit = min(100, limit)
return (skip, capped_limit)
async def get_post_or_404(id: int) -> PostTortoise:
return await PostTortoise.get(id=id)
@app.get("/posts")
async def list_posts(pagination: Tuple[int, int] = Depends(pagination)) -> List[PostDB]:
skip, limit = pagination
posts = await PostTortoise.all().offset(skip).limit(limit)
results = [PostDB.from_orm(post) for post in posts]
return results
@app.get("/posts/{id}", response_model=PostDB)
async def get_post(post: PostTortoise = Depends(get_post_or_404)) -> PostDB:
return PostDB.from_orm(post)
@app.post("/posts", response_model=PostDB, status_code=status.HTTP_201_CREATED)
async def create_post(post: PostCreate) -> PostDB:
post_tortoise = await PostTortoise.create(**post.dict())
return PostDB.from_orm(post_tortoise)
@app.patch("/posts/{id}", response_model=PostDB)
async def update_post(
post_update: PostPartialUpdate, post: PostTortoise = Depends(get_post_or_404)
) -> PostDB:
post.update_from_dict(post_update.dict(exclude_unset=True))
await post.save()
return PostDB.from_orm(post)
@app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_post(post: PostTortoise = Depends(get_post_or_404)):
await post.delete()
TORTOISE_ORM = {
"connections": {"default": "sqlite://app_tortoise.db"},
"apps": {
"models": {
"models": ["app_tortoise_models"],
"default_connection": "default",
},
},
}
register_tortoise(
app,
config=TORTOISE_ORM,
generate_schemas=True,
add_exception_handlers=True,
)