Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
use db.flush() for the crud factory and commit the changes after call…
Browse files Browse the repository at this point in the history
…ing a factory method
  • Loading branch information
execreate committed Nov 8, 2022
1 parent b3c63e0 commit 714b5c9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
20 changes: 13 additions & 7 deletions app/api/v1/blog_post.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from uuid import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from api.dependencies.database import get_db
from fastapi import APIRouter, Depends, Response, status
from schemas import blog_post as blog_post_schemas
Expand All @@ -14,16 +15,18 @@

@router.post("", status_code=201, response_model=blog_post_schemas.BlogPostSchema)
async def create_a_blog_post(
blog_post: blog_post_schemas.InBlogPostSchema, db=Depends(get_db)
blog_post: blog_post_schemas.InBlogPostSchema, db: AsyncSession = Depends(get_db)
):
crud = BlogPostCrud(db)
return await crud.create(blog_post)
result = await crud.create(blog_post)
await db.commit()
return result


@router.get("", response_model=blog_post_schemas.PaginatedBlogPostSchema)
async def list_blog_posts(
pagination: LimitOffsetPaginationParams = Depends(),
db=Depends(get_db),
db: AsyncSession = Depends(get_db),
):
crud = BlogPostCrud(db)
return await crud.get_paginated_list(pagination.limit, pagination.offset)
Expand All @@ -40,7 +43,7 @@ async def list_blog_posts(
)
async def retrieve_a_blog_post(
post_id: UUID,
db=Depends(get_db),
db: AsyncSession = Depends(get_db),
):
crud = BlogPostCrud(db)
return await crud.get_by_id(post_id)
Expand All @@ -58,10 +61,12 @@ async def retrieve_a_blog_post(
async def update_a_blog_post(
post_id: UUID,
blog_post: blog_post_schemas.UpdateBlogPostSchema,
db=Depends(get_db),
db: AsyncSession = Depends(get_db),
):
crud = BlogPostCrud(db)
return await crud.update_by_id(post_id, blog_post)
result = await crud.update_by_id(post_id, blog_post)
await db.commit()
return result


@router.delete(
Expand All @@ -75,8 +80,9 @@ async def update_a_blog_post(
)
async def delete_a_blog_post(
post_id: UUID,
db=Depends(get_db),
db: AsyncSession = Depends(get_db),
):
crud = BlogPostCrud(db)
await crud.delete_by_id(post_id)
await db.commit()
return Response(status_code=status.HTTP_204_NO_CONTENT)
5 changes: 3 additions & 2 deletions app/db/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _paginated_schema(self) -> Type[PAGINATED_SCHEMA]:
async def create(self, in_schema: IN_SCHEMA) -> SCHEMA:
entry = self._table(id=uuid4(), **in_schema.dict())
self._db_session.add(entry)
await self._db_session.commit()
await self._db_session.flush()
return self._schema.from_orm(entry)

async def get_by_id(self, entry_id: UUID) -> SCHEMA:
Expand All @@ -66,14 +66,15 @@ async def update_by_id(
in_data_dict: dict = in_data.dict(exclude_unset=True)
for _k, _v in in_data_dict.items():
setattr(entry, _k, _v)
await self._db_session.commit()
await self._db_session.flush()
return self._schema.from_orm(entry)

async def delete_by_id(self, entry_id: UUID) -> None:
entry = await self._db_session.get(self._table, entry_id)
if not entry:
raise HTTPException(status_code=404, detail="Object not found")
await self._db_session.delete(entry)
await self._db_session.flush()
return

async def get_paginated_list(self, limit: int, offset: int) -> PAGINATED_SCHEMA:
Expand Down

0 comments on commit 714b5c9

Please sign in to comment.