Coverage for bc/kwai-bc-training/src/kwai_bc_training/update_training_schedule.py: 91%

33 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for the use case "Update Training Schedule".""" 

2 

3from dataclasses import dataclass, replace 

4 

5from kwai_core.domain.presenter import Presenter 

6from kwai_core.domain.value_objects.owner import Owner 

7from kwai_core.domain.value_objects.time_period import TimePeriod 

8from kwai_core.domain.value_objects.unique_id import UniqueId 

9from kwai_core.domain.value_objects.weekday import Weekday 

10 

11from kwai_bc_training.coaches.coach_repository import CoachRepository 

12from kwai_bc_training.teams.team import TeamIdentifier 

13from kwai_bc_training.teams.team_repository import TeamRepository 

14from kwai_bc_training.training_schedule_command import TrainingScheduleCommand 

15from kwai_bc_training.trainings.training_schedule import ( 

16 TrainingScheduleEntity, 

17 TrainingScheduleIdentifier, 

18) 

19from kwai_bc_training.trainings.training_schedule_repository import ( 

20 TrainingScheduleRepository, 

21) 

22 

23 

24@dataclass(kw_only=True, frozen=True, slots=True) 

25class UpdateTrainingScheduleCommand(TrainingScheduleCommand): 

26 """Input for the use case.""" 

27 

28 id: int 

29 

30 

31class UpdateTrainingSchedule: 

32 """Use case for updating a training schedule.""" 

33 

34 def __init__( 

35 self, 

36 repo: TrainingScheduleRepository, 

37 team_repo: TeamRepository, 

38 coach_repo: CoachRepository, 

39 owner: Owner, 

40 presenter: Presenter[TrainingScheduleEntity], 

41 ): 

42 """Initialize the use case. 

43 

44 Args: 

45 repo: The repository used to update the training schedule. 

46 team_repo: A repository for getting the team. 

47 coach_repo: A repository for getting the coaches. 

48 owner: The user that executes this use case. 

49 presenter: A presenter to present a training schedule. 

50 """ 

51 self._repo = repo 

52 self._team_repo = team_repo 

53 self._coach_repo = coach_repo 

54 self._owner = owner 

55 self._presenter = presenter 

56 

57 async def execute(self, command: UpdateTrainingScheduleCommand) -> None: 

58 """Execute the use case. 

59 

60 Args: 

61 command: The input for this use case. 

62 

63 Raises: 

64 TrainingSchemaNotFoundException: when the training schedule does not exist. 

65 """ 

66 if command.team_id is not None: 

67 team = await self._team_repo.get_by_id(TeamIdentifier(command.team_id)) 

68 else: 

69 team = None 

70 

71 if command.coaches: 

72 coach_query = self._coach_repo.create_query().filter_by_uuids( 

73 *[ 

74 UniqueId.create_from_string(coach_uuid) 

75 for coach_uuid in command.coaches 

76 ] 

77 ) 

78 coaches = frozenset( 

79 [coach async for coach in self._coach_repo.get_all(coach_query)] 

80 ) 

81 else: 

82 coaches = frozenset({}) 

83 

84 training_schedule = await self._repo.get_by_id( 

85 TrainingScheduleIdentifier(command.id) 

86 ) 

87 training_schedule = replace( 

88 training_schedule, 

89 name=command.name, 

90 description=command.description, 

91 weekday=Weekday(command.weekday), 

92 period=TimePeriod.create_from_string( 

93 start=command.start_time, 

94 end=command.end_time, 

95 timezone=command.timezone, 

96 ), 

97 active=command.active, 

98 location=command.location, 

99 remark=command.remark, 

100 team=team, 

101 owner=self._owner, 

102 coaches=coaches, 

103 ) 

104 await self._repo.update(training_schedule) 

105 self._presenter.present(training_schedule)