Coverage for src/kwai/modules/training/get_trainings.py: 98%

40 statements  

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

1"""Module for the use case get trainings.""" 

2 

3from dataclasses import dataclass 

4from datetime import datetime 

5 

6from kwai.core.domain.use_case import UseCaseBrowseResult 

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

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

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

10 TrainingDefinitionIdentifier, 

11) 

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

13 TrainingDefinitionRepository, 

14) 

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

16 

17 

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

19class GetTrainingsCommand: 

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

21 

22 Attributes: 

23 limit: the max. number of elements to return. Default is None, which means all. 

24 offset: Offset to use. Default is None. 

25 year: Only return trainings of this year. 

26 month: Only return trainings of this month. 

27 start: Only return trainings starting from this date. 

28 end: Only return trainings before this date. 

29 coach: Only return trainings with this coach. 

30 definition: Only return trainings created from this definition. 

31 active: Only return trainings that are active (default is True). 

32 """ 

33 

34 limit: int | None = None 

35 offset: int | None = None 

36 year: int | None = None 

37 month: int | None = None 

38 start: datetime | None = None 

39 end: datetime | None = None 

40 coach: int | None = None 

41 definition: int | None = None 

42 active: bool = True 

43 

44 

45class GetTrainings: 

46 """Use case to get trainings.""" 

47 

48 def __init__( 

49 self, 

50 repo: TrainingRepository, 

51 coach_repo: CoachRepository, 

52 training_definition_repo: TrainingDefinitionRepository, 

53 ): 

54 """Initialize use case. 

55 

56 Attributes: 

57 repo: The repository for trainings. 

58 coach_repo: The repository for coaches. 

59 training_definition_repo: The repository for training definitions. 

60 """ 

61 self._repo = repo 

62 self._coach_repo = coach_repo 

63 self._training_definition_repo = training_definition_repo 

64 

65 async def execute(self, command: GetTrainingsCommand) -> UseCaseBrowseResult: 

66 """Execute the use case. 

67 

68 Args: 

69 command: The input for this use case. 

70 

71 Raises: 

72 CoachNotFoundException: Raised when a coach is not found. 

73 TrainingDefinitionNotFoundException: Raised when a definition is not found. 

74 

75 Returns: 

76 A tuple with the number of entities and an iterator for training entities. 

77 """ 

78 query = self._repo.create_query().order_by_date() 

79 

80 if command.year: 

81 query.filter_by_year_month(command.year, command.month) 

82 

83 if command.start and command.end: 

84 query.filter_by_dates(command.start, command.end) 

85 

86 if command.coach: 

87 coach = await self._coach_repo.get_by_id(CoachIdentifier(command.coach)) 

88 query.filter_by_coach(coach) 

89 

90 if command.definition: 

91 definition = await self._training_definition_repo.get_by_id( 

92 TrainingDefinitionIdentifier(command.definition) 

93 ) 

94 query.filter_by_definition(definition) 

95 

96 if command.active: 

97 query.filter_active() 

98 

99 query.order_by_date() 

100 

101 return UseCaseBrowseResult( 

102 count=await query.count(), 

103 iterator=self._repo.get_all(query, command.limit, command.offset), 

104 )