|
1 | 1 | import dataclasses |
2 | 2 | import json |
3 | | -import logging |
4 | 3 |
|
5 | 4 | import azure.functions as func |
6 | 5 |
|
7 | | -from time_tracker.time_entries._infrastructure import TimeEntriesSQLDao |
8 | | -from time_tracker.time_entries._domain import TimeEntryService, TimeEntry, _use_cases |
| 6 | +from ... import _domain |
| 7 | +from ... import _infrastructure |
9 | 8 | from time_tracker._infrastructure import DB |
10 | 9 |
|
11 | 10 |
|
12 | 11 | def update_time_entry(req: func.HttpRequest) -> func.HttpResponse: |
13 | | - logging.info( |
14 | | - 'Python HTTP trigger function processed a request to update an time entry.' |
15 | | - ) |
16 | | - time_entry_id = req.route_params.get('id') |
17 | | - time_entry_data = req.get_json() if req.get_body() else {} |
18 | | - time_entry_keys = [field.name for field in dataclasses.fields(TimeEntry)] |
19 | | - |
20 | | - if all(key in time_entry_keys for key in time_entry_data.keys()): |
21 | | - try: |
22 | | - response = _update(int(time_entry_id), time_entry_data) |
23 | | - status_code = 200 |
24 | | - except ValueError: |
25 | | - response = b'Invalid Format ID' |
26 | | - status_code = 404 |
27 | | - else: |
28 | | - response = b'Incorrect time entry body' |
29 | | - status_code = 400 |
30 | | - |
31 | | - return func.HttpResponse( |
32 | | - body=response, status_code=status_code, mimetype="application/json" |
33 | | - ) |
34 | | - |
35 | | - |
36 | | -def _update(time_entry_id: int, time_entry_data: dict) -> str: |
37 | 12 | database = DB() |
38 | | - time_entry_use_case = _use_cases.UpdateTimeEntryUseCase( |
39 | | - _create_time_entry_service(database) |
40 | | - ) |
41 | | - time_entry = time_entry_use_case.update_time_entry(time_entry_id, time_entry_data) |
42 | | - return json.dumps(time_entry.__dict__) if time_entry else b'Not Found' |
43 | | - |
44 | | - |
45 | | -def _create_time_entry_service(db: DB): |
46 | | - time_entry_dao = TimeEntriesSQLDao(db) |
47 | | - return TimeEntryService(time_entry_dao) |
| 13 | + time_entry_dao = _infrastructure.TimeEntriesSQLDao(database) |
| 14 | + time_entry_service = _domain.TimeEntryService(time_entry_dao) |
| 15 | + use_case = _domain._use_cases.UpdateTimeEntryUseCase(time_entry_service) |
| 16 | + |
| 17 | + try: |
| 18 | + time_entry_id = int(req.route_params.get("id")) |
| 19 | + time_entry_data = req.get_json() |
| 20 | + status_code = 200 |
| 21 | + |
| 22 | + if not _validate_time_entry(time_entry_data): |
| 23 | + status_code = 400 |
| 24 | + response = b"Incorrect time entry body" |
| 25 | + else: |
| 26 | + response = use_case.update_time_entry(time_entry_id, time_entry_data) |
| 27 | + if not response: |
| 28 | + status_code = 404 |
| 29 | + response = b"Not found" |
| 30 | + else: |
| 31 | + response = json.dumps(response.__dict__) |
| 32 | + |
| 33 | + return func.HttpResponse( |
| 34 | + body=response, |
| 35 | + status_code=status_code, |
| 36 | + mimetype="application/json", |
| 37 | + ) |
| 38 | + |
| 39 | + except ValueError: |
| 40 | + return func.HttpResponse( |
| 41 | + body=b"Invalid Format ID", |
| 42 | + status_code=400, |
| 43 | + mimetype="application/json" |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def _validate_time_entry(time_entry_data: dict) -> bool: |
| 48 | + time_entry_keys = [field.name for field in dataclasses.fields(_domain.TimeEntry)] |
| 49 | + return all(key in time_entry_keys for key in time_entry_data.keys()) |
0 commit comments