Coverage for bc/kwai-bc-training/src/kwai_bc_training/create_training_schedule.py: 100%
29 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 "Create Training Schedule"."""
3from kwai_core.domain.presenter import Presenter
4from kwai_core.domain.value_objects.owner import Owner
5from kwai_core.domain.value_objects.time_period import TimePeriod
6from kwai_core.domain.value_objects.unique_id import UniqueId
7from kwai_core.domain.value_objects.weekday import Weekday
9from kwai_bc_training.coaches.coach_repository import CoachRepository
10from kwai_bc_training.teams.team import TeamIdentifier
11from kwai_bc_training.teams.team_repository import TeamRepository
12from kwai_bc_training.training_schedule_command import TrainingScheduleCommand
13from kwai_bc_training.trainings.training_schedule import TrainingScheduleEntity
14from kwai_bc_training.trainings.training_schedule_repository import (
15 TrainingScheduleRepository,
16)
19CreateTrainingScheduleCommand = TrainingScheduleCommand
22class CreateTrainingSchedule:
23 """Use case for creating a training schedule."""
25 def __init__(
26 self,
27 repo: TrainingScheduleRepository,
28 team_repo: TeamRepository,
29 coach_repo: CoachRepository,
30 owner: Owner,
31 presenter: Presenter[TrainingScheduleEntity],
32 ):
33 """Initialize the use case.
35 Args:
36 repo: The repository used to create the training schedule.
37 team_repo: The repository for getting the team.
38 coach_repo: The repository for getting the coaches.
39 owner: The user that executes this use case.
40 presenter: A presenter for presenting the training schedule.
41 """
42 self._repo = repo
43 self._team_repo = team_repo
44 self._coach_repo = coach_repo
45 self._owner = owner
46 self._presenter = presenter
48 async def execute(self, command: CreateTrainingScheduleCommand) -> None:
49 """Execute the use case.
51 Args:
52 command: The input for this use case.
53 """
54 if command.team_id is not None:
55 team = await self._team_repo.get_by_id(TeamIdentifier(command.team_id))
56 else:
57 team = None
59 if command.coaches:
60 coach_query = self._coach_repo.create_query().filter_by_uuids(
61 *[
62 UniqueId.create_from_string(coach_uuid)
63 for coach_uuid in command.coaches
64 ]
65 )
66 coaches = frozenset(
67 [coach async for coach in self._coach_repo.get_all(coach_query)]
68 )
69 else:
70 coaches = frozenset({})
72 training_schedule = TrainingScheduleEntity(
73 name=command.name,
74 description=command.description,
75 weekday=Weekday(command.weekday),
76 period=TimePeriod.create_from_string(
77 start=command.start_time,
78 end=command.end_time,
79 timezone=command.timezone,
80 ),
81 active=command.active,
82 location=command.location,
83 remark=command.remark or "",
84 owner=self._owner,
85 team=team,
86 coaches=coaches,
87 )
88 self._presenter.present(await self._repo.create(training_schedule))