Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrOertlin committed Apr 11, 2024
2 parents 156a8a5 + 2c76f9e commit c570bd9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 9 additions & 2 deletions genotype_api/api/endpoints/plates.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from genotype_api.database.store import Store, get_store
from genotype_api.dto.plate import PlateResponse
from genotype_api.dto.user import CurrentUser
from genotype_api.exceptions import PlateNotFoundError
from genotype_api.exceptions import PlateNotFoundError, PlateExistsError
from genotype_api.security import get_active_user
from genotype_api.services.endpoint_services.plate_service import PlateService

Expand All @@ -30,7 +30,14 @@ def upload_plate(
plate_service: PlateService = Depends(get_plate_service),
current_user: CurrentUser = Depends(get_active_user),
):
plate_service.upload_plate(file)

try:
plate_service.upload_plate(file)
except PlateExistsError:
raise HTTPException(
detail="Plate already exists in the database.", status_code=HTTPStatus.BAD_REQUEST
)
return JSONResponse("Plate uploaded successfully", status_code=status.HTTP_201_CREATED)


@router.patch(
Expand Down
4 changes: 4 additions & 0 deletions genotype_api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ class SampleExistsError(Exception):

class SNPExistsError(Exception):
pass


class PlateExistsError(Exception):
pass
8 changes: 3 additions & 5 deletions genotype_api/services/endpoint_services/plate_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from genotype_api.database.filter_models.plate_models import PlateSignOff, PlateOrderParams
from genotype_api.database.models import Plate, Analysis, User, Sample
from genotype_api.dto.plate import PlateResponse, UserOnPlate, AnalysisOnPlate, SampleStatus
from genotype_api.exceptions import PlateNotFoundError, UserNotFoundError
from genotype_api.exceptions import PlateNotFoundError, UserNotFoundError, PlateExistsError
from genotype_api.file_parsing.excel import GenotypeAnalysis
from genotype_api.file_parsing.files import check_file
from genotype_api.services.endpoint_services.base_service import BaseService
Expand Down Expand Up @@ -73,16 +73,14 @@ def upload_plate(self, file: UploadFile):
plate_id: str = self._get_plate_id_from_file(file_name)
db_plate = self.store.get_plate_by_plate_id(plate_id)
if db_plate:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Plate with id {db_plate.id} already exists",
)
raise PlateExistsError

excel_parser = GenotypeAnalysis(
excel_file=BytesIO(file.file.read()),
file_name=str(file_name),
include_key="-CG-",
)

analyses: list[Analysis] = list(excel_parser.generate_analyses())
self.store.check_analyses_objects(analyses=analyses, analysis_type=Types.GENOTYPE)
self.store.create_analyses_samples(analyses=analyses)
Expand Down

0 comments on commit c570bd9

Please sign in to comment.