1
1
from fastapi import APIRouter
2
2
3
- from api .models .bidding import BiddingReadModel
3
+ from api .models .bidding import BiddingResponse , PlaceBidRequest
4
4
from api .shared import dependency
5
5
from config .container import Container , inject
6
+ from modules .bidding .application .command import PlaceBidCommand , RetractBidCommand
6
7
from modules .bidding .application .query .get_bidding_details import GetBiddingDetails
7
8
from seedwork .application import Application
8
9
13
14
"""
14
15
15
16
16
- @router .get ("/bidding/{listing_id}" , tags = ["bidding" ], response_model = BiddingReadModel )
17
+ @router .get ("/bidding/{listing_id}" , tags = ["bidding" ], response_model = BiddingResponse )
17
18
@inject
18
19
async def get_bidding_details_of_listing (
19
20
listing_id ,
@@ -25,7 +26,67 @@ async def get_bidding_details_of_listing(
25
26
query = GetBiddingDetails (listing_id = listing_id )
26
27
query_result = app .execute_query (query )
27
28
payload = query_result .payload
28
- return BiddingReadModel (
29
+ return BiddingResponse (
30
+ listing_id = str (payload .id ),
31
+ auction_end_date = payload .ends_at ,
32
+ bids = payload .bids ,
33
+ )
34
+
35
+
36
+ @router .post (
37
+ "/bidding/{listing_id}/place_bid" , tags = ["bidding" ], response_model = BiddingResponse
38
+ )
39
+ @inject
40
+ async def place_bid (
41
+ listing_id ,
42
+ request_body : PlaceBidRequest ,
43
+ app : Application = dependency (Container .application ),
44
+ ):
45
+ """
46
+ Places a bid on a listing
47
+ """
48
+ # TODO: get bidder from current user
49
+
50
+ command = PlaceBidCommand (
51
+ listing_id = listing_id ,
52
+ bidder_id = request_body .bidder_id ,
53
+ amount = request_body .amount ,
54
+ )
55
+ app .execute_command (command )
56
+
57
+ query = GetBiddingDetails (listing_id = listing_id )
58
+ query_result = app .execute_query (query )
59
+ payload = query_result .payload
60
+ return BiddingResponse (
61
+ listing_id = str (payload .id ),
62
+ auction_end_date = payload .ends_at ,
63
+ bids = payload .bids ,
64
+ )
65
+
66
+
67
+ @router .post (
68
+ "/bidding/{listing_id}/retract_bid" ,
69
+ tags = ["bidding" ],
70
+ response_model = BiddingResponse ,
71
+ )
72
+ @inject
73
+ async def retract_bid (
74
+ listing_id ,
75
+ app : Application = dependency (Container .application ),
76
+ ):
77
+ """
78
+ Retracts a bid from a listing
79
+ """
80
+ command = RetractBidCommand (
81
+ listing_id = listing_id ,
82
+ bidder_id = "" ,
83
+ )
84
+ app .execute_command (command )
85
+
86
+ query = GetBiddingDetails (listing_id = listing_id )
87
+ query_result = app .execute_query (query )
88
+ payload = query_result .payload
89
+ return BiddingResponse (
29
90
listing_id = str (payload .id ),
30
91
auction_end_date = payload .ends_at ,
31
92
bids = payload .bids ,
0 commit comments