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 

7from kwai_core.domain.value_objects.unique_id import UniqueId 

8 

9from kwai_bc_training.coaches.coach_repository import CoachRepository 

10from kwai_bc_training.trainings.training import ( 

11 TrainingCoachEntity, 

12 TrainingEntity, 

13 TrainingIdentifier, 

14) 

15from kwai_bc_training.trainings.training_repository import TrainingRepository 

16 

17 

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

19class AddCoachToTrainingCommand: 

20 """Input for the use case.""" 

21 

22 training_id: int 

23 coach_uuid: str 

24 head: bool 

25 present: bool = False 

26 payed: bool = False 

27 remark: str = "" 

28 

29 

30class AddCoachToTraining: 

31 """Add a coach to a training.""" 

32 

33 def __init__( 

34 self, 

35 training_repo: TrainingRepository, 

36 coach_repository: CoachRepository, 

37 owner: Owner, 

38 presenter: Presenter[TrainingEntity], 

39 ): 

40 """Initialize the use case. 

41 

42 Attributes: 

43 training_repo: Repository for trainings 

44 coach_repository: Repository for coaches 

45 presenter: Presenter for a training. 

46 """ 

47 self._training_repo = training_repo 

48 self._coach_repository = coach_repository 

49 self._owner = owner 

50 self._presenter = presenter 

51 

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

53 """Execute the use case. 

54 

55 Args: 

56 command: the input for this use case. 

57 

58 Raises: 

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

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

61 """ 

62 training = await self._training_repo.get_by_id( 

63 TrainingIdentifier(command.training_id) 

64 ) 

65 

66 coach_query = self._coach_repository.create_query().filter_by_uuid( 

67 UniqueId.create_from_string(command.coach_uuid) 

68 ) 

69 coach = await self._coach_repository.get(coach_query) 

70 

71 training = training.add_coach( 

72 TrainingCoachEntity( 

73 coach=coach, 

74 present=command.present, 

75 payed=command.payed, 

76 type=1 if command.head else 0, 

77 remark=command.remark, 

78 owner=self._owner, 

79 ) 

80 ) 

81 await self._training_repo.update(training) 

82 

83 self._presenter.present(training)