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
« 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'."""
3from dataclasses import dataclass
4from datetime import datetime
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
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
15@dataclass(frozen=True, kw_only=True, slots=True)
16class GetTrainingSessionsCommand:
17 """Input for the get training sessions use case.
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 """
26 limit: int = 0
27 offset: int = 0
28 coach_uuid: str
29 start: datetime
30 end: datetime
33class GetTrainingSessions:
34 """Use case 'get training sessions'.
36 Returns training sessions of a coach.
37 """
39 def __init__(
40 self,
41 training_repo: TrainingRepository,
42 coach_repo: CoachRepository,
43 presenter: AsyncPresenter[IterableResult[TrainingSession]],
44 ):
45 """Initialize the use case.
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
56 async def execute(self, command: GetTrainingSessionsCommand) -> None:
57 """Execute the use case.
59 Args:
60 command: The input for this use case.
62 Raises:
63 CoachNotFoundException: Raised when the coach is not found.
64 """
65 query = self._training_repo.create_query().order_by_date()
67 if command.start and command.end:
68 query.filter_by_dates(
69 Timestamp(timestamp=command.start), Timestamp(timestamp=command.end)
70 )
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)
77 query.order_by_date()
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)
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 )