From f4a33438d0aeb3679e6ab6957eead0fc5411f651 Mon Sep 17 00:00:00 2001 From: ChristianOertlin Date: Mon, 18 Mar 2024 12:48:22 +0100 Subject: [PATCH] move session handling (#105) (patch) ## Description Move session handling from analysis endpoint to crud --- genotype_api/api/endpoints/analyses.py | 20 +++++++++++--------- genotype_api/database/crud/read.py | 8 ++++++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/genotype_api/api/endpoints/analyses.py b/genotype_api/api/endpoints/analyses.py index 9f49a05..1082dd5 100644 --- a/genotype_api/api/endpoints/analyses.py +++ b/genotype_api/api/endpoints/analyses.py @@ -7,8 +7,13 @@ from sqlmodel import Session, select from sqlmodel.sql.expression import Select, SelectOfScalar +from genotype_api.database.crud import delete from genotype_api.database.crud.create import create_analyses_sample_objects, create_analysis -from genotype_api.database.crud.read import check_analyses_objects, get_analysis +from genotype_api.database.crud.read import ( + check_analyses_objects, + get_analysis_by_id, + get_analyses_with_skip_and_limit, +) from genotype_api.database.crud.update import refresh_sample_status from genotype_api.database.models import Analysis, AnalysisRead, AnalysisReadWithGenotype, User from genotype_api.database.session_handler import get_session @@ -29,8 +34,7 @@ def read_analysis( current_user: User = Depends(get_active_user), ): """Return analysis.""" - - return get_analysis(session=session, analysis_id=analysis_id) + return get_analysis_by_id(session=session, analysis_id=analysis_id) @router.get("/", response_model=list[AnalysisRead]) @@ -41,8 +45,9 @@ def read_analyses( current_user: User = Depends(get_active_user), ) -> list[Analysis]: """Return all analyses.""" - analyses: list[Analysis] = session.exec(select(Analysis).offset(skip).limit(limit)).all() - + analyses: list[Analysis] = get_analyses_with_skip_and_limit( + session=session, skip=skip, limit=limit + ) return analyses @@ -53,10 +58,7 @@ def delete_analysis( current_user: User = Depends(get_active_user), ): """Delete analysis based on analysis_id""" - analysis = get_analysis(session=session, analysis_id=analysis_id) - session.delete(analysis) - session.commit() - + delete.delete_analysis(session=session, analysis_id=analysis_id) return JSONResponse(f"Deleted analysis: {analysis_id}", status_code=status.HTTP_200_OK) diff --git a/genotype_api/database/crud/read.py b/genotype_api/database/crud/read.py index 1d5a927..bb0ee3a 100644 --- a/genotype_api/database/crud/read.py +++ b/genotype_api/database/crud/read.py @@ -27,13 +27,17 @@ def get_analysis_type_sample( return session.exec(statement).first() -def get_analysis(session: Session, analysis_id: int) -> Analysis: +def get_analysis_by_id(session: Session, analysis_id: int) -> Analysis: """Get analysis""" - statement = select(Analysis).where(Analysis.id == analysis_id) return session.exec(statement).one() +def get_analyses_with_skip_and_limit(session: Session, skip: int, limit: int) -> list[Analysis]: + statement = select(Analysis).offset(skip).limit(limit) + return session.exec(statement).all() + + def get_plate(session: Session, plate_id: int) -> Plate: """Get plate"""