Skip to content

Commit 2c13844

Browse files
Fix place_bid
fix place_bid
2 parents 704d85b + f034c55 commit 2c13844

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

carrot/app/auction/repositories.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
)
1818

1919
class AuctionRepository:
20-
def __init__(self, session: Annotated[AsyncSession, Depends(get_db_session)]) -> None:
20+
def __init__(self, session: AsyncSession) -> None:
2121
self.session = session
2222

23+
2324
async def create_auction(self, product: Product, auction: Auction) -> Auction:
2425
self.session.add(product)
2526
await self.session.flush() # Ensure product ID is generated
@@ -80,18 +81,12 @@ async def delete_auction(self, auction: Auction) -> None:
8081
async def update_auction(self, auction: Auction) -> Auction:
8182
merged = await self.session.merge(auction)
8283
await self.session.commit()
83-
await self.session.refresh(merged)
8484
return merged
8585

8686
async def update_auction_without_commit(self, auction: Auction) -> Auction:
8787
merged = await self.session.merge(auction)
88-
await self.session.refresh(merged)
8988
return merged
90-
91-
class BidRepository:
92-
def __init__(self, session: Annotated[AsyncSession, Depends(get_db_session)]) -> None:
93-
self.session = session
94-
89+
9590
async def add_bid_without_commit(self, bid: Bid) -> Bid:
9691
self.session.add(bid)
9792
return bid
@@ -115,4 +110,6 @@ async def gets_user_bids(self, user_id: str) -> List[Bid]:
115110
.options(joinedload(Bid.auction).joinedload(Auction.product))
116111
)
117112
result = await self.session.execute(stmt)
118-
return result.scalars().all()
113+
return result.scalars().all()
114+
115+

carrot/app/auction/router.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from carrot.db.connection import get_db_session
66
from carrot.app.auth.utils import login_with_header
77

8-
from carrot.app.auction.services import AuctionService, BidService
8+
from carrot.app.auction.services import AuctionService
99
from carrot.app.auction.schemas import AuctionCreate, AuctionResponse, BidCreate, BidResponse # Pydantic 모델 가정
1010

1111
from carrot.app.user.models import User
@@ -20,7 +20,7 @@ async def create_new_auction(
2020
owner: Annotated[User, Depends(login_with_header)],
2121
product_data: ProductPostRequest,
2222
auction_data: AuctionCreate,
23-
service: Annotated[AuctionService, Depends()],
23+
service: Annotated[AuctionService, Depends(AuctionService.create)],
2424
) -> AuctionResponse:
2525
auction = await service.create_auction_with_product(
2626
owner_id=owner.id,
@@ -33,7 +33,7 @@ async def create_new_auction(
3333
# 2. 경매 목록 조회 (카테고리, 지역 필터링)
3434
@auction_router.get("/", response_model=List[AuctionResponse])
3535
async def get_auctions(
36-
service: Annotated[AuctionService, Depends()],
36+
service: Annotated[AuctionService, Depends(AuctionService.create)],
3737
category_id: Optional[str] = Query(None, description="카테고리 ID로 필터링"),
3838
region_id: Optional[str] = Query(None, description="지역 ID로 필터링"),
3939
) -> List[AuctionResponse]:
@@ -45,7 +45,7 @@ async def get_auctions(
4545
@auction_router.get("/{auction_id}", response_model=AuctionResponse)
4646
async def get_auction_detail(
4747
auction_id: str,
48-
service: Annotated[AuctionService, Depends()],
48+
service: Annotated[AuctionService, Depends(AuctionService.create)],
4949
) -> AuctionResponse:
5050
auction = await service.get_auction_details(auction_id)
5151
return AuctionResponse.model_validate(auction)
@@ -56,10 +56,10 @@ async def place_bid(
5656
auction_id: str,
5757
bid_data: BidCreate,
5858
bidder: Annotated[User, Depends(login_with_header)],
59-
bid_service: Annotated[BidService, Depends()],
59+
service: Annotated[AuctionService, Depends(AuctionService.create)],
6060
) -> BidResponse:
6161

62-
bid = await bid_service.place_bid(
62+
bid = await service.place_bid(
6363
auction_id=auction_id,
6464
bidder_id=bidder.id,
6565
bid_price=bid_data.bid_price

carrot/app/auction/services.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sqlalchemy.ext.asyncio import AsyncSession
88
from carrot.app.auction.models import Auction, AuctionStatus, Bid
99
from carrot.app.auction.schemas import AuctionCreate
10-
from carrot.app.auction.repositories import AuctionRepository, BidRepository
10+
from carrot.app.auction.repositories import AuctionRepository
1111
from carrot.db.connection import get_db_session
1212

1313
from carrot.app.auction.exceptions import (
@@ -20,8 +20,21 @@
2020
from carrot.app.product.schemas import ProductPostRequest
2121

2222
class 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

Comments
 (0)