mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2025-11-30 13:20:40 +03:00
code format
This commit is contained in:
@@ -12,7 +12,9 @@ router = APIRouter(prefix="/v1/nonsense")
|
||||
|
||||
|
||||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=NonsenseResponse)
|
||||
async def create_nonsense(payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)):
|
||||
async def create_nonsense(
|
||||
payload: NonsenseSchema, db_session: AsyncSession = Depends(get_db)
|
||||
):
|
||||
nonsense = Nonsense(**payload.model_dump())
|
||||
await nonsense.save(db_session)
|
||||
return nonsense
|
||||
@@ -103,7 +105,9 @@ async def import_nonsense(
|
||||
# If an error occurs, roll back the session
|
||||
await db_session.rollback()
|
||||
# Raise an HTTP exception with a 422 status code
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
finally:
|
||||
# Ensure that the database session is closed, regardless of whether an error occurred or not
|
||||
await db_session.close()
|
||||
@@ -147,4 +151,4 @@ async def import_nonsense(
|
||||
#
|
||||
|
||||
# TODO: https://medium.com/@amitosh/full-text-search-fts-with-postgresql-and-sqlalchemy-edc436330a0c
|
||||
# TODO: https://www.postgresql.org/docs/13/textsearch-intro.html
|
||||
# TODO: https://www.postgresql.org/docs/13/textsearch-intro.html
|
||||
|
||||
@@ -13,21 +13,29 @@ router = APIRouter(prefix="/v1/stuff")
|
||||
|
||||
|
||||
@router.post("/add_many", status_code=status.HTTP_201_CREATED)
|
||||
async def create_multi_stuff(payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)):
|
||||
async def create_multi_stuff(
|
||||
payload: list[StuffSchema], db_session: AsyncSession = Depends(get_db)
|
||||
):
|
||||
try:
|
||||
stuff_instances = [Stuff(**stuff.model_dump()) for stuff in payload]
|
||||
db_session.add_all(stuff_instances)
|
||||
await db_session.commit()
|
||||
except SQLAlchemyError as ex:
|
||||
# logger.exception(ex)
|
||||
raise HTTPException(status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)) from ex
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
|
||||
) from ex
|
||||
else:
|
||||
logger.info(f"{len(stuff_instances)} instances of Stuff inserted into database.")
|
||||
logger.info(
|
||||
f"{len(stuff_instances)} instances of Stuff inserted into database."
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
@router.post("", status_code=status.HTTP_201_CREATED, response_model=StuffResponse)
|
||||
async def create_stuff(payload: StuffSchema, db_session: AsyncSession = Depends(get_db)):
|
||||
async def create_stuff(
|
||||
payload: StuffSchema, db_session: AsyncSession = Depends(get_db)
|
||||
):
|
||||
stuff = Stuff(**payload.model_dump())
|
||||
await stuff.save(db_session)
|
||||
return stuff
|
||||
|
||||
@@ -13,7 +13,9 @@ router = APIRouter(prefix="/v1/user")
|
||||
|
||||
|
||||
@router.post("/", status_code=status.HTTP_201_CREATED, response_model=UserResponse)
|
||||
async def create_user(payload: UserSchema, request: Request, db_session: AsyncSession = Depends(get_db)):
|
||||
async def create_user(
|
||||
payload: UserSchema, request: Request, db_session: AsyncSession = Depends(get_db)
|
||||
):
|
||||
logger.info(f"Creating user: {payload}")
|
||||
_user: User = User(**payload.model_dump())
|
||||
await _user.save(db_session)
|
||||
@@ -23,15 +25,23 @@ async def create_user(payload: UserSchema, request: Request, db_session: AsyncSe
|
||||
return _user
|
||||
|
||||
|
||||
@router.post("/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse)
|
||||
async def get_token_for_user(user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db)):
|
||||
@router.post(
|
||||
"/token", status_code=status.HTTP_201_CREATED, response_model=TokenResponse
|
||||
)
|
||||
async def get_token_for_user(
|
||||
user: UserLogin, request: Request, db_session: AsyncSession = Depends(get_db)
|
||||
):
|
||||
_user: User = await User.find(db_session, [User.email == user.email])
|
||||
|
||||
# TODO: out exception handling to external module
|
||||
if not _user:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="User not found")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
|
||||
)
|
||||
if not _user.check_password(user.password):
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Password is incorrect")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED, detail="Password is incorrect"
|
||||
)
|
||||
|
||||
# TODO: add refresh token
|
||||
_token = await create_access_token(_user, request)
|
||||
|
||||
Reference in New Issue
Block a user