3
3
from api .models import ListingIndexModel , ListingReadModel , ListingWriteModel
4
4
from api .shared import dependency
5
5
from config .container import Container , inject
6
+ from modules .catalog import CatalogModule
6
7
from modules .catalog .application .command .create_listing_draft import (
7
8
CreateListingDraftCommand ,
8
9
)
9
10
from modules .catalog .application .query .get_all_listings import GetAllListings
10
11
from modules .catalog .application .query .get_listing_details import GetListingDetails
11
- from modules . catalog . module import CatalogModule
12
+ from seedwork . domain . value_objects import Money
12
13
from seedwork .infrastructure .request_context import request_context
13
14
14
15
router = APIRouter ()
@@ -23,21 +24,24 @@ async def get_all_listings(
23
24
Shows all published listings in the catalog
24
25
"""
25
26
query = GetAllListings ()
26
- query_result = module .execute_query (query )
27
- return dict (data = query_result .result )
27
+ with module .unit_of_work ():
28
+ query_result = module .execute_query (query )
29
+ return dict (data = query_result .result )
28
30
29
31
30
32
@router .get ("/catalog/{listing_id}" , tags = ["catalog" ], response_model = ListingReadModel )
31
33
@inject
32
34
async def get_listing_details (
33
- listing_id , module : CatalogModule = dependency (Container .catalog_module )
35
+ listing_id ,
36
+ module : CatalogModule = dependency (Container .catalog_module ),
34
37
):
35
38
"""
36
39
Shows listing details
37
40
"""
38
41
query = GetListingDetails (listing_id = listing_id )
39
- query_result = module .execute_query (query )
40
- return query_result .result
42
+ with module .unit_of_work ():
43
+ query_result = module .execute_query (query )
44
+ return query_result .result
41
45
42
46
43
47
@router .post (
@@ -51,19 +55,21 @@ async def create_listing(
51
55
"""
52
56
Creates a new listing.
53
57
"""
54
- command_result = module .execute_command (
55
- CreateListingDraftCommand (
56
- title = request_body .title ,
57
- description = "" ,
58
- price = 1 ,
59
- seller_id = request_context .current_user .id ,
60
- )
58
+ command = CreateListingDraftCommand (
59
+ title = request_body .title ,
60
+ description = request_body .description ,
61
+ ask_price = Money (request_body .ask_price_amount , request_body .ask_price_currency ),
62
+ seller_id = request_context .current_user .id ,
61
63
)
64
+ with module .unit_of_work ():
65
+ command_result = module .execute_command (command )
66
+
67
+ query = GetListingDetails (listing_id = command_result .result )
68
+ query_result = module .execute_query (query )
69
+ return query_result .result
62
70
63
- query = GetListingDetails (listing_id = command_result .result )
64
- query_result = module .execute_query (query )
65
- return query_result .result
66
71
67
- # TODO: for now we return just the id, but in the future we should return
68
- # a representation of a newly created listing resource
69
- return {"id" : result .id }
72
+ #
73
+ # # TODO: for now we return just the id, but in the future we should return
74
+ # # a representation of a newly created listing resource
75
+ # return {"id": result.id}
0 commit comments