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

17 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 

6from kwai_core.domain.value_objects.unique_id import UniqueId 

7 

8from kwai_bc_club.coach_command import CoachCommand 

9from kwai_bc_club.domain.club_coach import ClubCoachEntity 

10from kwai_bc_club.repositories.coach_repository import CoachRepository 

11 

12 

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

14class UpdateCoachCommand(CoachCommand): 

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

16 

17 Attributes: 

18 uuid: The unique id of the coach. 

19 """ 

20 

21 uuid: str 

22 

23 

24class UpdateCoach: 

25 """Update Coach use case.""" 

26 

27 def __init__( 

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

29 ): 

30 """Initialize the use case.""" 

31 self._coach_repo = coach_repo 

32 self._presenter = presenter 

33 

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

35 """Execute the use case. 

36 

37 Raises: 

38 CoachNotFoundException: Raised when a coach cannot be found. 

39 """ 

40 coach = await self._coach_repo.get( 

41 self._coach_repo.create_query().filter_by_uuid( 

42 UniqueId.create_from_string(command.uuid) 

43 ) 

44 ) 

45 

46 coach = replace( 

47 coach, 

48 diploma=command.diploma, 

49 description=command.description, 

50 remark=command.remark, 

51 active=command.active, 

52 ) 

53 

54 await self._coach_repo.update(coach) 

55 self._presenter.present(coach)