|
| 1 | +from azure.cosmos import CosmosClient, PartitionKey |
| 2 | + |
1 | 3 | from api.application_settings import ApplicationSettings |
2 | 4 | from api.workflows.products.publish_product.publish_product_workflow import ( |
3 | 5 | PublishProductWorkflow, |
4 | 6 | ) |
| 7 | +from common.application_environment import ApplicationEnvironment |
| 8 | +from domain.entities.product import Product |
5 | 9 |
|
6 | 10 |
|
7 | 11 | class DependencyContainer: |
8 | 12 | @classmethod |
9 | 13 | def initialize(cls) -> None: |
10 | 14 | cls.initialize_application_settings() |
| 15 | + cls.initialize_local_environment() |
11 | 16 |
|
12 | 17 | @classmethod |
13 | 18 | def initialize_application_settings(cls) -> None: |
14 | 19 | cls.application_settings = ApplicationSettings() # type: ignore[reportCallIssue] |
15 | 20 |
|
| 21 | + @classmethod |
| 22 | + def initialize_local_environment(cls) -> None: |
| 23 | + if ApplicationEnvironment.get_current() != ApplicationEnvironment.LOCAL: |
| 24 | + return |
| 25 | + |
| 26 | + with CosmosClient( |
| 27 | + cls.get_application_settings().cosmos_db_no_sql_url, |
| 28 | + cls.get_application_settings().cosmos_db_no_sql_key.get_secret_value(), |
| 29 | + ) as cosmosdb_client: |
| 30 | + cosmosdb_client.create_database_if_not_exists( |
| 31 | + id=cls.get_application_settings().cosmos_db_no_sql_database |
| 32 | + ) |
| 33 | + cosmosdb_database = cosmosdb_client.get_database_client( |
| 34 | + cls.get_application_settings().cosmos_db_no_sql_database |
| 35 | + ) |
| 36 | + cosmosdb_database.create_container_if_not_exists( |
| 37 | + id=Product.__name__, partition_key=PartitionKey("/id") |
| 38 | + ) |
| 39 | + |
16 | 40 | @classmethod |
17 | 41 | def get_application_settings(cls) -> ApplicationSettings: |
18 | 42 | return cls.application_settings |
19 | 43 |
|
20 | 44 | @classmethod |
21 | 45 | def get_publish_product_workflow(cls) -> PublishProductWorkflow: |
22 | | - return PublishProductWorkflow() |
| 46 | + return PublishProductWorkflow( |
| 47 | + application_settings=cls.get_application_settings() |
| 48 | + ) |
0 commit comments