Coverage for bc/kwai-bc-training/src/kwai_bc_training/create_training_schedule.py: 96%

28 statements  

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

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

2 

3from kwai_core.domain.presenter import Presenter 

4from kwai_core.domain.value_objects.owner import Owner 

5from kwai_core.domain.value_objects.time_period import TimePeriod 

6from kwai_core.domain.value_objects.weekday import Weekday 

7 

8from kwai_bc_training.coaches.coach import CoachIdentifier 

9from kwai_bc_training.coaches.coach_repository import CoachRepository 

10from kwai_bc_training.teams.team import TeamIdentifier 

11from kwai_bc_training.teams.team_repository import TeamRepository 

12from kwai_bc_training.training_schedule_command import TrainingScheduleCommand 

13from kwai_bc_training.trainings.training_schedule import TrainingScheduleEntity 

14from kwai_bc_training.trainings.training_schedule_repository import ( 

15 TrainingScheduleRepository, 

16) 

17 

18 

19CreateTrainingScheduleCommand = TrainingScheduleCommand 

20 

21 

22class CreateTrainingSchedule: 

23 """Use case for creating a training schedule.""" 

24 

25 def __init__( 

26 self, 

27 repo: TrainingScheduleRepository, 

28 team_repo: TeamRepository, 

29 coach_repo: CoachRepository, 

30 owner: Owner, 

31 presenter: Presenter[TrainingScheduleEntity], 

32 ): 

33 """Initialize the use case. 

34 

35 Args: 

36 repo: The repository used to create the training schedule. 

37 team_repo: The repository for getting the team. 

38 coach_repo: The repository for getting the coaches. 

39 owner: The user that executes this use case. 

40 presenter: A presenter for presenting the training schedule. 

41 """ 

42 self._repo = repo 

43 self._team_repo = team_repo 

44 self._coach_repo = coach_repo 

45 self._owner = owner 

46 self._presenter = presenter 

47 

48 async def execute(self, command: CreateTrainingScheduleCommand) -> None: 

49 """Execute the use case. 

50 

51 Args: 

52 command: The input for this use case. 

53 """ 

54 if command.team_id is not None: 

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

56 else: 

57 team = None 

58 

59 if command.coaches: 

60 coaches = frozenset( 

61 [ 

62 coach 

63 async for coach in self._coach_repo.get_by_ids( 

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

65 ) 

66 ] 

67 ) 

68 else: 

69 coaches = frozenset({}) 

70 

71 training_schedule = TrainingScheduleEntity( 

72 name=command.name, 

73 description=command.description, 

74 weekday=Weekday(command.weekday), 

75 period=TimePeriod.create_from_string( 

76 start=command.start_time, 

77 end=command.end_time, 

78 timezone=command.timezone, 

79 ), 

80 active=command.active, 

81 location=command.location, 

82 remark=command.remark or "", 

83 owner=self._owner, 

84 team=team, 

85 coaches=coaches, 

86 ) 

87 self._presenter.present(await self._repo.create(training_schedule))