Coverage for bc/kwai-bc-training/src/kwai_bc_training/add_coach_to_training.py: 100%

25 statements  

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

1"""Module that defines the use case for adding a coach to a training.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.presenter import Presenter 

6from kwai_core.domain.value_objects.owner import Owner 

7 

8from kwai_bc_training.coaches.coach import CoachIdentifier 

9from kwai_bc_training.coaches.coach_repository import CoachRepository 

10from kwai_bc_training.trainings.training import TrainingEntity, TrainingIdentifier 

11from kwai_bc_training.trainings.training_repository import TrainingRepository 

12from kwai_bc_training.trainings.value_objects import TrainingCoach 

13 

14 

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

16class AddCoachToTrainingCommand: 

17 """Input for the use case.""" 

18 

19 training_id: int 

20 coach_id: int 

21 head: bool 

22 present: bool = False 

23 payed: bool = False 

24 remark: str = "" 

25 

26 

27class AddCoachToTraining: 

28 """Add a coach to a training.""" 

29 

30 def __init__( 

31 self, 

32 training_repo: TrainingRepository, 

33 coach_repository: CoachRepository, 

34 owner: Owner, 

35 presenter: Presenter[TrainingEntity], 

36 ): 

37 """Initialize the use case. 

38 

39 Attributes: 

40 training_repo: Repository for trainings 

41 coach_repository: Repository for coaches 

42 presenter: Presenter for a training. 

43 """ 

44 self._training_repo = training_repo 

45 self._coach_repository = coach_repository 

46 self._owner = owner 

47 self._presenter = presenter 

48 

49 async def execute(self, command: AddCoachToTrainingCommand): 

50 """Execute the use case. 

51 

52 Args: 

53 command: the input for this use case. 

54 

55 Raises: 

56 TrainingNotFoundException: Raised when the training doesn't exist. 

57 CoachNotFoundException: Raised when the coach doesn't exist. 

58 """ 

59 training = await self._training_repo.get_by_id( 

60 TrainingIdentifier(command.training_id) 

61 ) 

62 coach = await self._coach_repository.get_by_id( 

63 CoachIdentifier(command.coach_id) 

64 ) 

65 

66 training = training.add_coach( 

67 TrainingCoach( 

68 coach=coach, 

69 present=command.present, 

70 payed=command.payed, 

71 type=1 if command.head else 0, 

72 remark=command.remark, 

73 owner=self._owner, 

74 ) 

75 ) 

76 await self._training_repo.update(training) 

77 

78 self._presenter.present(training)