Skip to content

Commit

Permalink
move session handling (#105) (patch)
Browse files Browse the repository at this point in the history
## Description

Move session handling from analysis endpoint to crud
  • Loading branch information
ChrOertlin authored Mar 18, 2024
1 parent 4c6a9e7 commit f4a3343
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
20 changes: 11 additions & 9 deletions genotype_api/api/endpoints/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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])
Expand All @@ -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


Expand All @@ -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)


Expand Down
8 changes: 6 additions & 2 deletions genotype_api/database/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down

0 comments on commit f4a3343

Please sign in to comment.