Coverage for src/kwai/modules/training/create_training_definition.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module for the use case "Create Training Definition"."""
3from kwai.core.domain.value_objects.owner import Owner
4from kwai.core.domain.value_objects.time_period import TimePeriod
5from kwai.core.domain.value_objects.weekday import Weekday
6from kwai.modules.training.teams.team import TeamIdentifier
7from kwai.modules.training.teams.team_repository import TeamRepository
8from kwai.modules.training.training_definition_command import TrainingDefinitionCommand
9from kwai.modules.training.trainings.training_definition import TrainingDefinitionEntity
10from kwai.modules.training.trainings.training_definition_repository import (
11 TrainingDefinitionRepository,
12)
15CreateTrainingDefinitionCommand = TrainingDefinitionCommand
18class CreateTrainingDefinition:
19 """Use case for creating a training definition."""
21 def __init__(
22 self,
23 repo: TrainingDefinitionRepository,
24 team_repo: TeamRepository,
25 owner: Owner,
26 ):
27 """Initialize the use case.
29 Args:
30 repo: The repository used to create the training definition.
31 team_repo: The repository for getting the team.
32 owner: The user that executes this use case.
33 """
34 self._repo = repo
35 self._team_repo = team_repo
36 self._owner = owner
38 async def execute(
39 self, command: CreateTrainingDefinitionCommand
40 ) -> TrainingDefinitionEntity:
41 """Execute the use case.
43 Args:
44 command: The input for this use case.
45 """
46 if command.team_id is not None:
47 team = await self._team_repo.get_by_id(TeamIdentifier(command.team_id))
48 else:
49 team = None
51 training_definition = TrainingDefinitionEntity(
52 name=command.name,
53 description=command.description,
54 weekday=Weekday(command.weekday),
55 period=TimePeriod.create_from_string(
56 start=command.start_time,
57 end=command.end_time,
58 timezone=command.timezone,
59 ),
60 active=command.active,
61 location=command.location,
62 remark=command.remark,
63 owner=self._owner,
64 team=team,
65 )
66 return await self._repo.create(training_definition)