Coverage for src/kwai/modules/training/update_training.py: 95%

40 statements  

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

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

2 

3from dataclasses import dataclass 

4 

5from kwai.core.domain.entity import Entity 

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 

10from kwai.modules.training.coaches.coach import CoachIdentifier 

11from kwai.modules.training.coaches.coach_repository import CoachRepository 

12from kwai.modules.training.teams.team import TeamIdentifier 

13from kwai.modules.training.teams.team_repository import TeamRepository 

14from kwai.modules.training.training_command import TrainingCommand 

15from kwai.modules.training.trainings.training import TrainingEntity, TrainingIdentifier 

16from kwai.modules.training.trainings.training_definition import ( 

17 TrainingDefinitionIdentifier, 

18) 

19from kwai.modules.training.trainings.training_definition_repository import ( 

20 TrainingDefinitionRepository, 

21) 

22from kwai.modules.training.trainings.training_repository import TrainingRepository 

23from kwai.modules.training.trainings.value_objects import TrainingCoach 

24 

25 

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

27class UpdateTrainingCommand(TrainingCommand): 

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

29 

30 id: int 

31 

32 

33class UpdateTraining: 

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

35 

36 def __init__( 

37 self, 

38 repo: TrainingRepository, 

39 definition_repo: TrainingDefinitionRepository, 

40 coach_repo: CoachRepository, 

41 team_repo: TeamRepository, 

42 owner: Owner, 

43 ): 

44 """Initialize the use case. 

45 

46 Args: 

47 repo: The repository used to create the training. 

48 definition_repo: The repository for getting the training definition. 

49 coach_repo: The repository for getting the coaches. 

50 team_repo: The repository for getting the teams. 

51 owner: The user that executes this use case. 

52 """ 

53 self._repo = repo 

54 self._definition_repo = definition_repo 

55 self._coach_repo = coach_repo 

56 self._team_repo = team_repo 

57 self._owner = owner 

58 

59 async def execute(self, command: UpdateTrainingCommand) -> TrainingEntity: 

60 """Executes the use case. 

61 

62 Args: 

63 command: The input for this use case. 

64 

65 Raises: 

66 TrainingDefinitionNotFoundException: Raised when a training definition 

67 cannot be found. 

68 """ 

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

70 

71 if command.definition: 

72 definition = await self._definition_repo.get_by_id( 

73 TrainingDefinitionIdentifier(command.definition) 

74 ) 

75 else: 

76 definition = None 

77 

78 if command.teams: 

79 teams = [ 

80 entity 

81 async for entity in self._team_repo.get_by_ids( 

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

83 ) 

84 ] 

85 else: 

86 teams = [] 

87 

88 if command.coaches: 

89 coaches = [ 

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

91 async for coach in self._coach_repo.get_by_ids( 

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

93 ) 

94 ] 

95 else: 

96 coaches = [] 

97 

98 training = Entity.replace( 

99 training, 

100 texts=[ 

101 LocaleText( 

102 locale=Locale(text.locale), 

103 format=DocumentFormat(text.format), 

104 title=text.title, 

105 content=text.content, 

106 summary=text.summary, 

107 author=self._owner, 

108 ) 

109 for text in command.texts 

110 ], 

111 definition=definition, 

112 coaches=coaches, 

113 teams=teams, 

114 period=Period( 

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

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

117 ), 

118 active=command.active, 

119 cancelled=command.cancelled, 

120 location=command.location, 

121 remark=command.remark, 

122 ) 

123 

124 await self._repo.update(training) 

125 return training