generated from execreate/fastapi-supertokens-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog_post.py
88 lines (76 loc) · 2.05 KB
/
blog_post.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
81
82
83
84
85
86
87
88
from api.dependencies.database import DbSessionDep
from api.dependencies.pagination import PaginationDep
from fastapi import APIRouter, Response, status
from schemas import blog_post as blog_post_schemas
from db.crud.blog_post import BlogPostCrud
router = APIRouter(
prefix="/blog",
tags=["Blog posts"],
)
@router.post("", status_code=201, response_model=blog_post_schemas.OutBlogPostSchema)
async def create_a_blog_post(
blog_post: blog_post_schemas.InBlogPostSchema,
db: DbSessionDep,
):
crud = BlogPostCrud(db)
result = await crud.create(blog_post)
await crud.commit_session()
return result
@router.get("", response_model=blog_post_schemas.PaginatedBlogPostSchema)
async def list_blog_posts(
db: DbSessionDep,
pagination: PaginationDep,
):
crud = BlogPostCrud(db)
return await crud.get_paginated_list(pagination.limit, pagination.offset)
@router.get(
"/{post_id}",
response_model=blog_post_schemas.OutBlogPostSchema,
responses={
404: {
"description": "Object not found",
},
},
)
async def retrieve_a_blog_post(
post_id: int,
db: DbSessionDep,
):
crud = BlogPostCrud(db)
return await crud.get_by_id(post_id)
@router.patch(
"/{post_id}",
response_model=blog_post_schemas.OutBlogPostSchema,
responses={
404: {
"description": "Object not found",
},
},
)
async def update_a_blog_post(
post_id: int,
blog_post: blog_post_schemas.UpdateBlogPostSchema,
db: DbSessionDep,
):
crud = BlogPostCrud(db)
await crud.update_by_id(post_id, blog_post)
result = await crud.get_by_id(post_id)
await crud.commit_session()
return result
@router.delete(
"/{post_id}",
status_code=204,
responses={
404: {
"description": "Object not found",
},
},
)
async def delete_a_blog_post(
post_id: int,
db: DbSessionDep,
):
crud = BlogPostCrud(db)
await crud.delete_by_id(post_id)
await crud.commit_session()
return Response(status_code=status.HTTP_204_NO_CONTENT)