Coverage for bc/kwai-bc-training/src/kwai_bc_training/delete_coach_from_training.py: 100%
17 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 that defines the use case for deleting a coach from a training."""
3from dataclasses import dataclass
5from kwai_core.domain.presenter import Presenter
7from kwai_bc_training.trainings.training import TrainingEntity, TrainingIdentifier
8from kwai_bc_training.trainings.training_repository import TrainingRepository
11@dataclass
12class DeleteCoachFromTrainingCommand:
13 """Input for the use case."""
15 training_id: int
16 coach_id: int
19class DeleteCoachFromTraining:
20 """Use case for deleting a coach from a training."""
22 def __init__(
23 self,
24 training_repo: TrainingRepository,
25 presenter: Presenter[TrainingEntity],
26 ):
27 """Initialize the use case.
29 Attributes:
30 training_repo (TrainingRepository): the training repository
31 presenter (Presenter[TrainingEntity]): A presenter for a training
32 """
33 self._training_repo = training_repo
34 self._presenter = presenter
36 async def execute(self, command: DeleteCoachFromTrainingCommand):
37 """Execute the use case.
39 Args:
40 command: the input for this use case.
42 Raises:
43 TrainingNotFoundException: Raised when the training with the given id does not exist.
44 """
45 training = await self._training_repo.get_by_id(
46 TrainingIdentifier(command.training_id)
47 )
48 training_coaches = list(
49 filter(
50 lambda coach: coach.coach.id.value == command.coach_id, training.coaches
51 )
52 )
53 if len(training_coaches) > 0:
54 training = training.delete_coach(training_coaches[0])
55 await self._training_repo.update(training)
57 self._presenter.present(training)