Coverage for src/kwai/api/v1/club/endpoints/members.py: 100%

31 statements  

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

1"""Module for defining the endpoints for members of the club API.""" 

2 

3from fastapi import APIRouter, Depends, HTTPException, Query, status 

4from pydantic import BaseModel, Field 

5 

6from kwai.api.dependencies import create_database, get_current_user 

7from kwai.api.v1.club.presenters import JsonApiMemberPresenter, JsonApiMembersPresenter 

8from kwai.api.v1.club.schemas.member import MemberDocument 

9from kwai.core.json_api import PaginationModel 

10from kwai.modules.club.get_member import GetMember, GetMemberCommand 

11from kwai.modules.club.get_members import GetMembers, GetMembersCommand 

12from kwai.modules.club.repositories.member_db_repository import MemberDbRepository 

13from kwai.modules.club.repositories.member_repository import MemberNotFoundException 

14from kwai.modules.identity.users.user import UserEntity 

15 

16 

17router = APIRouter() 

18 

19 

20class MembersFilterModel(BaseModel): 

21 """Define the JSON:API filter for members.""" 

22 

23 enabled: bool = Field(Query(default=True, alias="filter[enabled]")) 

24 license_end_month: int = Field(Query(default=0, alias="filter[license_end_month]")) 

25 license_end_year: int = Field(Query(default=0, alias="filter[license_end_year]")) 

26 

27 

28@router.get("/members") 

29async def get_members( 

30 pagination: PaginationModel = Depends(PaginationModel), 

31 members_filter: MembersFilterModel = Depends(MembersFilterModel), 

32 db=Depends(create_database), 

33 user: UserEntity = Depends(get_current_user), 

34) -> MemberDocument: 

35 """Get members.""" 

36 command = GetMembersCommand( 

37 offset=pagination.offset or 0, 

38 limit=pagination.limit or 0, 

39 active=members_filter.enabled, 

40 license_end_year=members_filter.license_end_year, 

41 license_end_month=members_filter.license_end_month, 

42 ) 

43 presenter = JsonApiMembersPresenter() 

44 await GetMembers(MemberDbRepository(db), presenter).execute(command) 

45 

46 return presenter.get_document() 

47 

48 

49@router.get("/members/{uuid}") 

50async def get_member( 

51 uuid: str, 

52 db=Depends(create_database), 

53 user: UserEntity = Depends(get_current_user), 

54) -> MemberDocument: 

55 """Get a member with the given unique id.""" 

56 command = GetMemberCommand(uuid=uuid) 

57 

58 presenter = JsonApiMemberPresenter() 

59 try: 

60 await GetMember(MemberDbRepository(db), presenter).execute(command) 

61 except MemberNotFoundException as ex: 

62 raise HTTPException( 

63 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex) 

64 ) from ex 

65 

66 return presenter.get_document()