Coverage for bc/kwai-bc-training/src/kwai_bc_training/update_training.py: 95%

40 statements  

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

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

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.period import Period 

8from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText 

9from kwai_core.domain.value_objects.timestamp import Timestamp 

10 

11from kwai_bc_training.coaches.coach import CoachIdentifier 

12from kwai_bc_training.coaches.coach_repository import CoachRepository 

13from kwai_bc_training.teams.team import TeamIdentifier 

14from kwai_bc_training.teams.team_repository import TeamRepository 

15from kwai_bc_training.training_command import TrainingCommand 

16from kwai_bc_training.trainings.training import TrainingEntity, TrainingIdentifier 

17from kwai_bc_training.trainings.training_repository import TrainingRepository 

18from kwai_bc_training.trainings.training_schedule import ( 

19 TrainingScheduleIdentifier, 

20) 

21from kwai_bc_training.trainings.training_schedule_repository import ( 

22 TrainingScheduleRepository, 

23) 

24from kwai_bc_training.trainings.value_objects import TrainingCoach 

25 

26 

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

28class UpdateTrainingCommand(TrainingCommand): 

29 """Input for the "Update Training" use case.""" 

30 

31 id: int 

32 

33 

34class UpdateTraining: 

35 """Use case for updating a training.""" 

36 

37 def __init__( 

38 self, 

39 repo: TrainingRepository, 

40 schema_repo: TrainingScheduleRepository, 

41 coach_repo: CoachRepository, 

42 team_repo: TeamRepository, 

43 owner: Owner, 

44 presenter: Presenter[TrainingEntity], 

45 ): 

46 """Initialize the use case. 

47 

48 Args: 

49 repo: The repository used to create the training. 

50 schema_repo: The repository for getting the training schema. 

51 coach_repo: The repository for getting the coaches. 

52 team_repo: The repository for getting the teams. 

53 owner: The user that executes this use case. 

54 presenter: A presenter for a training. 

55 """ 

56 self._repo = repo 

57 self._schema_repo = schema_repo 

58 self._coach_repo = coach_repo 

59 self._team_repo = team_repo 

60 self._owner = owner 

61 self._presenter = presenter 

62 

63 async def execute(self, command: UpdateTrainingCommand) -> None: 

64 """Executes the use case. 

65 

66 Args: 

67 command: The input for this use case. 

68 

69 Raises: 

70 TrainingSchemaNotFoundException: Raised when a training schema cannot be found. 

71 """ 

72 training = await self._repo.get_by_id(TrainingIdentifier(command.id)) 

73 

74 if command.schedule: 

75 schedule = await self._schema_repo.get_by_id( 

76 TrainingScheduleIdentifier(command.schedule) 

77 ) 

78 else: 

79 schedule = None 

80 

81 if command.teams: 

82 teams = frozenset( 

83 [ 

84 entity 

85 async for entity in self._team_repo.get_by_ids( 

86 *[TeamIdentifier(team_id) for team_id in command.teams] 

87 ) 

88 ] 

89 ) 

90 else: 

91 teams = frozenset({}) 

92 

93 if command.coaches: 

94 coaches = frozenset( 

95 [ 

96 TrainingCoach(coach=coach, owner=self._owner) 

97 async for coach in self._coach_repo.get_by_ids( 

98 *[CoachIdentifier(coach.id) for coach in command.coaches] 

99 ) 

100 ] 

101 ) 

102 else: 

103 coaches = frozenset({}) 

104 

105 training = replace( 

106 training, 

107 texts=tuple( 

108 [ 

109 LocaleText( 

110 locale=Locale(text.locale), 

111 format=DocumentFormat(text.format), 

112 title=text.title, 

113 content=text.content, 

114 summary=text.summary, 

115 author=self._owner, 

116 ) 

117 for text in command.texts 

118 ] 

119 ), 

120 schedule=schedule, 

121 coaches=coaches, 

122 teams=teams, 

123 period=Period( 

124 start_date=Timestamp.create_from_string(command.start_date), 

125 end_date=Timestamp.create_from_string(command.end_date), 

126 ), 

127 active=command.active, 

128 cancelled=command.cancelled, 

129 location=command.location, 

130 remark=command.remark, 

131 ) 

132 

133 await self._repo.update(training) 

134 self._presenter.present(training)