Skip to content

Commit

Permalink
refactore(add store) (#125)(major)
Browse files Browse the repository at this point in the history
# Description

Rewrites of the sql backend to phase out sqlmodel and update to pydantic2
  • Loading branch information
ChrOertlin authored Apr 5, 2024
1 parent 7b9556a commit fce6f11
Show file tree
Hide file tree
Showing 34 changed files with 695 additions and 728 deletions.
12 changes: 9 additions & 3 deletions genotype_api/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware

from genotype_api.config import security_settings
from genotype_api.database.session_handler import create_db_and_tables
from genotype_api.config import security_settings, settings
from genotype_api.database.database import create_all_tables, initialise_database, close_session
from genotype_api.api.endpoints import samples, snps, users, plates, analyses
from sqlalchemy.exc import NoResultFound

Expand Down Expand Up @@ -74,4 +74,10 @@ def welcome():

@app.on_event("startup")
def on_startup():
create_db_and_tables()
initialise_database(settings.db_uri)
create_all_tables()


@app.on_event("shutdown")
async def on_shutdown():
close_session()
23 changes: 12 additions & 11 deletions genotype_api/api/endpoints/analyses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@

from fastapi import APIRouter, Depends, File, Query, UploadFile, status, HTTPException
from fastapi.responses import JSONResponse
from sqlmodel import Session


from genotype_api.database.models import User
from genotype_api.database.store import Store, get_store
from genotype_api.dto.analysis import AnalysisResponse
from genotype_api.dto.user import CurrentUser

from genotype_api.database.session_handler import get_session
from genotype_api.exceptions import AnalysisNotFoundError
from genotype_api.security import get_active_user
from genotype_api.services.analysis_service.analysis_service import AnalysisService
from genotype_api.services.endpoint_services.analysis_service import (
AnalysisService,
)


router = APIRouter()


def get_analysis_service(session: Session = Depends(get_session)):
return AnalysisService(session)
def get_analysis_service(store: Store = Depends(get_store)) -> AnalysisService:
return AnalysisService(store)


@router.get("/{analysis_id}", response_model=AnalysisResponse)
def read_analysis(
analysis_id: int,
analysis_service: AnalysisService = Depends(get_analysis_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Return analysis."""
try:
Expand All @@ -43,7 +44,7 @@ def read_analyses(
skip: int = 0,
limit: int = Query(default=100, lte=100),
analysis_service: AnalysisService = Depends(get_analysis_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Return all analyses."""
try:
Expand All @@ -59,7 +60,7 @@ def read_analyses(
def delete_analysis(
analysis_id: int,
analysis_service: AnalysisService = Depends(get_analysis_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Delete analysis based on analysis id."""
try:
Expand All @@ -78,7 +79,7 @@ def delete_analysis(
def upload_sequence_analysis(
file: UploadFile = File(...),
analysis_service: AnalysisService = Depends(get_analysis_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Reading VCF file, creating and uploading sequence analyses and sample objects to the database."""

Expand Down
25 changes: 13 additions & 12 deletions genotype_api/api/endpoints/plates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
from typing import Literal
from fastapi import APIRouter, Depends, File, Query, UploadFile, status, HTTPException
from fastapi.responses import JSONResponse
from sqlmodel import Session
from genotype_api.database.filter_models.plate_models import PlateOrderParams
from genotype_api.database.models import User
from genotype_api.database.session_handler import get_session


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.security import get_active_user
from genotype_api.services.plate_service.plate_service import PlateService
from genotype_api.services.endpoint_services.plate_service import PlateService


router = APIRouter()


def get_plate_service(session: Session = Depends(get_session)) -> PlateService:
return PlateService(session)
def get_plate_service(store: Store = Depends(get_store)) -> PlateService:
return PlateService(store)


@router.post(
Expand All @@ -29,7 +30,7 @@ def get_plate_service(session: Session = Depends(get_session)) -> PlateService:
def upload_plate(
file: UploadFile = File(...),
plate_service: PlateService = Depends(get_plate_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
return plate_service.upload_plate(file)

Expand All @@ -44,10 +45,10 @@ def sign_off_plate(
method_document: str = Query(...),
method_version: str = Query(...),
plate_service: PlateService = Depends(get_plate_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Sign off a plate.
This means that current User sign off that the plate is checked
This means that current CurrentUser sign off that the plate is checked
Add Depends with current user
"""

Expand Down Expand Up @@ -84,7 +85,7 @@ def sign_off_plate(
def read_plate(
plate_id: int,
plate_service: PlateService = Depends(get_plate_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Display information about a plate."""
try:
Expand All @@ -107,7 +108,7 @@ async def read_plates(
skip: int | None = 0,
limit: int | None = 10,
plate_service: PlateService = Depends(get_plate_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Display all plates"""
order_params = PlateOrderParams(
Expand All @@ -125,7 +126,7 @@ async def read_plates(
def delete_plate(
plate_id: int,
plate_service: PlateService = Depends(get_plate_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Delete plate."""
try:
Expand Down
39 changes: 18 additions & 21 deletions genotype_api/api/endpoints/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@

from fastapi import APIRouter, Depends, Query
from fastapi.responses import JSONResponse
from sqlmodel import Session
from starlette import status

from genotype_api.constants import Sexes, Types
from genotype_api.database.filter_models.sample_models import SampleFilterParams
from genotype_api.database.models import (
Sample,
User,
)
from genotype_api.database.session_handler import get_session
from genotype_api.dto.sample import SampleResponse

from genotype_api.database.store import Store, get_store
from genotype_api.dto.sample import SampleResponse, SampleCreate
from genotype_api.dto.user import CurrentUser
from genotype_api.exceptions import SampleNotFoundError, SampleExistsError
from genotype_api.models import MatchResult, SampleDetail
from genotype_api.security import get_active_user
from genotype_api.services.sample_service.sample_service import SampleService
from genotype_api.services.endpoint_services.sample_service import SampleService

router = APIRouter()


def get_sample_service(session: Session = Depends(get_session)) -> SampleService:
return SampleService(session)
def get_sample_service(store: Store = Depends(get_store)) -> SampleService:
return SampleService(store)


@router.get(
Expand All @@ -34,7 +31,7 @@ def get_sample_service(session: Session = Depends(get_session)) -> SampleService
def read_sample(
sample_id: str,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
try:
return sample_service.get_sample(sample_id)
Expand All @@ -48,12 +45,12 @@ def read_sample(
"/",
)
def create_sample(
sample: Sample,
sample: SampleCreate,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
try:
sample_service.create_sample(sample=sample)
sample_service.create_sample(sample_create=sample)
new_sample: SampleResponse = sample_service.get_sample(sample_id=sample.id)
if not new_sample:
return JSONResponse(
Expand Down Expand Up @@ -90,7 +87,7 @@ def read_samples(
commented: bool | None = False,
status_missing: bool | None = False,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Returns a list of samples matching the provided filters."""
filter_params = SampleFilterParams(
Expand All @@ -113,7 +110,7 @@ def update_sex(
genotype_sex: Sexes | None = None,
sequence_sex: Sexes | None = None,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Updating sex field on sample and sample analyses."""
try:
Expand All @@ -135,7 +132,7 @@ def update_comment(
sample_id: str,
comment: str = Query(...),
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Updating comment field on sample."""
try:
Expand All @@ -155,7 +152,7 @@ def set_sample_status(
sample_id: str,
sample_service: SampleService = Depends(get_sample_service),
status: Literal["pass", "fail", "cancel"] | None = None,
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Check sample analyses and update sample status accordingly."""
try:
Expand All @@ -175,7 +172,7 @@ def match(
date_min: date | None = date.min,
date_max: date | None = date.max,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
) -> list[MatchResult]:
"""Match sample genotype against all other genotypes."""
return sample_service.get_match_results(
Expand All @@ -196,7 +193,7 @@ def match(
def get_status_detail(
sample_id: str,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
try:
return sample_service.get_status_detail(sample_id)
Expand All @@ -210,7 +207,7 @@ def get_status_detail(
def delete_sample(
sample_id: str,
sample_service: SampleService = Depends(get_sample_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Delete sample and its Analyses."""
sample_service.delete_sample(sample_id)
Expand Down
32 changes: 13 additions & 19 deletions genotype_api/api/endpoints/snps.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
"""Routes for the snps"""

from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile
from sqlmodel import Session
from sqlmodel.sql.expression import Select, SelectOfScalar
from starlette.responses import JSONResponse
from fastapi import APIRouter, Depends, Query, UploadFile


from genotype_api.database.crud.create import create_snps
from genotype_api.database.crud.read import get_snps, get_snps_by_limit_and_skip
from genotype_api.database.models import SNP, User
from genotype_api.database.crud import delete
from genotype_api.database.session_handler import get_session
from starlette.responses import JSONResponse
from genotype_api.database.store import Store, get_store
from genotype_api.dto.snp import SNPResponse
from genotype_api.dto.user import CurrentUser
from genotype_api.exceptions import SNPExistsError
from genotype_api.security import get_active_user
from genotype_api.services.snp_reader_service.snp_reader import SNPReaderService
from genotype_api.services.snp_service.snp_service import SNPService

SelectOfScalar.inherit_cache = True
Select.inherit_cache = True
from genotype_api.services.endpoint_services.snp_service import SNPService


router = APIRouter()


def get_snp_service(session: Session = Depends(get_session)) -> SNPService:
return SNPService(session)
def get_snp_service(store: Store = Depends(get_store)) -> SNPService:
return SNPService(store)


@router.get("/", response_model=list[SNPResponse])
def read_snps(
skip: int = 0,
limit: int = Query(default=100, lte=100),
snp_service: SNPService = Depends(get_snp_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
return snp_service.get_snps(skip=skip, limit=limit)


@router.post("/", response_model=list[SNP])
@router.post("/", response_model=list[SNPResponse])
async def upload_snps(
snps_file: UploadFile,
snp_service: SNPService = Depends(get_snp_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
try:
return snp_service.upload_snps(snps_file)
Expand All @@ -51,7 +45,7 @@ async def upload_snps(
@router.delete("/")
def delete_snps(
snp_service: SNPService = Depends(get_snp_service),
current_user: User = Depends(get_active_user),
current_user: CurrentUser = Depends(get_active_user),
):
"""Delete all SNPs"""

Expand Down
Loading

0 comments on commit fce6f11

Please sign in to comment.