Coverage for apps/kwai-api/src/kwai_api/v1/portal/coaches/presenters.py: 64%
11 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 presenters for creating JSON:API resources for one or more coaches."""
3from typing import Self
5from kwai_bc_club.domain.club_coach import ClubCoachEntity
6from kwai_core.domain.presenter import AsyncPresenter, IterableResult
7from kwai_core.json_api import JsonApiPresenter, Meta
9from kwai_api.v1.portal.coaches.schemas import (
10 CoachAttributes,
11 CoachesDocument,
12 CoachResource,
13)
16class JsonApiCoachesPresenter(
17 JsonApiPresenter[CoachesDocument], AsyncPresenter[IterableResult[ClubCoachEntity]]
18):
19 """A presenter that transforms an iterable of coaches into a JSON:API document."""
21 async def present(self, use_case_result: IterableResult[ClubCoachEntity]) -> Self:
22 self._document = CoachesDocument(
23 meta=Meta(
24 count=use_case_result.count,
25 offset=use_case_result.offset,
26 limit=use_case_result.limit,
27 )
28 )
29 async for coach in use_case_result:
30 self._document.data.append(
31 CoachResource(
32 id=str(coach.uuid),
33 attributes=CoachAttributes(
34 name=str(coach.name),
35 description=coach.description,
36 diploma=coach.diploma,
37 ),
38 )
39 )
40 return self