visus-plus/api/app/application/patients_repository.py

44 lines
1.4 KiB
Python

from typing import Sequence, Optional, Tuple
from sqlalchemy import select, func
from sqlalchemy.ext.asyncio import AsyncSession
from app.domain.models import Patient
class PatientsRepository:
def __init__(self, db: AsyncSession):
self.db = db
async def get_all(self, skip: int = 0, limit: int = 10) -> Tuple[Sequence[Patient], int]:
stmt = select(Patient).offset(skip).limit(limit)
result = await self.db.execute(stmt)
patients = result.scalars().all()
count_stmt = select(func.count()).select_from(Patient)
count_result = await self.db.execute(count_stmt)
total_count = count_result.scalar()
return patients, total_count
async def get_by_id(self, patient_id: int) -> Optional[Patient]:
stmt = select(Patient).filter_by(id=patient_id)
result = await self.db.execute(stmt)
return result.scalars().first()
async def create(self, patient: Patient) -> Patient:
self.db.add(patient)
await self.db.commit()
await self.db.refresh(patient)
return patient
async def update(self, patient: Patient) -> Patient:
await self.db.merge(patient)
await self.db.commit()
return patient
async def delete(self, patient: Patient) -> Patient:
await self.db.delete(patient)
await self.db.commit()
return patient