Coverage for src/kwai/modules/training/update_training_definition.py: 96%
26 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 "Update Training Definition"."""
3from dataclasses import dataclass
5from kwai.core.domain.entity import Entity
6from kwai.core.domain.value_objects.owner import Owner
7from kwai.core.domain.value_objects.time_period import TimePeriod
8from kwai.core.domain.value_objects.weekday import Weekday
9from kwai.modules.training.teams.team import TeamIdentifier
10from kwai.modules.training.teams.team_repository import TeamRepository
11from kwai.modules.training.training_definition_command import TrainingDefinitionCommand
12from kwai.modules.training.trainings.training_definition import (
13 TrainingDefinitionEntity,
14 TrainingDefinitionIdentifier,
15)
16from kwai.modules.training.trainings.training_definition_repository import (
17 TrainingDefinitionRepository,
18)
21@dataclass(kw_only=True, frozen=True, slots=True)
22class UpdateTrainingDefinitionCommand(TrainingDefinitionCommand):
23 """Input for the "Update Training Definition" use case."""
25 id: int
28class UpdateTrainingDefinition:
29 """Use case for updating a training definition."""
31 def __init__(
32 self,
33 repo: TrainingDefinitionRepository,
34 team_repo: TeamRepository,
35 owner: Owner,
36 ):
37 """Initialize the use case.
39 Args:
40 repo: The repository used to update the training definition.
41 team_repo: A repository for getting the team.
42 owner: The user that executes this use case.
43 """
44 self._repo = repo
45 self._team_repo = team_repo
46 self._owner = owner
48 async def execute(
49 self, command: UpdateTrainingDefinitionCommand
50 ) -> TrainingDefinitionEntity:
51 """Execute the use case.
53 Args:
54 command: The input for this use case.
56 Raises:
57 TrainingDefinitionNotFoundException: when the training definition does not
58 exist.
59 """
60 if command.team_id is not None:
61 team = await self._team_repo.get_by_id(TeamIdentifier(command.team_id))
62 else:
63 team = None
64 training_definition = await self._repo.get_by_id(
65 TrainingDefinitionIdentifier(command.id)
66 )
67 training_definition = Entity.replace(
68 training_definition,
69 name=command.name,
70 description=command.description,
71 weekday=Weekday(command.weekday),
72 period=TimePeriod.create_from_string(
73 start=command.start_time,
74 end=command.end_time,
75 timezone=command.timezone,
76 ),
77 active=command.active,
78 location=command.location,
79 remark=command.remark,
80 team=team,
81 owner=self._owner,
82 )
83 await self._repo.update(training_definition)
85 return training_definition