88from ... import _infrastructure
99
1010_JSON_PATH = (
11- 'time_entries/_infrastructure/_data_persistence/time_entries_data.json'
11+ 'time_tracker/ time_entries/_infrastructure/_data_persistence/time_entries_data.json'
1212)
1313
14+
1415def create_time_entry (req : func .HttpRequest ) -> func .HttpResponse :
1516
16- time_entry_dao = _infrastructure .TimeEntriesJsonDao (_JSON_PATH )
17- time_entry_service = _domain .TimeEntryService (time_entry_dao )
18- use_case = _domain ._use_cases .CreateTimeEntryUseCase (time_entry_service )
19-
20- time_entry_data = req .get_json ()
21-
22- time_entry_to_create = _domain .TimeEntry (
23- id = None ,
24- start_date = time_entry_data ["start_date" ],
25- owner_id = time_entry_data ["owner_id" ],
26- description = time_entry_data ["description" ],
27- activity_id = time_entry_data ["activity_id" ],
28- uri = time_entry_data ["uri" ],
29- technologies = time_entry_data ["technologies" ],
30- end_date = time_entry_data ["end_date" ],
31- deleted = time_entry_data ["deleted" ],
32- timezone_offset = time_entry_data ["timezone_offset" ],
33- project_id = time_entry_data ["project_id" ]
34- )
35-
36- created_time_entry = use_case .create_time_entry (time_entry_to_create .__dict__ )
37-
38- if not created_time_entry :
17+ time_entry_dao = _infrastructure .TimeEntriesJsonDao (_JSON_PATH )
18+ time_entry_service = _domain .TimeEntryService (time_entry_dao )
19+ use_case = _domain ._use_cases .CreateTimeEntryUseCase (time_entry_service )
20+
21+ time_entry_data = req .get_json ()
22+
23+ validation_errors = _validate_time_entry (time_entry_data )
24+ if validation_errors :
25+ return func .HttpResponse (
26+ body = json .dumps (validation_errors ), status_code = 400 , mimetype = "application/json"
27+ )
28+
29+ time_entry_to_create = _domain .TimeEntry (
30+ id = None ,
31+ start_date = time_entry_data ["start_date" ],
32+ owner_id = time_entry_data ["owner_id" ],
33+ description = time_entry_data ["description" ],
34+ activity_id = time_entry_data ["activity_id" ],
35+ uri = time_entry_data ["uri" ],
36+ technologies = time_entry_data ["technologies" ],
37+ end_date = time_entry_data ["end_date" ],
38+ deleted = False ,
39+ timezone_offset = time_entry_data ["timezone_offset" ],
40+ project_id = time_entry_data ["project_id" ]
41+ )
42+
43+ created_time_entry = use_case .create_time_entry (time_entry_to_create )
44+
45+ if not created_time_entry :
46+ return func .HttpResponse (
47+ body = json .dumps ({'error' : 'time_entry could not be created' }),
48+ status_code = 500 ,
49+ mimetype = "application/json"
50+ )
51+
3952 return func .HttpResponse (
40- body = json .dumps ({ 'error' : 'time_entry could not be created' } ),
41- status_code = 500 ,
53+ body = json .dumps (created_time_entry . __dict__ ),
54+ status_code = 201 ,
4255 mimetype = "application/json"
4356 )
4457
45- return func .HttpResponse (
46- body = json .dumps (created_time_entry .__dict__ ),
47- status_code = 201 ,
48- mimetype = "application/json"
49- )
58+
59+ def _validate_time_entry (time_entry_data : dict ) -> typing .List [str ]:
60+ time_entry_fields = [field .name for field in dataclasses .fields (_domain .TimeEntry )]
61+ time_entry_fields .pop (8 )
62+ missing_keys = [field for field in time_entry_fields if field not in time_entry_data ]
63+ return [
64+ f'The { missing_key } key is missing in the input data'
65+ for missing_key in missing_keys
66+ ]
0 commit comments