Coverage for bc/kwai-bc-training/src/kwai_bc_training/create_training.py: 100%
36 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"."""
3from kwai_core.domain.presenter import Presenter
4from kwai_core.domain.value_objects.owner import Owner
5from kwai_core.domain.value_objects.period import Period
6from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
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.teams.team import TeamIdentifier
12from kwai_bc_training.teams.team_repository import TeamRepository
13from kwai_bc_training.training_command import TrainingCommand
14from kwai_bc_training.trainings.training import TrainingEntity
15from kwai_bc_training.trainings.training_repository import TrainingRepository
16from kwai_bc_training.trainings.training_schedule import (
17 TrainingScheduleIdentifier,
18)
19from kwai_bc_training.trainings.training_schedule_repository import (
20 TrainingScheduleRepository,
21)
22from kwai_bc_training.trainings.value_objects import TrainingCoach
25CreateTrainingCommand = TrainingCommand
28class CreateTraining:
29 """Use case for creating a training."""
31 def __init__(
32 self,
33 repo: TrainingRepository,
34 schema_repo: TrainingScheduleRepository,
35 coach_repo: CoachRepository,
36 team_repo: TeamRepository,
37 owner: Owner,
38 presenter: Presenter[TrainingEntity],
39 ):
40 """Initialize the use case.
42 Args:
43 repo: The repository used to create the training.
44 schema_repo: The repository for getting the training schedule.
45 coach_repo: The repository for getting the coaches.
46 team_repo: The repository for getting the teams.
47 owner: The user that executes this use case.
48 presenter: A presenter for a training.
49 """
50 self._repo = repo
51 self._schema_repo = schema_repo
52 self._coach_repo = coach_repo
53 self._team_repo = team_repo
54 self._owner = owner
55 self._presenter = presenter
57 async def execute(self, command: CreateTrainingCommand) -> None:
58 """Execute the use case.
60 Args:
61 command: The input for this use case.
63 Raises:
64 TrainingSchemaNotFoundException: Raised when a training schedule cannot be found.
65 """
66 if command.schedule:
67 schedule = await self._schema_repo.get_by_id(
68 TrainingScheduleIdentifier(command.schedule)
69 )
70 else:
71 schedule = None
73 if command.teams:
74 teams = frozenset(
75 [
76 entity
77 async for entity in self._team_repo.get_by_ids(
78 *[TeamIdentifier(team_id) for team_id in command.teams]
79 )
80 ]
81 )
82 else:
83 teams = frozenset({})
85 if command.coaches:
86 coaches = frozenset(
87 [
88 TrainingCoach(coach=coach, owner=self._owner)
89 async for coach in self._coach_repo.get_by_ids(
90 *[CoachIdentifier(coach.id) for coach in command.coaches]
91 )
92 ]
93 )
94 else:
95 coaches = frozenset({})
97 training = TrainingEntity(
98 texts=tuple(
99 [
100 LocaleText(
101 locale=Locale(text.locale),
102 format=DocumentFormat(text.format),
103 title=text.title,
104 content=text.content,
105 summary=text.summary,
106 author=self._owner,
107 )
108 for text in command.texts
109 ]
110 ),
111 schedule=schedule,
112 coaches=coaches,
113 teams=teams,
114 period=Period(
115 start_date=Timestamp.create_from_string(command.start_date),
116 end_date=Timestamp.create_from_string(command.end_date),
117 ),
118 active=command.active,
119 cancelled=command.cancelled,
120 location=command.location,
121 remark=command.remark,
122 )
124 self._presenter.present(await self._repo.create(training))