|
7 | 7 | CosmosDBModel, |
8 | 8 | CosmosDBDao, |
9 | 9 | CosmosDBRepository, |
| 10 | + CustomError, |
10 | 11 | ) |
11 | 12 | from time_tracker_api.database import CRUDDao, APICosmosDBDao |
12 | 13 | from typing import List, Callable |
13 | 14 | from commons.data_access_layer.database import EventContext |
14 | 15 | from utils.enums.status import Status |
15 | 16 | from utils.query_builder import CosmosDBQueryBuilder |
16 | | -from commons.data_access_layer.file_stream import FileStream |
| 17 | +from commons.data_access_layer.file import FileStream |
| 18 | + |
17 | 19 |
|
18 | 20 | class ActivityDao(CRUDDao): |
19 | 21 | pass |
@@ -118,16 +120,28 @@ def find_all_from_blob_storage( |
118 | 120 | self, |
119 | 121 | event_context: EventContext, |
120 | 122 | mapper: Callable = None, |
| 123 | + activity_id: str = None, |
121 | 124 | file_name: str = "activity.json", |
122 | | - ): |
| 125 | + ): |
123 | 126 | tenant_id_value = self.find_partition_key_value(event_context) |
124 | 127 | function_mapper = self.get_mapper_or_dict(mapper) |
125 | 128 | if tenant_id_value is None: |
126 | 129 | return [{"result": "error", "message": "tenant_id is None"}] |
127 | | - |
128 | | - fs = FileStream("storageaccounteystr82c5","tt-common-files") |
| 130 | + |
| 131 | + fs = FileStream("tt-common-files") |
129 | 132 | result = fs.get_file_stream(file_name) |
130 | | - return list(map(function_mapper, json.load(result))) if result is not None else [] |
| 133 | + result_json = list(map(function_mapper, json.loads( |
| 134 | + result))) if result is not None else [] |
| 135 | + print(result_json) |
| 136 | + if activity_id is not None: |
| 137 | + result_json = [ |
| 138 | + activity |
| 139 | + for activity in result_json |
| 140 | + if activity.id == activity_id |
| 141 | + ] |
| 142 | + |
| 143 | + return result_json |
| 144 | + |
131 | 145 |
|
132 | 146 | class ActivityCosmosDBDao(APICosmosDBDao, ActivityDao): |
133 | 147 | def __init__(self, repository): |
@@ -162,11 +176,25 @@ def get_all_v1( |
162 | 176 | ) |
163 | 177 | return activities |
164 | 178 |
|
165 | | - def get_all(self,**kwargs) -> list: |
| 179 | + def get_all(self, **kwargs) -> list: |
166 | 180 | event_ctx = self.create_event_context("read-many") |
167 | | - activities = self.repository.find_all_from_blob_storage(event_context=event_ctx) |
| 181 | + activities = self.repository.find_all_from_blob_storage( |
| 182 | + event_context=event_ctx |
| 183 | + ) |
168 | 184 | return activities |
169 | 185 |
|
| 186 | + def get(self, id: str = None) -> list: |
| 187 | + event_ctx = self.create_event_context("read-many") |
| 188 | + activities = self.repository.find_all_from_blob_storage( |
| 189 | + event_context=event_ctx, |
| 190 | + activity_id=id |
| 191 | + ) |
| 192 | + |
| 193 | + if len(activities) > 0: |
| 194 | + return activities[0] |
| 195 | + else: |
| 196 | + raise CustomError(404, "It was not found") |
| 197 | + |
170 | 198 | def create(self, activity_payload: dict): |
171 | 199 | event_ctx = self.create_event_context('create') |
172 | 200 | activity_payload['status'] = Status.ACTIVE.value |
|
0 commit comments