Coverage for bc/kwai-bc-club/src/kwai_bc_club/update_coach.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module that defines an 'Update Coach' use case.""" 

2 

3from dataclasses import dataclass, replace 

4 

5from kwai_core.domain.presenter import Presenter 

6 

7from kwai_bc_club.coach_command import CoachCommand 

8from kwai_bc_club.domain.club_coach import ClubCoachEntity, ClubCoachIdentifier 

9from kwai_bc_club.repositories.coach_repository import CoachRepository 

10 

11 

12@dataclass(kw_only=True, frozen=True, slots=True) 

13class UpdateCoachCommand(CoachCommand): 

14 """Input for the update Coach use case.""" 

15 

16 id: int 

17 

18 

19class UpdateCoach: 

20 """Update Coach use case.""" 

21 

22 def __init__( 

23 self, coach_repo: CoachRepository, presenter: Presenter[ClubCoachEntity] 

24 ): 

25 """Initialize the use case.""" 

26 self._coach_repo = coach_repo 

27 self._presenter = presenter 

28 

29 async def execute(self, command: UpdateCoachCommand) -> None: 

30 """Execute the use case. 

31 

32 Raises: 

33 CoachNotFoundException: Raised when a coach cannot be found. 

34 """ 

35 coach = await self._coach_repo.get_by_id(ClubCoachIdentifier(command.id)) 

36 

37 coach = replace( 

38 coach, 

39 diploma=command.diploma, 

40 description=command.description, 

41 remark=command.remark, 

42 active=command.active, 

43 ) 

44 

45 await self._coach_repo.update(coach) 

46 self._presenter.present(coach)