Skip to content

Commit

Permalink
Simplify async query execution in ReadHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
ahdamin committed Oct 9, 2024
1 parent dcb8078 commit 3a230f4
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions genotype_api/database/crud/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,27 @@ async def get_analyses_by_plate_id(self, plate_id: int) -> list[Analysis]:
filtered_query = apply_analysis_filter(
analyses=analyses, filter_functions=filter_functions, plate_id=plate_id
)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def get_analysis_by_id(self, analysis_id: int) -> Analysis:
analyses: Query = self._get_query(Analysis)
filter_functions = [AnalysisFilter.BY_ID]
filtered_query = apply_analysis_filter(
analyses=analyses, filter_functions=filter_functions, analysis_id=analysis_id
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_analyses(self) -> list[Analysis]:
filtered_query = self._get_query(Analysis)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def get_analyses_with_skip_and_limit(self, skip: int, limit: int) -> list[Analysis]:
analyses: Query = self._get_query(Analysis)
filter_functions = [AnalysisFilter.SKIP_AND_LIMIT]
filtered_query = apply_analysis_filter(
analyses=analyses, filter_functions=filter_functions, skip=skip, limit=limit
)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def get_analyses_by_type_between_dates(
self, analysis_type: Types, date_min: date, date_max: date
Expand All @@ -80,9 +76,7 @@ async def get_analyses_by_type_between_dates(
)

filtered_query = filtered_query.options(selectinload(Analysis.genotypes))

result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def get_analysis_by_type_and_sample_id(
self, sample_id: str, analysis_type: Types
Expand All @@ -97,9 +91,7 @@ async def get_analysis_by_type_and_sample_id(
)

filtered_query = filtered_query.options(selectinload(Analysis.genotypes))

result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_plate_by_id(self, plate_id: int) -> Plate:
plates: Query = self._get_query(Plate).options(
Expand All @@ -109,17 +101,15 @@ async def get_plate_by_id(self, plate_id: int) -> Plate:
filtered_query = apply_plate_filter(
plates=plates, filter_functions=filter_functions, entry_id=plate_id
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_plate_by_plate_id(self, plate_id: str) -> Plate:
plates: Query = self._get_query(Plate).options(selectinload(Plate.analyses))
filter_functions = [PlateFilter.BY_PLATE_ID]
filtered_query = apply_plate_filter(
plates=plates, filter_functions=filter_functions, plate_id=plate_id
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_ordered_plates(self, order_params: PlateOrderParams) -> list[Plate]:
sort_func = desc if order_params.sort_order == "descend" else asc
Expand All @@ -135,17 +125,15 @@ async def get_ordered_plates(self, order_params: PlateOrderParams) -> list[Plate
limit=order_params.limit,
sort_func=sort_func,
)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def get_genotype_by_id(self, entry_id: int) -> Genotype:
genotypes: Query = self._get_query(Genotype).options(selectinload(Genotype.analysis))
filter_functions = [GenotypeFilter.BY_ID]
filtered_query = apply_genotype_filter(
genotypes=genotypes, filter_functions=filter_functions, entry_id=entry_id
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_filtered_samples(self, filter_params: SampleFilterParams) -> list[Sample]:
query = (
Expand All @@ -169,8 +157,7 @@ async def get_filtered_samples(self, filter_params: SampleFilterParams) -> list[
.offset(filter_params.skip)
.limit(filter_params.limit)
)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

@staticmethod
def _get_incomplete_samples(query: Query) -> Query:
Expand Down Expand Up @@ -210,35 +197,31 @@ async def get_sample_by_id(self, sample_id: str) -> Sample:
filtered_query = filtered_query.options(
selectinload(Sample.analyses).selectinload(Analysis.genotypes)
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_user_by_id(self, user_id: int) -> User:
users: Query = self._get_query(User).options(selectinload(User.plates))
filter_functions = [UserFilter.BY_ID]
filtered_query = apply_user_filter(
users=users, filter_functions=filter_functions, user_id=user_id
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_user_by_email(self, email: str) -> User | None:
users: Query = self._get_query(User)
filter_functions = [UserFilter.BY_EMAIL]
filtered_query = apply_user_filter(
users=users, filter_functions=filter_functions, email=email
)
result = await self.session.execute(filtered_query)
return result.scalars().first()
return (await self.session.execute(filtered_query)).scalars().first()

async def get_users_with_skip_and_limit(self, skip: int, limit: int) -> list[User]:
users: Query = self._get_query(User).options(selectinload(User.plates))
filter_functions = [UserFilter.SKIP_AND_LIMIT]
filtered_query = apply_user_filter(
users=users, filter_functions=filter_functions, skip=skip, limit=limit
)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def check_analyses_objects(self, analyses: list[Analysis], analysis_type: Types) -> None:
"""Raising 400 if any analysis in the list already exist in the database"""
Expand All @@ -253,14 +236,12 @@ async def check_analyses_objects(self, analyses: list[Analysis], analysis_type:

async def get_snps(self) -> list[SNP]:
filtered_query = self._get_query(SNP)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

async def get_snps_by_limit_and_skip(self, skip: int, limit: int) -> list[SNP]:
snps: Query = self._get_query(SNP)
filter_functions = [SNPFilter.SKIP_AND_LIMIT]
filtered_query = apply_snp_filter(
snps=snps, filter_functions=filter_functions, skip=skip, limit=limit
)
result = await self.session.execute(filtered_query)
return result.scalars().all()
return (await self.session.execute(filtered_query)).scalars().all()

0 comments on commit 3a230f4

Please sign in to comment.