1- from fastapi import APIRouter , Depends , HTTPException , status
1+ from fastapi import APIRouter , Depends , HTTPException , status , Request
2+ from fastapi .exceptions import ResponseValidationError
23from sqlalchemy .exc import SQLAlchemyError
34from sqlalchemy .ext .asyncio import AsyncSession
45
@@ -21,7 +22,6 @@ async def create_multi_stuff(
2122 db_session .add_all (stuff_instances )
2223 await db_session .commit ()
2324 except SQLAlchemyError as ex :
24- # logger.exception(ex)
2525 raise HTTPException (
2626 status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = repr (ex )
2727 ) from ex
@@ -43,10 +43,29 @@ async def create_stuff(
4343
4444@router .get ("/{name}" , response_model = StuffResponse )
4545async def find_stuff (
46+ request : Request ,
4647 name : str ,
48+ pool : bool = False ,
4749 db_session : AsyncSession = Depends (get_db ),
4850):
49- return await Stuff .find (db_session , name )
51+ try :
52+ if not pool :
53+ result = await Stuff .find (db_session , name )
54+ else :
55+ # execute the compiled SQL statement
56+ stmt = await Stuff .find (db_session , name , compile_sql = True )
57+ result = await request .app .postgres_pool .fetchrow (str (stmt ))
58+ result = dict (result )
59+ except SQLAlchemyError as ex :
60+ raise HTTPException (
61+ status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = repr (ex )
62+ ) from ex
63+ if not result :
64+ raise HTTPException (
65+ status_code = status .HTTP_404_NOT_FOUND ,
66+ detail = f"Stuff with name { name } not found." ,
67+ )
68+ return result
5069
5170
5271@router .delete ("/{name}" )
0 commit comments