@@ -42,20 +42,43 @@ async def create_stuff(
42
42
43
43
44
44
@router .get ("/{name}" , response_model = StuffResponse )
45
- async def find_stuff (
45
+ async def find_stuff (name : str , db_session : AsyncSession = Depends (get_db )):
46
+ result = await Stuff .find (db_session , name )
47
+ if not result :
48
+ raise HTTPException (status_code = status .HTTP_404_NOT_FOUND , detail = f"Stuff with name { name } not found." )
49
+ return result
50
+
51
+
52
+ @router .get ("/pool/{name}" , response_model = StuffResponse )
53
+ async def find_stuff_pool (
46
54
request : Request ,
47
55
name : str ,
48
- pool : bool = False ,
49
56
db_session : AsyncSession = Depends (get_db ),
50
57
):
58
+ """
59
+ Asynchronous function to find a specific 'Stuff' object in the database using a connection pool.
60
+
61
+ This function compiles an SQL statement to find a 'Stuff' object by its name, executes the statement
62
+ using a connection from the application's connection pool, and returns the result as a dictionary.
63
+ If the 'Stuff' object is not found, it raises an HTTPException with a 404 status code.
64
+ If an SQLAlchemyError occurs during the execution of the SQL statement, it raises an HTTPException
65
+ with a 422 status code.
66
+
67
+ Args:
68
+ request (Request): The incoming request. Used to access the application's connection pool.
69
+ name (str): The name of the 'Stuff' object to find.
70
+ db_session (AsyncSession): The database session. Used to compile the SQL statement.
71
+
72
+ Returns:
73
+ dict: The found 'Stuff' object as a dictionary.
74
+
75
+ Raises:
76
+ HTTPException: If the 'Stuff' object is not found or an SQLAlchemyError occurs.
77
+ """
51
78
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 )
79
+ stmt = await Stuff .find (db_session , name , compile_sql = True )
80
+ result = await request .app .postgres_pool .fetchrow (str (stmt ))
81
+ result = dict (result )
59
82
except SQLAlchemyError as ex :
60
83
raise HTTPException (
61
84
status_code = status .HTTP_422_UNPROCESSABLE_ENTITY , detail = repr (ex )
@@ -68,6 +91,7 @@ async def find_stuff(
68
91
return result
69
92
70
93
94
+
71
95
@router .delete ("/{name}" )
72
96
async def delete_stuff (name : str , db_session : AsyncSession = Depends (get_db )):
73
97
stuff = await Stuff .find (db_session , name )
0 commit comments