Coverage for bc/kwai-bc-training/src/kwai_bc_training/get_coaches.py: 100%

17 statements  

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

1"""Module for defining the use case 'get coaches'.""" 

2 

3from dataclasses import dataclass 

4 

5from kwai_core.domain.presenter import AsyncPresenter, IterableResult 

6 

7from kwai_bc_training.coaches.coach import CoachEntity 

8from kwai_bc_training.coaches.coach_repository import CoachRepository 

9 

10 

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

12class GetCoachesCommand: 

13 """Input for the use case GetCoaches.""" 

14 

15 active: bool 

16 limit: int = 0 

17 offset: int = 0 

18 

19 

20class GetCoaches: 

21 """Use case for getting coaches.""" 

22 

23 def __init__( 

24 self, 

25 coach_repo: CoachRepository, 

26 presenter: AsyncPresenter[IterableResult[CoachEntity]], 

27 ): 

28 """Initialize the use case. 

29 

30 Args: 

31 coach_repo: The repository for getting the coaches. 

32 presenter: A presenter for coaches. 

33 """ 

34 self._coach_repo = coach_repo 

35 self._presenter = presenter 

36 

37 async def execute(self, command: GetCoachesCommand) -> None: 

38 """Execute the use case.""" 

39 coach_query = self._coach_repo.create_query() 

40 

41 if command.active: 

42 coach_query.filter_by_active() 

43 

44 await self._presenter.present( 

45 IterableResult[CoachEntity]( 

46 count=await coach_query.count(), 

47 limit=command.limit or 0, 

48 offset=command.offset or 0, 

49 iterator=self._coach_repo.get_all(coach_query), 

50 ) 

51 )