Coverage for bc/kwai-bc-club/src/kwai_bc_club/get_members.py: 97%
29 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 'Get Members'."""
3from dataclasses import dataclass
5from kwai_core.domain.presenter import AsyncPresenter, IterableResult
6from kwai_core.domain.value_objects.date import Date
8from kwai_bc_club.domain.member import MemberEntity
9from kwai_bc_club.repositories.member_repository import MemberRepository
12@dataclass(kw_only=True, frozen=True, slots=True)
13class GetMembersCommand:
14 """Input for the get members use case.
16 Attributes:
17 limit: the max. number of elements to return. Default is None, which means all.
18 offset: Offset to use. Default is None.
19 active: When true (the default), only return the active members.
20 license_end_month: Only return members with a license ending in the given month.
21 license_end_year: Only return members with a license ending in the given year.
22 name: Filter on the part of the name of the member.
23 is_coach: Filter for coaches or not.
24 """
26 limit: int = 0
27 offset: int = 0
28 active: bool = True
29 license_end_month: int = 0
30 license_end_year: int = 0
31 name: str = ""
32 is_coach: bool | None = None
35class GetMembers:
36 """Use case get members."""
38 def __init__(
39 self,
40 repo: MemberRepository,
41 presenter: AsyncPresenter[IterableResult[MemberEntity]],
42 ):
43 """Initialize use case.
45 Args:
46 repo: The repository for members.
47 presenter: The presenter for members.
48 """
49 self._repo = repo
50 self._presenter = presenter
52 async def execute(self, command: GetMembersCommand):
53 """Execute the use case.
55 Args:
56 command: the input for this use case.
57 """
58 query = self._repo.create_query()
60 if command.active:
61 query = query.filter_by_active()
63 if command.license_end_month != 0:
64 query = query.filter_by_license_date(
65 command.license_end_month, command.license_end_year or Date.today().year
66 )
68 if command.name:
69 query = query.filter_like_name(command.name)
71 if command.is_coach is not None:
72 query = (
73 query.filter_is_coach()
74 if command.is_coach
75 else query.filter_is_not_coach()
76 )
78 await self._presenter.present(
79 IterableResult(
80 count=await query.count(),
81 limit=command.limit,
82 offset=command.offset,
83 iterator=self._repo.get_all(query, command.limit, command.offset),
84 )
85 )