From 7ffef3424079d7389bb384da6207d0f18007f1eb Mon Sep 17 00:00:00 2001 From: fbraem Date: Mon, 18 Sep 2023 14:42:40 +0200 Subject: [PATCH] feat: add api to get trainings from a training definition --- .../endpoints/training_definitions.py | 55 +++++++++++++++++++ .../endpoints/test_training_definitions.py | 14 +++++ 2 files changed, 69 insertions(+) diff --git a/backend/src/kwai/api/v1/trainings/endpoints/training_definitions.py b/backend/src/kwai/api/v1/trainings/endpoints/training_definitions.py index 0dd32d3ea..9db0163f5 100644 --- a/backend/src/kwai/api/v1/trainings/endpoints/training_definitions.py +++ b/backend/src/kwai/api/v1/trainings/endpoints/training_definitions.py @@ -2,11 +2,15 @@ from fastapi import APIRouter, Depends, HTTPException, status from kwai.api.dependencies import deps, get_current_user +from kwai.api.v1.trainings.endpoints.trainings import TrainingsFilterModel +from kwai.api.v1.trainings.schemas.training import TrainingResource from kwai.api.v1.trainings.schemas.training_definition import TrainingDefinitionResource from kwai.core.db.database import Database from kwai.core.domain.value_objects.owner import Owner from kwai.core.json_api import Meta, PaginationModel from kwai.modules.identity.users.user import UserEntity +from kwai.modules.training.coaches.coach_db_repository import CoachDbRepository +from kwai.modules.training.coaches.coach_repository import CoachNotFoundException from kwai.modules.training.create_training_definition import ( CreateTrainingDefinition, CreateTrainingDefinitionCommand, @@ -23,6 +27,7 @@ GetTrainingDefinitions, GetTrainingDefinitionsCommand, ) +from kwai.modules.training.get_trainings import GetTrainings, GetTrainingsCommand from kwai.modules.training.trainings.training_db_repository import TrainingDbRepository from kwai.modules.training.trainings.training_definition_db_repository import ( TrainingDefinitionDbRepository, @@ -181,3 +186,53 @@ async def delete_training_definition( await DeleteTrainingDefinition( TrainingDefinitionDbRepository(db), TrainingDbRepository(db) ).execute(command) + + +@router.get( + "/training_definitions/{training_definition_id}/trainings", + responses={ + status.HTTP_404_NOT_FOUND: { + "description": "Training definition or coach was not found." + } + }, +) +async def get_trainings( + training_definition_id: int, + pagination: PaginationModel = Depends(PaginationModel), + trainings_filter: TrainingsFilterModel = Depends(TrainingsFilterModel), + db=deps.depends(Database), +) -> TrainingResource.get_document_model(): + """Get trainings of the given training definition.""" + command = GetTrainingsCommand( + offset=pagination.offset or 0, + limit=pagination.limit, + year=trainings_filter.year, + month=trainings_filter.month, + start=trainings_filter.start, + end=trainings_filter.end, + active=trainings_filter.active, + coach=trainings_filter.coach, + definition=training_definition_id, + ) + + try: + count, training_iterator = await GetTrainings( + TrainingDbRepository(db), + CoachDbRepository(db), + TrainingDefinitionDbRepository(db), + ).execute(command) + except TrainingDefinitionNotFoundException as ex: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, detail=str(ex) + ) from ex + except CoachNotFoundException as ex: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, detail=str(ex) + ) from ex + + document = TrainingResource.serialize_list( + [TrainingResource(training) async for training in training_iterator] + ) + document.meta = Meta(count=count, offset=command.offset, limit=command.limit) + + return document diff --git a/backend/src/tests/api/v1/trainings/endpoints/test_training_definitions.py b/backend/src/tests/api/v1/trainings/endpoints/test_training_definitions.py index cecf36988..96745c333 100644 --- a/backend/src/tests/api/v1/trainings/endpoints/test_training_definitions.py +++ b/backend/src/tests/api/v1/trainings/endpoints/test_training_definitions.py @@ -107,3 +107,17 @@ def test_delete_training_definition( f"/api/v1/training_definitions/{training_definition_entity.id}" ) assert response.status_code == status.HTTP_200_OK + + +def test_get_trainings( + client: TestClient, training_definition_entity: TrainingDefinitionEntity +): + """Test get trainings from a training definition.""" + response = client.get( + f"/api/v1/training_definitions/{training_definition_entity.id}/trainings" + ) + assert response.status_code == status.HTTP_200_OK + + json = response.json() + assert "meta" in json, "There should be a meta object in the response" + assert "data" in json, "There should be a data list in the response"