1- from typing import Annotated , List
1+ from typing import List
22
3- from fastapi import Depends
4- from sqlalchemy import select , and_ , text , or_
3+ from sqlalchemy import select , or_
54from sqlalchemy .ext .asyncio import AsyncSession
65
7- from carrot .app .user .models import LocalAccount , SocialAccount , User
86from carrot .app .product .models import Product
9- from carrot .db . connection import get_db_session
7+ from carrot .app . auction . models import Auction
108
119
1210class ProductRepository :
13- def __init__ (self , session : Annotated [ AsyncSession , Depends ( get_db_session )] ) -> None :
11+ def __init__ (self , session : AsyncSession ) -> None :
1412 self .session = session
1513
1614 async def create_post (self , product : Product ) -> Product :
@@ -19,24 +17,30 @@ async def create_post(self, product: Product) -> Product:
1917 await self .session .refresh (product )
2018 return product
2119
20+ async def create_auction (self , product : Product , auction : Auction ) -> Product :
21+ auction .product_id = product .id
22+ self .session .add (auction )
23+ await self .session .commit ()
24+ await self .session .refresh (auction )
25+ return product
26+
2227 async def update_post (self , product : Product ) -> Product :
2328 merged = await self .session .merge (product )
2429 await self .session .commit ()
2530 await self .session .refresh (merged )
2631 return merged
27-
32+
2833 async def get_post_by_product_id (self , product_id : str ) -> Product :
2934 query = select (Product ).where (Product .id == product_id )
3035 posts = await self .session .execute (query )
31-
3236 return posts .scalars ().one_or_none ()
33-
37+
3438 async def get_posts_by_query (self , user_id : str , keyword : str , region_id : str ) -> List [Product ]:
3539 query = select (Product )
36-
40+
3741 if user_id :
3842 query = query .where (Product .owner_id == user_id )
39-
43+
4044 if keyword :
4145 search_pattern = f"%{ keyword } %"
4246 query = query .where (
@@ -45,20 +49,18 @@ async def get_posts_by_query(self, user_id: str, keyword: str, region_id: str) -
4549 Product .content .ilike (search_pattern )
4650 )
4751 )
48-
52+
4953 if region_id :
5054 query = query .where (Product .region_id == region_id )
51-
55+
5256 posts = await self .session .execute (query )
53-
5457 return posts .scalars ().all ()
55-
58+
5659 async def get_posts_all (self ) -> List [Product ]:
5760 query = select (Product )
5861 posts = await self .session .execute (query )
59-
6062 return posts .scalars ().all ()
61-
63+
6264 async def remove_post (self , product : Product ):
6365 await self .session .delete (product )
64- await self .session .commit ()
66+ await self .session .commit ()
0 commit comments