@@ -100,58 +100,22 @@ def validate_product_image_owner(
100100) -> ProductImage :
101101 return productService .get_product_image (db , product .id , image_id )
102102
103- class ProductSortType (str , Enum ):
104- ACCURACY = "accuracy"
105- PRICE_ASC = "price_asc"
106- PRICE_DESC = "price_desc"
107- LATEST = "latest"
108- LIKES = "likes"
109-
110103@router .get ("/" , status_code = 200 )
111104def get_products (
112105 q : Optional [str ] = Query (None ),
113106 category_id : Optional [int ] = Query (None ),
114107 soldout : Optional [bool ] = Query (None ),
115108 min_price : Optional [int ] = Query (None , ge = 0 ),
116109 max_price : Optional [int ] = Query (None , ge = 0 ),
117- sort_type : ProductSortType = Query (),
110+ sort_type : ProductSortType = Query (ProductSortType . ACCURACY ),
118111 page : int = Query (0 , ge = 0 ),
119112 limit : int = Query (20 , le = 100 ),
120113 db : Session = Depends (get_db_session ),
121114 productService : ProductService = Depends ()
122115) -> list [ProductResponse ]:
123-
124- query = select (Product )
125-
126- if q :
127- query = query .where (Product .title .contains (q ))
128-
129- if category_id :
130- query = query .where (Product .category_id == category_id )
131-
132- if soldout is not None :
133- query = query .where (Product .soldout == soldout )
134-
135- if min_price is not None :
136- query = query .where (Product .price >= min_price )
137-
138- if max_price is not None :
139- query = query .where (Product .price <= max_price )
140-
141- # 🏷 정렬 기준 적용
142- if sort_type == ProductSortType .PRICE_ASC :
143- query = query .order_by (asc (Product .price ))
144- elif sort_type == ProductSortType .PRICE_DESC :
145- query = query .order_by (desc (Product .price ))
146- elif sort_type == ProductSortType .LATEST :
147- query = query .order_by (desc (Product .date ))
148- elif sort_type == ProductSortType .LIKES :
149- query = query .order_by (desc (Product .heart_count ))
150- else :
151- query = query .order_by (Product .id ) # 기본값: 등록 순
152-
153- products = db .exec (query .offset (page * limit ).limit (limit )).all ()
154-
116+ products = productService .get_products (
117+ db , q , category_id , soldout , min_price , max_price , sort_type , page , limit
118+ )
155119 result = [
156120 ProductResponse (product = product , productImages = productService .get_product_images (db , product .id ))
157121 for product in products
0 commit comments