1- from typing import Annotated
1+ from typing import Annotated , Any
22from uuid import UUID
33
44from fastapi import APIRouter , Depends , Header , Query , Request , status
55
66from app .core .config import settings
77from app .core .database import SessionDep
8+ from app .core .s3 import get_s3_client_gen
89from app .core .security import RoleChecker , UserRole
910from app .services .inventory .models import ProductStatus
1011from app .services .inventory .schemas import (
1516 ReservationResponse ,
1617)
1718from app .services .inventory .service import InventoryAdminService , InventoryService
19+ from app .services .media .service import generate_presigned_get_url
1820from app .services .user .models import User
1921from app .shared .decorators import idempotent
2022from app .shared .deps import get_current_user
5052)
5153
5254
55+ async def _enrich_product_images (product : Any , s3_client : Any ) -> ProductRead :
56+ """Helper to generate presigned URLs for product images."""
57+ read_obj = ProductRead .model_validate (product )
58+ image_urls = []
59+ if hasattr (product , 'images' ):
60+ for img in product .images :
61+ if img .status == 'active' :
62+ url = await generate_presigned_get_url (s3_client , img .file_path )
63+ image_urls .append (url )
64+ read_obj .image_urls = image_urls
65+ return read_obj
66+
67+
5368@router_v1 .get ('/' , response_model = list [ProductRead ])
5469async def get_active_products (
5570 session : SessionDep ,
5671 service : Annotated [InventoryService , Depends (get_inventory_service )],
72+ s3_client : Any = Depends (get_s3_client_gen ),
5773 skip : int = 0 ,
5874 limit : int = 50 ,
5975) -> list [ProductRead ]:
@@ -63,7 +79,7 @@ async def get_active_products(
6379 limit = limit ,
6480 session = session ,
6581 )
66- return [ProductRead . model_validate ( p ) for p in products ]
82+ return [await _enrich_product_images ( p , s3_client ) for p in products ]
6783
6884
6985@router_v1 .post ('/' , response_model = ProductRead , status_code = status .HTTP_201_CREATED )
@@ -134,12 +150,13 @@ async def get_product(
134150 product_id : UUID ,
135151 session : SessionDep ,
136152 service : Annotated [InventoryService , Depends (get_inventory_service )],
153+ s3_client : Any = Depends (get_s3_client_gen ),
137154) -> ProductRead :
138155 product = await service .get_product (
139156 session = session ,
140157 product_id = product_id ,
141158 )
142- return ProductRead . model_validate (product )
159+ return await _enrich_product_images (product , s3_client )
143160
144161
145162@router_v1 .post ('/reserve' , response_model = ReservationResponse )
0 commit comments