Skip to content

Commit f4d22d6

Browse files
add top-bid router
1 parent 151029f commit f4d22d6

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

carrot/app/auction/exceptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,12 @@ def __init__(self) -> None:
4040
status_code=status.HTTP_400_BAD_REQUEST,
4141
error_code="AUC_005",
4242
error_msg="The bid price is too low.",
43+
)
44+
45+
class NoBidsFoundError(CarrotException):
46+
def __init__(self) -> None:
47+
super().__init__(
48+
status_code=status.HTTP_404_NOT_FOUND,
49+
error_code="AUC_006",
50+
error_msg="No bids found for this auction.",
4351
)

carrot/app/auction/repositories.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,15 @@ async def gets_user_bids(self, user_id: str) -> List[Bid]:
112112
)
113113
result = await self.session.execute(stmt)
114114
return result.scalars().all()
115+
116+
async def get_top_bid_for_auction(self, auction_id: str) -> Optional[Bid]:
117+
stmt = (
118+
select(Bid)
119+
.where(Bid.auction_id == auction_id)
120+
.order_by(Bid.bid_price.desc())
121+
.limit(1)
122+
)
123+
result = await self.session.execute(stmt)
124+
return result.scalar_one_or_none()
115125

116126

carrot/app/auction/router.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,14 @@ async def place_bid(
2626
bid_price=bid_data.bid_price
2727
)
2828

29-
return BidResponse.model_validate(bid)
29+
return BidResponse.model_validate(bid)
30+
31+
@auction_router.get("/{auction_id}/top-bid", response_model=BidResponse)
32+
async def get_top_bid(
33+
auction_id: str,
34+
service: Annotated[AuctionService, Depends()],
35+
) -> BidResponse:
36+
37+
top_bid = await service.get_top_bid(auction_id)
38+
39+
return BidResponse.model_validate(top_bid)

carrot/app/auction/services.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
from carrot.app.auction.exceptions import (
1414
AuctionAlreadyExistsError,
15-
AuctionNotFoundError,
15+
AuctionNotFoundError,
16+
NoBidsFoundError,
1617
NotAllowedActionError,
1718
AuctionAlreadyFinishedError
1819
)
@@ -50,4 +51,17 @@ async def place_bid(self, auction_id: str, bidder_id: str, bid_price: int) -> Bi
5051
await self.db_session.commit()
5152
await self.db_session.refresh(new_bid)
5253
await self.db_session.refresh(auction)
53-
return new_bid
54+
return new_bid
55+
56+
async def get_top_bid(self, auction_id: str) -> Bid:
57+
auction = await self.repository.get_auction_by_id(auction_id)
58+
59+
if auction is None:
60+
raise AuctionNotFoundError()
61+
62+
top_bid = await self.repository.get_top_bid_by_auction_id(auction_id)
63+
64+
if top_bid is None:
65+
raise NoBidsFoundError() # 입찰이 없는 경우
66+
67+
return top_bid

0 commit comments

Comments
 (0)