Skip to content

Commit e003454

Browse files
committed
- split endpoint to find stuff for pool and traditional
1 parent 514eea7 commit e003454

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

app/api/stuff.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,43 @@ async def create_stuff(
4242

4343

4444
@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(
4654
request: Request,
4755
name: str,
48-
pool: bool = False,
4956
db_session: AsyncSession = Depends(get_db),
5057
):
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+
"""
5178
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)
5982
except SQLAlchemyError as ex:
6083
raise HTTPException(
6184
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, detail=repr(ex)
@@ -68,6 +91,7 @@ async def find_stuff(
6891
return result
6992

7093

94+
7195
@router.delete("/{name}")
7296
async def delete_stuff(name: str, db_session: AsyncSession = Depends(get_db)):
7397
stuff = await Stuff.find(db_session, name)

0 commit comments

Comments
 (0)