Coverage for apps/kwai-api/src/kwai_api/v1/trainings/coaches/endpoints.py: 76%

68 statements  

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

1"""Module for defining endpoints for coaches.""" 

2 

3from typing import Annotated 

4 

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_bc_training.update_coach_training import ( 

22 UpdateCoachTraining, 

23 UpdateCoachTrainingCommand, 

24) 

25from kwai_core.db.database import Database 

26from kwai_core.db.uow import UnitOfWork 

27from kwai_core.domain.value_objects.owner import Owner 

28 

29from kwai_api.dependencies import create_database, get_current_user 

30from kwai_api.v1.trainings.coaches.presenters import ( 

31 JsonApiCoachesPresenter, 

32 JsonApiTrainingCoachesPresenter, 

33) 

34from kwai_api.v1.trainings.coaches.schemas import ( 

35 CoachesDocument, 

36 TrainingCoachDocument, 

37 TrainingCoachesDocument, 

38) 

39from kwai_api.v1.trainings.presenters import JsonApiTrainingPresenter 

40from kwai_api.v1.trainings.schemas import TrainingDocument 

41from kwai_api.v1.trainings.security_dependencies import check_permission 

42 

43 

44router = APIRouter() 

45 

46 

47@router.get( 

48 "/trainings/coaches", 

49 dependencies=[Depends(check_permission)], 

50) 

51async def get_coaches( 

52 database: Annotated[Database, Depends(create_database)], 

53) -> CoachesDocument: 

54 """Get coaches.""" 

55 presenter = JsonApiCoachesPresenter() 

56 await GetCoaches(CoachDbRepository(database), presenter).execute( 

57 GetCoachesCommand(active=True) 

58 ) 

59 

60 return presenter.get_document() 

61 

62 

63@router.get( 

64 "/trainings/{training_id}/coaches", 

65 responses={status.HTTP_404_NOT_FOUND: {"description": "Training was not found"}}, 

66 dependencies=[Depends(check_permission)], 

67) 

68async def get_training_coaches( 

69 training_id: int, database: Annotated[Database, Depends(create_database)] 

70) -> TrainingCoachesDocument: 

71 """Get the coaches of a training.""" 

72 command = GetTrainingCommand(id=training_id) 

73 presenter = JsonApiTrainingCoachesPresenter() 

74 try: 

75 await GetTraining(TrainingDbRepository(database), presenter).execute(command) 

76 except TrainingNotFoundException as ex: 

77 raise HTTPException( 

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

79 ) from ex 

80 

81 return presenter.get_document() 

82 

83 

84@router.post( 

85 "/trainings/{training_id}/coaches", 

86 responses={ 

87 status.HTTP_404_NOT_FOUND: {"description": "Training was not found"}, 

88 }, 

89 status_code=status.HTTP_201_CREATED, 

90 dependencies=[Depends(check_permission)], 

91) 

92async def create_training_coach( 

93 training_id: int, 

94 database: Annotated[Database, Depends(create_database)], 

95 resource: TrainingCoachDocument, 

96 user: Annotated[UserEntity, Depends(get_current_user)], 

97) -> TrainingDocument: 

98 """Add a coach to the training.""" 

99 command = AddCoachToTrainingCommand( 

100 training_id=training_id, 

101 coach_uuid=resource.data.relationships.coach.data.id, 

102 present=resource.data.attributes.present, 

103 payed=resource.data.attributes.payed, 

104 head=resource.data.attributes.head, 

105 ) 

106 presenter = JsonApiTrainingPresenter() 

107 try: 

108 async with UnitOfWork(database): 

109 await AddCoachToTraining( 

110 TrainingDbRepository(database), 

111 CoachDbRepository(database), 

112 Owner(id=user.id, uuid=user.uuid, name=user.name), 

113 presenter, 

114 ).execute(command) 

115 except TrainingNotFoundException as ex: 

116 raise HTTPException( 

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

118 ) from ex 

119 except CoachNotFoundException as ex: 

120 raise HTTPException( 

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

122 ) from ex 

123 

124 return presenter.get_document() 

125 

126 

127@router.delete( 

128 "/trainings/{training_id}/coaches/{coach_id}", 

129 responses={status.HTTP_404_NOT_FOUND: {"description": "Training was not found."}}, 

130 status_code=status.HTTP_200_OK, 

131 dependencies=[Depends(check_permission)], 

132) 

133async def delete_coach_from_training( 

134 training_id: int, 

135 coach_id: str, 

136 database: Annotated[Database, Depends(create_database)], 

137 user: Annotated[UserEntity, Depends(get_current_user)], 

138) -> TrainingDocument: 

139 """Remove a coach from a training.""" 

140 command = DeleteCoachFromTrainingCommand( 

141 training_id=training_id, coach_uuid=coach_id 

142 ) 

143 presenter = JsonApiTrainingPresenter() 

144 try: 

145 async with UnitOfWork(database): 

146 await DeleteCoachFromTraining( 

147 TrainingDbRepository(database), presenter 

148 ).execute(command) 

149 except TrainingNotFoundException as ex: 

150 raise HTTPException( 

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

152 ) from ex 

153 

154 return presenter.get_document() 

155 

156 

157@router.patch( 

158 "/trainings/{training_id}/coaches/{coach_id}", 

159 responses={ 

160 status.HTTP_404_NOT_FOUND: {"description": "Training or coach was not found."} 

161 }, 

162 status_code=status.HTTP_200_OK, 

163 dependencies=[Depends(check_permission)], 

164) 

165async def update_coach_of_training( 

166 training_id: int, 

167 coach_id: str, 

168 database: Annotated[Database, Depends(create_database)], 

169 resource: TrainingCoachDocument, 

170) -> TrainingDocument: 

171 """Update the coach of a training.""" 

172 command = UpdateCoachTrainingCommand( 

173 coach_uuid=coach_id, 

174 training_id=training_id, 

175 present=resource.data.attributes.present, 

176 payed=resource.data.attributes.payed, 

177 head=resource.data.attributes.head, 

178 ) 

179 presenter = JsonApiTrainingPresenter() 

180 

181 try: 

182 async with UnitOfWork(database): 

183 await UpdateCoachTraining( 

184 TrainingDbRepository(database), presenter 

185 ).execute(command) 

186 except TrainingNotFoundException as ex: 

187 raise HTTPException( 

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

189 ) from ex 

190 

191 return presenter.get_document()