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
« 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
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
17@dataclass(kw_only=True, frozen=True, slots=True)
18class GetTrainingsCommand:
19 """Input for the get trainings use case.
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 """
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
44class GetTrainings:
45 """Use case to get trainings."""
47 def __init__(
48 self,
49 repo: TrainingRepository,
50 coach_repo: CoachRepository,
51 training_definition_repo: TrainingDefinitionRepository,
52 ):
53 """Initialize use case.
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
64 async def execute(self, command: GetTrainingsCommand) -> UseCaseBrowseResult:
65 """Execute the use case.
67 Args:
68 command: The input for this use case.
70 Raises:
71 CoachNotFoundException: Raised when a coach is not found.
72 TrainingDefinitionNotFoundException: Raised when a definition is not found.
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()
79 if command.year:
80 query.filter_by_year_month(command.year, command.month)
82 if command.start and command.end:
83 query.filter_by_dates(command.start, command.end)
85 if command.coach:
86 coach = await self._coach_repo.get_by_id(CoachIdentifier(command.coach))
87 query.filter_by_coach(coach)
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)
95 if command.active:
96 query.filter_active()
98 return UseCaseBrowseResult(
99 count=await query.count(),
100 iterator=self._repo.get_all(query, command.limit, command.offset),
101 )