Coverage for kwai/modules/training/create_training.py: 97%
34 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Module for the use case "Create training"."""
3from kwai.core.domain.value_objects.local_timestamp import LocalTimestamp
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 LocaleText
7from kwai.modules.training.coaches.coach import CoachIdentifier
8from kwai.modules.training.coaches.coach_repository import CoachRepository
9from kwai.modules.training.teams.team import TeamIdentifier
10from kwai.modules.training.teams.team_repository import TeamRepository
11from kwai.modules.training.training_command import TrainingCommand
12from kwai.modules.training.trainings.training import TrainingEntity
13from kwai.modules.training.trainings.training_definition import (
14 TrainingDefinitionIdentifier,
15)
16from kwai.modules.training.trainings.training_definition_repository import (
17 TrainingDefinitionRepository,
18)
19from kwai.modules.training.trainings.training_repository import TrainingRepository
20from kwai.modules.training.trainings.value_objects import TrainingCoach
22CreateTrainingCommand = TrainingCommand
25class CreateTraining:
26 """Use case for creating a training."""
28 def __init__(
29 self,
30 repo: TrainingRepository,
31 definition_repo: TrainingDefinitionRepository,
32 coach_repo: CoachRepository,
33 team_repo: TeamRepository,
34 owner: Owner,
35 ):
36 """Initialize the use case.
38 Args:
39 repo: The repository used to create the training.
40 definition_repo: The repository for getting the training definition.
41 coach_repo: The repository for getting the coaches.
42 team_repo: The repository for getting the teams.
43 owner: The user that executes this use case.
44 """
45 self._repo = repo
46 self._definition_repo = definition_repo
47 self._coach_repo = coach_repo
48 self._team_repo = team_repo
49 self._owner = owner
51 async def execute(self, command: CreateTrainingCommand) -> TrainingEntity:
52 """Executes the use case.
54 Args:
55 command: The input for this use case.
57 Raises:
58 TrainingDefinitionNotFoundException: Raised when a training definition
59 cannot be found.
60 """
61 if command.definition:
62 definition = self._definition_repo.get_by_id(
63 TrainingDefinitionIdentifier(command.definition)
64 )
65 else:
66 definition = None
68 if command.teams:
69 teams = [
70 entity
71 async for entity in self._team_repo.get_by_ids(
72 *[TeamIdentifier(team_id) for team_id in command.teams]
73 )
74 ]
75 else:
76 teams = []
78 if command.coaches:
79 coaches = [
80 TrainingCoach(coach=coach, owner=self._owner)
81 async for coach in self._coach_repo.get_by_ids(
82 *[CoachIdentifier(coach.id) for coach in command.coaches]
83 )
84 ]
85 else:
86 coaches = []
88 training = TrainingEntity(
89 content=[
90 LocaleText(
91 locale=text["locale"],
92 format=text["format"],
93 title=text["title"],
94 content=text["content"],
95 summary=text["summary"],
96 author=self._owner,
97 )
98 for text in command.text
99 ],
100 definition=definition,
101 coaches=coaches,
102 teams=teams,
103 period=Period(
104 start_date=LocalTimestamp.create_from_string(command.start_date),
105 end_date=LocalTimestamp.create_from_string(command.end_date),
106 ),
107 active=command.active,
108 cancelled=command.cancelled,
109 location=command.location,
110 remark=command.remark,
111 )
113 return await self._repo.create(training)