Coverage for kwai/modules/training/get_trainings.py: 95%

39 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

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

2from dataclasses import dataclass 

3from datetime import datetime 

4 

5from kwai.core.domain.use_case import UseCaseBrowseResult 

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

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

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

9 TrainingDefinitionIdentifier, 

10) 

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

12 TrainingDefinitionRepository, 

13) 

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

15 

16 

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

18class GetTrainingsCommand: 

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

20 

21 Attributes: 

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

23 offset: Offset to use. Default is None. 

24 year: Only return trainings of this year. 

25 month: Only return trainings of this month. 

26 start: Only return trainings starting from this date. 

27 end: Only return trainings before this date. 

28 coach: Only return trainings with this coach. 

29 definition: Only return trainings created from this definition. 

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

31 """ 

32 

33 limit: int | None = None 

34 offset: int | None = None 

35 year: int | None = None 

36 month: int | None = None 

37 start: datetime | None = None 

38 end: datetime | None = None 

39 coach: int | None = None 

40 definition: int | None = None 

41 active: bool = True 

42 

43 

44class GetTrainings: 

45 """Use case to get trainings.""" 

46 

47 def __init__( 

48 self, 

49 repo: TrainingRepository, 

50 coach_repo: CoachRepository, 

51 training_definition_repo: TrainingDefinitionRepository, 

52 ): 

53 """Initialize use case. 

54 

55 Attributes: 

56 repo: The repository for trainings. 

57 coach_repo: The repository for coaches. 

58 training_definition_repo: The repository for training definitions. 

59 """ 

60 self._repo = repo 

61 self._coach_repo = coach_repo 

62 self._training_definition_repo = training_definition_repo 

63 

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

65 """Execute the use case. 

66 

67 Args: 

68 command: The input for this use case. 

69 

70 Raises: 

71 CoachNotFoundException: Raised when a coach is not found. 

72 TrainingDefinitionNotFoundException: Raised when a definition is not found. 

73 

74 Returns: 

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

76 """ 

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

78 

79 if command.year: 

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

81 

82 if command.start and command.end: 

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

84 

85 if command.coach: 

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

87 query.filter_by_coach(coach) 

88 

89 if command.definition: 

90 definition = await self._training_definition_repo.get_by_id( 

91 TrainingDefinitionIdentifier(command.definition) 

92 ) 

93 query.filter_by_definition(definition) 

94 

95 if command.active: 

96 query.filter_active() 

97 

98 return UseCaseBrowseResult( 

99 count=await query.count(), 

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

101 )