Coverage for apps/kwai-api/src/kwai_api/v1/trainings/coaches/endpoints.py: 84%
58 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 for defining endpoints for coaches."""
3from typing import Annotated
5from fastapi import APIRouter, Depends, HTTPException, status
6from kwai_bc_identity.users.user import UserEntity
7from kwai_bc_training.add_coach_to_training import (
8 AddCoachToTraining,
9 AddCoachToTrainingCommand,
10)
11from kwai_bc_training.coaches.coach_db_repository import CoachDbRepository
12from kwai_bc_training.coaches.coach_repository import CoachNotFoundException
13from kwai_bc_training.delete_coach_from_training import (
14 DeleteCoachFromTraining,
15 DeleteCoachFromTrainingCommand,
16)
17from kwai_bc_training.get_coaches import GetCoaches, GetCoachesCommand
18from kwai_bc_training.get_training import GetTraining, GetTrainingCommand
19from kwai_bc_training.trainings.training_db_repository import TrainingDbRepository
20from kwai_bc_training.trainings.training_repository import TrainingNotFoundException
21from kwai_core.db.database import Database
22from kwai_core.db.uow import UnitOfWork
23from kwai_core.domain.value_objects.owner import Owner
25from kwai_api.dependencies import create_database, get_current_user
26from kwai_api.v1.trainings.coaches.presenters import (
27 JsonApiCoachesPresenter,
28 JsonApiTrainingCoachesPresenter,
29)
30from kwai_api.v1.trainings.coaches.schemas import (
31 CoachesDocument,
32 CreateTrainingCoachDocument,
33 TrainingCoachesDocument,
34)
35from kwai_api.v1.trainings.presenters import JsonApiTrainingPresenter
36from kwai_api.v1.trainings.schemas import TrainingDocument
39router = APIRouter()
42@router.get("/trainings/coaches")
43async def get_coaches(
44 database: Annotated[Database, Depends(create_database)],
45) -> CoachesDocument:
46 """Get coaches."""
47 presenter = JsonApiCoachesPresenter()
48 await GetCoaches(CoachDbRepository(database), presenter).execute(
49 GetCoachesCommand(active=True)
50 )
52 return presenter.get_document()
55@router.get(
56 "/trainings/{training_id}/coaches",
57 responses={status.HTTP_404_NOT_FOUND: {"description": "Training was not found"}},
58)
59async def get_training_coaches(
60 training_id: int, database: Annotated[Database, Depends(create_database)]
61) -> TrainingCoachesDocument:
62 """Get the coaches of a training."""
63 command = GetTrainingCommand(id=training_id)
64 presenter = JsonApiTrainingCoachesPresenter()
65 try:
66 await GetTraining(TrainingDbRepository(database), presenter).execute(command)
67 except TrainingNotFoundException as ex:
68 raise HTTPException(
69 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
70 ) from ex
72 return presenter.get_document()
75@router.post(
76 "/trainings/{training_id}/coaches",
77 responses={
78 status.HTTP_404_NOT_FOUND: {"description": "Training was not found"},
79 },
80 status_code=status.HTTP_201_CREATED,
81)
82async def create_training_coach(
83 training_id: int,
84 database: Annotated[Database, Depends(create_database)],
85 resource: CreateTrainingCoachDocument,
86 user: Annotated[UserEntity, Depends(get_current_user)],
87) -> TrainingDocument:
88 """Add a coach to the training."""
89 if resource.data.id is None:
90 raise HTTPException(
91 status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
92 detail="Id is missing for the coach relationship",
93 )
95 command = AddCoachToTrainingCommand(
96 training_id=training_id,
97 coach_id=int(resource.data.id),
98 present=resource.data.attributes.present,
99 payed=resource.data.attributes.payed,
100 head=resource.data.attributes.head,
101 )
102 presenter = JsonApiTrainingPresenter()
103 try:
104 async with UnitOfWork(database):
105 await AddCoachToTraining(
106 TrainingDbRepository(database),
107 CoachDbRepository(database),
108 Owner(id=user.id, uuid=user.uuid, name=user.name),
109 presenter,
110 ).execute(command)
111 except TrainingNotFoundException as ex:
112 raise HTTPException(
113 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
114 ) from ex
115 except CoachNotFoundException as ex:
116 raise HTTPException(
117 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
118 ) from ex
120 return presenter.get_document()
123@router.delete(
124 "/trainings/{training_id}/coaches/{coach_id}",
125 responses={status.HTTP_404_NOT_FOUND: {"description": "Training was not found."}},
126 status_code=status.HTTP_200_OK,
127)
128async def delete_coach_from_training(
129 training_id: int,
130 coach_id: int,
131 database: Annotated[Database, Depends(create_database)],
132 user: Annotated[UserEntity, Depends(get_current_user)],
133) -> TrainingDocument:
134 """Remove a coach from a training."""
135 command = DeleteCoachFromTrainingCommand(training_id=training_id, coach_id=coach_id)
136 presenter = JsonApiTrainingPresenter()
137 try:
138 async with UnitOfWork(database):
139 await DeleteCoachFromTraining(
140 TrainingDbRepository(database), presenter
141 ).execute(command)
142 except TrainingNotFoundException as ex:
143 raise HTTPException(
144 status_code=status.HTTP_404_NOT_FOUND, detail=str(ex)
145 ) from ex
147 return presenter.get_document()