11from V2 .source .daos .activities_dao import ActivitiesDao
22from V2 .source .dtos .activity import Activity
3+ import dataclasses
34import json
45import typing
56
6-
77class ActivitiesJsonDao (ActivitiesDao ):
88 def __init__ (self , json_data_file_path : str ):
99 self .json_data_file_path = json_data_file_path
10- self .activity_keys = Activity .__dataclass_fields__ .keys ()
10+ self .activity_keys = [
11+ field .name for field in dataclasses .fields (Activity )
12+ ]
1113
1214 def get_by_id (self , activity_id : str ) -> Activity :
13- activities_grouped_by_id = {
15+ activity = {
1416 activity .get ('id' ): activity
1517 for activity in self .__get_activities_from_file ()
16- }
17- activity = activities_grouped_by_id .get (activity_id )
18- activity_dto = (
19- self .__create_activity_dto (activity ) if activity else None
20- )
18+ }.get (activity_id )
2119
22- return activity_dto
20+ return self . __create_activity_dto ( activity ) if activity else None
2321
2422 def get_all (self ) -> typing .List [Activity ]:
25- all_activities = self . __get_activities_from_file ()
26- activity_dtos = [
27- self . __create_activity_dto ( activity ) for activity in all_activities
23+ return [
24+ self . __create_activity_dto ( activity )
25+ for activity in self . __get_activities_from_file ()
2826 ]
2927
30- return activity_dtos
31-
3228 def __get_activities_from_file (self ) -> typing .List [dict ]:
3329 try :
3430 file = open (self .json_data_file_path )
@@ -42,5 +38,4 @@ def __get_activities_from_file(self) -> typing.List[dict]:
4238
4339 def __create_activity_dto (self , activity : dict ) -> Activity :
4440 activity = {key : activity .get (key ) for key in self .activity_keys }
45- activity_dto = Activity (** activity )
46- return activity_dto
41+ return Activity (** activity )
0 commit comments