@@ -207,7 +207,8 @@ async def get_posts(
207207 response : Response,
208208 page : int = 1 ,
209209 per_page : int = 10 ,
210- category : str = None
210+ category : str | None = None ,
211+ db : Annotated[AsyncSession, Depends(async_get_db)]
211212):
212213 """ Conditional caching based on parameters."""
213214
@@ -255,7 +256,8 @@ def generate_etag(data: Any) -> str:
255256async def get_user (
256257 request : Request,
257258 response : Response,
258- user_id : int
259+ user_id : int ,
260+ db : Annotated[AsyncSession, Depends(async_get_db)]
259261):
260262 """ Endpoint with ETag support for efficient caching."""
261263
@@ -289,7 +291,8 @@ Use Last-Modified headers for time-based cache validation:
289291async def get_post (
290292 request : Request,
291293 response : Response,
292- post_id : int
294+ post_id : int ,
295+ db : Annotated[AsyncSession, Depends(async_get_db)]
293296):
294297 """ Endpoint with Last-Modified header support."""
295298
@@ -343,13 +346,13 @@ async def serve_static(response: Response, file_path: str):
343346``` python
344347# Reference data (rarely changes)
345348@router.get (" /api/v1/countries" )
346- async def get_countries (response : Response, db : AsyncSession = Depends(async_get_db)):
349+ async def get_countries (response : Response, db : Annotated[ AsyncSession, Depends(async_get_db)] ):
347350 response.headers[" Cache-Control" ] = " public, max-age=86400" # 24 hours
348351 return await crud_countries.get_all(db = db)
349352
350353# User-generated content (moderate changes)
351354@router.get (" /api/v1/posts" )
352- async def get_posts (response : Response, db : AsyncSession = Depends(async_get_db)):
355+ async def get_posts (response : Response, db : Annotated[ AsyncSession, Depends(async_get_db)] ):
353356 response.headers[" Cache-Control" ] = " public, max-age=1800" # 30 minutes
354357 return await crud_posts.get_multi(db = db, is_deleted = False )
355358
@@ -358,7 +361,7 @@ async def get_posts(response: Response, db: AsyncSession = Depends(async_get_db)
358361async def get_notifications (
359362 response : Response,
360363 current_user : dict = Depends(get_current_user),
361- db : AsyncSession = Depends(async_get_db)
364+ db : Annotated[ AsyncSession, Depends(async_get_db)]
362365):
363366 response.headers[" Cache-Control" ] = " private, max-age=300" # 5 minutes
364367 response.headers[" Vary" ] = " Authorization"
@@ -444,12 +447,15 @@ async def update_post(
444447 response : Response,
445448 post_id : int ,
446449 post_data : PostUpdate,
447- current_user : dict = Depends(get_current_user)
450+ current_user : dict = Depends(get_current_user),
451+ db : Annotated[AsyncSession, Depends(async_get_db)]
448452):
449453 """ Update post and invalidate related caches."""
450454
451455 # Update the post
452456 updated_post = await crud_posts.update(db = db, id = post_id, object = post_data)
457+ if not updated_post:
458+ raise HTTPException(status_code = 404 , detail = " Post not found" )
453459
454460 # Set headers to indicate cache invalidation is needed
455461 response.headers[" Cache-Control" ] = " no-cache"
0 commit comments