|
1 | 1 | from dataclasses import dataclass |
2 | | - |
3 | 2 | from azure.cosmos import PartitionKey |
4 | | - |
5 | 3 | from commons.data_access_layer.cosmos_db import CosmosDBModel, CosmosDBDao, CosmosDBRepository |
6 | 4 | from time_tracker_api.database import CRUDDao, APICosmosDBDao |
| 5 | +from time_tracker_api.customers.customers_model import create_dao as customers_create_dao |
| 6 | +from time_tracker_api.customers.customers_model import CustomerCosmosDBModel |
7 | 7 |
|
8 | 8 |
|
9 | 9 | class ProjectDao(CRUDDao): |
@@ -35,19 +35,48 @@ class ProjectCosmosDBModel(CosmosDBModel): |
35 | 35 | def __init__(self, data): |
36 | 36 | super(ProjectCosmosDBModel, self).__init__(data) # pragma: no cover |
37 | 37 |
|
| 38 | + def __contains__(self, item): |
| 39 | + if type(item) is CustomerCosmosDBModel: |
| 40 | + return True if item.id == self.customer_id else False |
| 41 | + else: |
| 42 | + raise NotImplementedError |
| 43 | + |
38 | 44 | def __repr__(self): |
39 | 45 | return '<Project %r>' % self.name # pragma: no cover |
40 | 46 |
|
41 | 47 | def __str___(self): |
42 | 48 | return "the project \"%s\"" % self.name # pragma: no cover |
43 | 49 |
|
44 | 50 |
|
45 | | -def create_dao() -> ProjectDao: |
46 | | - repository = CosmosDBRepository.from_definition(container_definition, |
47 | | - mapper=ProjectCosmosDBModel) |
| 51 | +class ProjectCosmosDBRepository(CosmosDBRepository): |
| 52 | + def __init__(self): |
| 53 | + CosmosDBRepository.__init__(self, container_id=container_definition['id'], |
| 54 | + partition_key_attribute='tenant_id', |
| 55 | + mapper=ProjectCosmosDBModel) |
| 56 | + |
48 | 57 |
|
49 | | - class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao): |
50 | | - def __init__(self): |
51 | | - CosmosDBDao.__init__(self, repository) |
| 58 | +class ProjectCosmosDBDao(APICosmosDBDao, ProjectDao): |
| 59 | + def __init__(self, repository): |
| 60 | + CosmosDBDao.__init__(self, repository) |
| 61 | + |
| 62 | + def get_all(self, conditions: dict = None, **kwargs) -> list: |
| 63 | + """ |
| 64 | + Get all the projects an active client has |
| 65 | + :param (dict) conditions: Conditions for querying the database |
| 66 | + :param (dict) kwargs: Pass arguments |
| 67 | + :return (list): ProjectCosmosDBModel object list |
| 68 | + """ |
| 69 | + event_ctx = self.create_event_context("read-many") |
| 70 | + customer_dao = customers_create_dao() |
| 71 | + customers = customer_dao.get_all() |
| 72 | + |
| 73 | + customers_id = [customer.id for customer in customers] |
| 74 | + conditions = conditions if conditions else {} |
| 75 | + custom_condition = "c.customer_id IN {}".format(str(tuple(customers_id))) |
| 76 | + return self.repository.find_all(event_ctx, conditions, custom_sql_conditions=[custom_condition], **kwargs) |
| 77 | + |
| 78 | + |
| 79 | +def create_dao() -> ProjectDao: |
| 80 | + repository = ProjectCosmosDBRepository() |
52 | 81 |
|
53 | | - return ProjectCosmosDBDao() |
| 82 | + return ProjectCosmosDBDao(repository) |
0 commit comments