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