Coverage for apps/kwai-api/src/kwai_api/v1/club/coaches/presenters.py: 100%

21 statements  

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

1"""Module that defines presenters for the coaches endpoint of the club API.""" 

2 

3from typing import Self 

4 

5from kwai_bc_club.domain.club_coach import ClubCoachEntity 

6from kwai_core.domain.presenter import AsyncPresenter, IterableResult, Presenter 

7from kwai_core.json_api import JsonApiPresenter, Meta, Relationship, ResourceMeta 

8 

9from kwai_api.v1.club.coaches.schemas import ( 

10 CoachAttributes, 

11 CoachDocument, 

12 CoachesDocument, 

13 CoachRelationships, 

14 CoachResource, 

15 UserAttributes, 

16 UserResource, 

17) 

18from kwai_api.v1.club.schemas.resources import ( 

19 MemberResourceIdentifier, 

20 UserResourceIdentifier, 

21) 

22 

23 

24class JsonApiCoachPresenter( 

25 JsonApiPresenter[CoachDocument], Presenter[ClubCoachEntity] 

26): 

27 """A presenter that transforms a club coach entity into a JSON:API document.""" 

28 

29 def present(self, club_coach: ClubCoachEntity) -> Self: 

30 if club_coach.member.user is None: 

31 included = set() 

32 else: 

33 included = { 

34 UserResource( 

35 id=str(club_coach.member.user.uuid), 

36 attributes=UserAttributes(name=str(club_coach.member.user.name)), 

37 ) 

38 } 

39 

40 self._document = CoachDocument( 

41 data=CoachResource( 

42 meta=ResourceMeta( 

43 created_at=str(club_coach.traceable_time.created_at), 

44 updated_at=str(club_coach.traceable_time.updated_at), 

45 ), 

46 id=str(club_coach.uuid), 

47 attributes=CoachAttributes( 

48 diploma=club_coach.diploma, 

49 description=club_coach.description, 

50 name=str(club_coach.name), 

51 head=club_coach.head, 

52 active=club_coach.active, 

53 remark=club_coach.remark, 

54 license=club_coach.member.license.number, 

55 license_end_date=str(club_coach.member.license.end_date), 

56 birthdate=str(club_coach.member.person.birthdate), 

57 ), 

58 relationships=CoachRelationships( 

59 user=Relationship[UserResourceIdentifier]( 

60 data=None 

61 if club_coach.member.user is None 

62 else UserResourceIdentifier(id=str(club_coach.member.user.uuid)) 

63 ), 

64 member=Relationship[MemberResourceIdentifier]( 

65 data=MemberResourceIdentifier(id=str(club_coach.member.uuid)) 

66 ), 

67 ), 

68 ), 

69 included=included, 

70 ) 

71 return self 

72 

73 

74class JsonApiCoachesPresenter( 

75 JsonApiPresenter[CoachesDocument], 

76 AsyncPresenter[IterableResult[ClubCoachEntity]], 

77): 

78 """A presenter for transforming an iterator with coaches into a JSON:API document.""" 

79 

80 async def present(self, use_case_result: IterableResult[ClubCoachEntity]) -> Self: 

81 self._document = CoachesDocument( 

82 meta=Meta( 

83 count=use_case_result.count, 

84 offset=use_case_result.offset, 

85 limit=use_case_result.limit, 

86 ) 

87 ) 

88 async for coach in use_case_result: 

89 coach_document = JsonApiCoachPresenter().present(coach).get_document() 

90 self._document.data.append(coach_document.data) 

91 self._document.included |= coach_document.included 

92 

93 return self