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
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module for the use case get trainings."""
3from dataclasses import dataclass
4from datetime import datetime
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
18@dataclass(kw_only=True, frozen=True, slots=True)
19class GetTrainingsCommand:
20 """Input for the get trainings use case.
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 """
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
45class GetTrainings:
46 """Use case to get trainings."""
48 def __init__(
49 self,
50 repo: TrainingRepository,
51 coach_repo: CoachRepository,
52 training_definition_repo: TrainingDefinitionRepository,
53 ):
54 """Initialize use case.
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
65 async def execute(self, command: GetTrainingsCommand) -> UseCaseBrowseResult:
66 """Execute the use case.
68 Args:
69 command: The input for this use case.
71 Raises:
72 CoachNotFoundException: Raised when a coach is not found.
73 TrainingDefinitionNotFoundException: Raised when a definition is not found.
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()
80 if command.year:
81 query.filter_by_year_month(command.year, command.month)
83 if command.start and command.end:
84 query.filter_by_dates(command.start, command.end)
86 if command.coach:
87 coach = await self._coach_repo.get_by_id(CoachIdentifier(command.coach))
88 query.filter_by_coach(coach)
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)
96 if command.active:
97 query.filter_active()
99 query.order_by_date()
101 return UseCaseBrowseResult(
102 count=await query.count(),
103 iterator=self._repo.get_all(query, command.limit, command.offset),
104 )