33from api .models import ListingIndexModel , ListingReadModel , ListingWriteModel
44from api .shared import dependency
55from config .container import Container , inject
6+ from modules .catalog import CatalogModule
67from modules .catalog .application .command .create_listing_draft import (
78 CreateListingDraftCommand ,
89)
910from modules .catalog .application .query .get_all_listings import GetAllListings
1011from modules .catalog .application .query .get_listing_details import GetListingDetails
11- from modules . catalog . module import CatalogModule
12+ from seedwork . domain . value_objects import Money
1213from seedwork .infrastructure .request_context import request_context
1314
1415router = APIRouter ()
@@ -23,21 +24,24 @@ async def get_all_listings(
2324 Shows all published listings in the catalog
2425 """
2526 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 )
2830
2931
3032@router .get ("/catalog/{listing_id}" , tags = ["catalog" ], response_model = ListingReadModel )
3133@inject
3234async def get_listing_details (
33- listing_id , module : CatalogModule = dependency (Container .catalog_module )
35+ listing_id ,
36+ module : CatalogModule = dependency (Container .catalog_module ),
3437):
3538 """
3639 Shows listing details
3740 """
3841 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
4145
4246
4347@router .post (
@@ -51,19 +55,21 @@ async def create_listing(
5155 """
5256 Creates a new listing.
5357 """
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 ,
6163 )
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
6270
63- query = GetListingDetails (listing_id = command_result .result )
64- query_result = module .execute_query (query )
65- return query_result .result
6671
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