|
1 | | -# configure logger prior to first usage |
2 | | -import uuid |
3 | | - |
4 | | -from sqlalchemy.orm import Session |
5 | | - |
6 | 1 | from config.container import Container |
7 | | -from modules.catalog.domain.entities import Listing, Money |
8 | | -from modules.catalog.infrastructure.listing_repository import ( |
9 | | - Base, |
10 | | - PostgresJsonListingRepository, |
| 2 | +from modules.catalog.application.command.create_listing_draft import ( |
| 3 | + CreateListingDraftCommand, |
11 | 4 | ) |
| 5 | +from modules.catalog.domain.entities import Money |
| 6 | +from modules.catalog.infrastructure.listing_repository import Base |
12 | 7 | from seedwork.infrastructure.logging import LoggerFactory, logger |
13 | | -from seedwork.infrastructure.request_context import request_context |
14 | 8 |
|
15 | 9 | # a sample command line script to print all listings |
16 | 10 | # run with "cd src && python -m cli" |
17 | 11 |
|
18 | | - |
| 12 | +# configure logger prior to first usage |
19 | 13 | LoggerFactory.configure(logger_name="cli") |
20 | 14 |
|
21 | 15 |
|
22 | 16 | container = Container() |
23 | 17 | container.config.from_dict( |
24 | 18 | dict( |
25 | | - DATABASE_URL="sqlite+pysqlite:///:memory:", |
| 19 | + # DATABASE_URL="sqlite+pysqlite:///:memory:", |
| 20 | + DATABASE_URL="postgresql://postgres:password@localhost:5432/postgres", |
26 | 21 | DEBUG=True, |
27 | 22 | ) |
28 | 23 | ) |
29 | 24 |
|
30 | 25 | engine = container.engine() |
31 | 26 | Base.metadata.create_all(engine) |
32 | 27 |
|
33 | | -listing_id = uuid.uuid4() |
34 | | - |
35 | | -with Session(engine) as session: |
36 | | - request_context.correlation_id.set(uuid.uuid4()) |
37 | | - logger.info("Session 1") |
38 | | - print(engine, session) |
39 | | - repo = PostgresJsonListingRepository(session) |
40 | | - listing = Listing( |
41 | | - id=listing_id, |
42 | | - seller_id=uuid.uuid4(), |
43 | | - title="Foo", |
44 | | - description="", |
45 | | - ask_price=Money(1), |
46 | | - ) |
47 | | - repo.add(listing) |
| 28 | +catalog_module = container.catalog_module() |
48 | 29 |
|
| 30 | +with catalog_module.unit_of_work() as uow: |
| 31 | + logger.info(f"executing unit of work") |
| 32 | + count = uow.listing_repository.count() |
| 33 | + logger.info(f"{count} listing in the repository") |
49 | 34 |
|
50 | | -with Session(engine) as session: |
51 | | - request_context.correlation_id.set(uuid.uuid4()) |
52 | | - logger.info("Session 2") |
53 | | - repo = PostgresJsonListingRepository(session) |
54 | | - repo.get_by_id(listing_id) |
55 | | - |
56 | | - |
57 | | -# # configure catalog module |
58 | | -# |
59 | | -# |
60 | | -# # instantiate catalog module |
61 | | -# catalog_module = container.catalog_module() |
62 | | -# |
63 | | -# logger.info("Application configured") |
64 | | -# |
65 | | -# # let's generate a fake seller id for now |
66 | | -# seller_id = SellerRepository.next_id() |
67 | | -# |
68 | | -# |
69 | | -# ############################### |
70 | | -# from modules.catalog.infrastructure.listing_repository import PostgresJsonListingRepository |
71 | | -# |
72 | | -# |
73 | | -# session = ... |
74 | | -# repository = PostgresJsonListingRepository(sqla_session=session) |
75 | | - |
76 | | - |
77 | | -# from contextlib import contextmanager |
78 | | -# from modules.catalog.module import CatalogModule |
79 | | -# |
80 | | -# @contextmanager |
81 | | -# def business_tansaction(): |
82 | | -# try: |
83 | | -# unit_of_work = ... |
84 | | -# |
85 | | -# yield CatalogModule(unit_of_work, listing_repository=container.listing_repository()) |
86 | | -# finally: |
87 | | -# ... |
88 | | -# |
89 | | -# |
90 | | -# |
91 | | -# |
92 | | -# with BusinessTransactionOn(CatalogModule) as catalog_module: |
93 | | -# catalog_module.foo_repo() |
94 | | -# |
95 | | -# |
96 | | -# |
97 | | -# with catalog_module as transaction: |
98 | | -# print(transaction) |
99 | | - |
| 35 | +with catalog_module.unit_of_work(): |
| 36 | + logger.info(f"adding new draft") |
| 37 | + catalog_module.execute_command( |
| 38 | + CreateListingDraftCommand( |
| 39 | + title="First listing", description=".", ask_price=Money(100), seller_id=None |
| 40 | + ) |
| 41 | + ) |
100 | 42 |
|
101 | | -# |
102 | | -# # interact with a catalog module by issuing queries and commands |
103 | | -# # use request context if you want to logically separate queries/commands |
104 | | -# # from each other in the logs |
105 | | -# with request_context: |
106 | | -# command = CreateListingDraftCommand( |
107 | | -# title="Foo", description="Bar", ask_price=1, seller_id=seller_id |
108 | | -# ) |
109 | | -# result = catalog_module.execute_command(command) |
110 | | -# print(result) |
111 | | -# if result.is_ok(): |
112 | | -# logger.info("Draft added") |
113 | | -# else: |
114 | | -# logger.error(result.get_errors()) |
115 | | -# |
116 | | -# with request_context: |
117 | | -# query_result = catalog_module.execute_query(GetAllListings()) |
118 | | -# logger.info(f"All listings: {query_result.result}") |
119 | | -# |
120 | | -# with request_context: |
121 | | -# query_result = catalog_module.execute_query( |
122 | | -# GetListingsOfSeller(seller_id=seller_id) |
123 | | -# ) |
124 | | -# logger.info(f"Listings of seller {seller_id}: {query_result.result}") |
| 43 | +with catalog_module.unit_of_work() as uow: |
| 44 | + logger.info(f"executing unit of work") |
| 45 | + count = uow.listing_repository.count() |
| 46 | + logger.info(f"{count} listing in the repository") |
0 commit comments