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

32 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.weekday import Weekday 

9 

10from kwai_bc_training.coaches.coach import CoachIdentifier 

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 coaches = frozenset( 

73 [ 

74 coach 

75 async for coach in self._coach_repo.get_by_ids( 

76 *[CoachIdentifier(coachId) for coachId in command.coaches] 

77 ) 

78 ] 

79 ) 

80 else: 

81 coaches = frozenset({}) 

82 

83 training_schedule = await self._repo.get_by_id( 

84 TrainingScheduleIdentifier(command.id) 

85 ) 

86 training_schedule = replace( 

87 training_schedule, 

88 name=command.name, 

89 description=command.description, 

90 weekday=Weekday(command.weekday), 

91 period=TimePeriod.create_from_string( 

92 start=command.start_time, 

93 end=command.end_time, 

94 timezone=command.timezone, 

95 ), 

96 active=command.active, 

97 location=command.location, 

98 remark=command.remark, 

99 team=team, 

100 owner=self._owner, 

101 coaches=coaches, 

102 ) 

103 await self._repo.update(training_schedule) 

104 self._presenter.present(training_schedule)