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

30 statements  

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

1"""Module that defines the use case 'get training sessions'.""" 

2 

3from dataclasses import dataclass 

4from datetime import datetime 

5 

6from kwai_core.domain.presenter import AsyncPresenter, IterableResult 

7from kwai_core.domain.value_objects.timestamp import Timestamp 

8from kwai_core.domain.value_objects.unique_id import UniqueId 

9 

10from kwai_bc_training.coaches.coach_repository import CoachRepository 

11from kwai_bc_training.trainings.training import TrainingSession 

12from kwai_bc_training.trainings.training_repository import TrainingRepository 

13 

14 

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

16class GetTrainingSessionsCommand: 

17 """Input for the get training sessions use case. 

18 

19 Attributes: 

20 limit: the max. number of training sessions to return. Default is 0, which means all training sessions. 

21 offset: Offset to use. 

22 start: Only return sessions starting from this date. 

23 end: Onl return session before this. 

24 """ 

25 

26 limit: int = 0 

27 offset: int = 0 

28 coach_uuid: str 

29 start: datetime 

30 end: datetime 

31 

32 

33class GetTrainingSessions: 

34 """Use case 'get training sessions'. 

35 

36 Returns training sessions of a coach. 

37 """ 

38 

39 def __init__( 

40 self, 

41 training_repo: TrainingRepository, 

42 coach_repo: CoachRepository, 

43 presenter: AsyncPresenter[IterableResult[TrainingSession]], 

44 ): 

45 """Initialize the use case. 

46 

47 Attributes: 

48 training_repo: the training repository. 

49 coach_repo: the coach repository. 

50 presenter: the presenter. 

51 """ 

52 self._training_repo = training_repo 

53 self._coach_repo = coach_repo 

54 self._presenter = presenter 

55 

56 async def execute(self, command: GetTrainingSessionsCommand) -> None: 

57 """Execute the use case. 

58 

59 Args: 

60 command: The input for this use case. 

61 

62 Raises: 

63 CoachNotFoundException: Raised when the coach is not found. 

64 """ 

65 query = self._training_repo.create_query().order_by_date() 

66 

67 if command.start and command.end: 

68 query.filter_by_dates( 

69 Timestamp(timestamp=command.start), Timestamp(timestamp=command.end) 

70 ) 

71 

72 coach_uuid = UniqueId.create_from_string(command.coach_uuid) 

73 coach_query = self._coach_repo.create_query().filter_by_uuid(coach_uuid) 

74 coach = await self._coach_repo.get(coach_query) 

75 query.filter_by_coach(coach) 

76 

77 query.order_by_date() 

78 

79 async def generate_training_session(): 

80 async for training_entity in self._training_repo.get_all( 

81 query, command.limit, command.offset 

82 ): 

83 yield training_entity.get_training_session(coach_uuid) 

84 

85 await self._presenter.present( 

86 IterableResult[TrainingSession]( 

87 count=await query.count(), 

88 limit=command.limit, 

89 offset=command.offset, 

90 iterator=generate_training_session(), 

91 ) 

92 )