Coverage for bc/kwai-bc-training/src/kwai_bc_training/update_training.py: 95%
40 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 defining the use case "Update Training"."""
3from dataclasses import dataclass, replace
5from kwai_core.domain.presenter import Presenter
6from kwai_core.domain.value_objects.owner import Owner
7from kwai_core.domain.value_objects.period import Period
8from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
9from kwai_core.domain.value_objects.timestamp import Timestamp
10from kwai_core.domain.value_objects.unique_id import UniqueId
12from kwai_bc_training.coaches.coach_repository import CoachRepository
13from kwai_bc_training.teams.team import TeamIdentifier
14from kwai_bc_training.teams.team_repository import TeamRepository
15from kwai_bc_training.training_command import TrainingCommand
16from kwai_bc_training.trainings.training import (
17 TrainingCoachEntity,
18 TrainingEntity,
19 TrainingIdentifier,
20)
21from kwai_bc_training.trainings.training_repository import TrainingRepository
22from kwai_bc_training.trainings.training_schedule import (
23 TrainingScheduleIdentifier,
24)
25from kwai_bc_training.trainings.training_schedule_repository import (
26 TrainingScheduleRepository,
27)
30@dataclass(kw_only=True, frozen=True, slots=True)
31class UpdateTrainingCommand(TrainingCommand):
32 """Input for the "Update Training" use case."""
34 id: int
37class UpdateTraining:
38 """Use case for updating a training."""
40 def __init__(
41 self,
42 repo: TrainingRepository,
43 schema_repo: TrainingScheduleRepository,
44 coach_repo: CoachRepository,
45 team_repo: TeamRepository,
46 owner: Owner,
47 presenter: Presenter[TrainingEntity],
48 ):
49 """Initialize the use case.
51 Args:
52 repo: The repository used to create the training.
53 schema_repo: The repository for getting the training schema.
54 coach_repo: The repository for getting the coaches.
55 team_repo: The repository for getting the teams.
56 owner: The user that executes this use case.
57 presenter: A presenter for a training.
58 """
59 self._repo = repo
60 self._schema_repo = schema_repo
61 self._coach_repo = coach_repo
62 self._team_repo = team_repo
63 self._owner = owner
64 self._presenter = presenter
66 async def execute(self, command: UpdateTrainingCommand) -> None:
67 """Executes the use case.
69 Args:
70 command: The input for this use case.
72 Raises:
73 TrainingSchemaNotFoundException: Raised when a training schema cannot be found.
74 """
75 training = await self._repo.get_by_id(TrainingIdentifier(command.id))
77 if command.schedule:
78 schedule = await self._schema_repo.get_by_id(
79 TrainingScheduleIdentifier(command.schedule)
80 )
81 else:
82 schedule = None
84 if command.teams:
85 teams = frozenset(
86 [
87 entity
88 async for entity in self._team_repo.get_by_ids(
89 *[TeamIdentifier(team_id) for team_id in command.teams]
90 )
91 ]
92 )
93 else:
94 teams = frozenset({})
96 if command.coaches:
97 coach_query = self._coach_repo.create_query().filter_by_uuids(
98 *[UniqueId.create_from_string(coach.uuid) for coach in command.coaches]
99 )
100 coaches = frozenset(
101 [
102 TrainingCoachEntity(coach=coach, owner=self._owner)
103 async for coach in self._coach_repo.get_all(coach_query)
104 ]
105 )
106 else:
107 coaches = frozenset({})
109 training = replace(
110 training,
111 texts=tuple(
112 [
113 LocaleText(
114 locale=Locale(text.locale),
115 format=DocumentFormat(text.format),
116 title=text.title,
117 content=text.content,
118 summary=text.summary,
119 author=self._owner,
120 )
121 for text in command.texts
122 ]
123 ),
124 schedule=schedule,
125 coaches=coaches,
126 teams=teams,
127 period=Period(
128 start_date=Timestamp.create_from_string(command.start_date),
129 end_date=Timestamp.create_from_string(command.end_date),
130 ),
131 active=command.active,
132 cancelled=command.cancelled,
133 location=command.location,
134 remark=command.remark,
135 )
137 await self._repo.update(training)
138 self._presenter.present(training)