Coverage for bc/kwai-bc-training/src/kwai_bc_training/get_trainings.py: 100%
43 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 for the use case get trainings."""
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
9from kwai_bc_training.coaches.coach import CoachIdentifier
10from kwai_bc_training.coaches.coach_repository import CoachRepository
11from kwai_bc_training.trainings.training import TrainingEntity
12from kwai_bc_training.trainings.training_repository import TrainingRepository
13from kwai_bc_training.trainings.training_schedule import (
14 TrainingScheduleIdentifier,
15)
16from kwai_bc_training.trainings.training_schedule_repository import (
17 TrainingScheduleRepository,
18)
21@dataclass(kw_only=True, frozen=True, slots=True)
22class GetTrainingsCommand:
23 """Input for the get trainings use case.
25 Attributes:
26 limit: the max. number of elements to return. Default is None, which means all.
27 offset: Offset to use. Default is None.
28 year: Only return trainings of this year.
29 month: Only return trainings of this month.
30 start: Only return trainings starting from this date.
31 end: Only return trainings before this date.
32 coach: Only return trainings with this coach.
33 schedule: Only return trainings created from this training schedule.
34 active: Only return trainings that are active (default is True).
35 """
37 limit: int | None = None
38 offset: int | None = None
39 year: int | None = None
40 month: int | None = None
41 start: datetime | None = None
42 end: datetime | None = None
43 coach: int | None = None
44 schedule: int | None = None
45 active: bool = True
48class GetTrainings:
49 """Use case to get trainings."""
51 def __init__(
52 self,
53 repo: TrainingRepository,
54 coach_repo: CoachRepository,
55 training_schedule_repo: TrainingScheduleRepository,
56 presenter: AsyncPresenter,
57 ):
58 """Initialize use case.
60 Attributes:
61 repo: The repository for trainings.
62 coach_repo: The repository for coaches.
63 training_schedule_repo: The repository for training schedules.
64 presenter: A presenter for a training entity.
65 """
66 self._repo = repo
67 self._coach_repo = coach_repo
68 self._training_schedule_repo = training_schedule_repo
69 self._presenter = presenter
71 async def execute(self, command: GetTrainingsCommand) -> None:
72 """Execute the use case.
74 Args:
75 command: The input for this use case.
77 Raises:
78 CoachNotFoundException: Raised when a coach is not found.
79 TrainingSchemaNotFoundException: Raised when a training schedule is not found.
81 Returns:
82 A tuple with the number of entities and an iterator for training entities.
83 """
84 query = self._repo.create_query().order_by_date()
86 if command.year:
87 query.filter_by_year_month(command.year, command.month)
89 if command.start and command.end:
90 query.filter_by_dates(
91 Timestamp(timestamp=command.start), Timestamp(timestamp=command.end)
92 )
94 if command.coach:
95 coach = await self._coach_repo.get_by_id(CoachIdentifier(command.coach))
96 query.filter_by_coach(coach)
98 if command.schedule:
99 training_schedule = await self._training_schedule_repo.get_by_id(
100 TrainingScheduleIdentifier(command.schedule)
101 )
102 query.filter_by_training_schedule(training_schedule)
104 if command.active:
105 query.filter_active()
107 query.order_by_date()
109 await self._presenter.present(
110 IterableResult[TrainingEntity](
111 count=await query.count(),
112 limit=command.limit or 0,
113 offset=command.offset or 0,
114 iterator=self._repo.get_all(query, command.limit, command.offset),
115 )
116 )