77from sqlalchemy .ext .asyncio import AsyncSession
88from carrot .app .auction .models import Auction , AuctionStatus , Bid
99from carrot .app .auction .schemas import AuctionCreate
10- from carrot .app .auction .repositories import AuctionRepository , BidRepository
10+ from carrot .app .auction .repositories import AuctionRepository
1111from carrot .db .connection import get_db_session
1212
1313from carrot .app .auction .exceptions import (
2020from carrot .app .product .schemas import ProductPostRequest
2121
2222class AuctionService :
23- def __init__ (self , auction_repository : Annotated [AuctionRepository , Depends ()]) -> None :
24- self .repository = auction_repository
23+ def __init__ (
24+ self ,
25+ repository : AuctionRepository ,
26+ db_session : AsyncSession
27+ ) -> None :
28+ self .repository = repository
29+ self .db_session = db_session
30+
31+ @classmethod
32+ def create (cls , db_session : Annotated [AsyncSession , Depends (get_db_session )]) -> "AuctionService" :
33+ """세션을 공유하는 서비스 인스턴스 생성"""
34+ return cls (
35+ repository = AuctionRepository (db_session ),
36+ db_session = db_session
37+ )
2538
2639 async def create_auction_with_product (self , owner_id : str , region_id : str , product_data : ProductPostRequest , auction_data : AuctionCreate ) -> Auction :
2740 product = Product (
@@ -52,46 +65,28 @@ async def list_auctions(self, category_id: str | None = None, region_id: str | N
5265 async def get_auction_details (self , auction_id : str ) -> Auction :
5366 return await self .repository .get_auction_by_id (auction_id )
5467
55- class BidService :
56- def __init__ (
57- self ,
58- bid_repository : Annotated [BidRepository , Depends ()],
59- auction_repository : Annotated [AuctionRepository , Depends ()],
60- db_session : Annotated [AsyncSession , Depends (get_db_session )]
61- ) -> None :
62-
63- self .bid_repository = bid_repository
64- self .auction_repository = auction_repository
65- self .db_session = db_session
66-
67-
6868 async def place_bid (self , auction_id : str , bidder_id : str , bid_price : int ) -> Bid :
69- stmt = select (Auction ).where (Auction .id == auction_id ).with_for_update ()
70- result = await self .db_session .execute (stmt )
71- auction = result .scalar_one_or_none ()
72-
73- if not auction :
74- raise AuctionNotFoundError
69+ auction = await self .repository .get_auction_by_id (auction_id )
7570
7671 if auction .status != AuctionStatus .ACTIVE :
77- raise NotAllowedActionError
72+ raise NotAllowedActionError ()
7873 if auction .end_at <= datetime .now ():
79- raise NotAllowedActionError
74+ raise NotAllowedActionError ()
8075 if bid_price <= auction .current_price :
81- raise NotAllowedActionError
76+ raise NotAllowedActionError ()
8277
8378 new_bid = Bid (
8479 auction_id = auction_id ,
8580 bidder_id = bidder_id ,
8681 bid_price = bid_price
8782 )
88- await self .bid_repository .add_bid_without_commit (new_bid )
83+ await self .repository .add_bid_without_commit (new_bid )
8984
9085 auction .current_price = bid_price
9186 auction .bid_count += 1
92- await self .auction_repository .update_auction_without_commit (auction )
87+ await self .repository .update_auction_without_commit (auction )
9388
9489 await self .db_session .commit ()
9590 await self .db_session .refresh (new_bid )
96-
91+ await self . db_session . refresh ( auction )
9792 return new_bid
0 commit comments