From 0a9cc6bf4e1ab8ba3e57fe2eb0bc480d9b084425 Mon Sep 17 00:00:00 2001 From: Andrei Duvakin Date: Sun, 9 Feb 2025 12:20:30 +0500 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20=D1=80?= =?UTF-8?q?=D0=B5=D0=BF=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=B8=20=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=20=D0=B2=20=D0=90?= =?UTF-8?q?=D0=9F=D0=98=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/application/patients_repository.py | 42 +++++++++++ api/app/application/users_repository.py | 1 - api/app/domain/entities/patient.py | 16 +++++ api/app/domain/entities/register.py | 2 +- api/app/domain/entities/user.py | 2 +- api/app/infrastructure/patient_service.py | 84 ++++++++++++++++++++++ web-app/src/pages/PatientsPage.jsx | 5 ++ 7 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 api/app/application/patients_repository.py create mode 100644 api/app/domain/entities/patient.py create mode 100644 api/app/infrastructure/patient_service.py create mode 100644 web-app/src/pages/PatientsPage.jsx diff --git a/api/app/application/patients_repository.py b/api/app/application/patients_repository.py new file mode 100644 index 0000000..65cc79a --- /dev/null +++ b/api/app/application/patients_repository.py @@ -0,0 +1,42 @@ +from sqlalchemy import select +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): + stmt = select(Patient) + result = await self.db.execute(stmt) + return result.scalars().all() + + async def get_by_id(self, patient_id: int): + stmt = select(Patient).filter(Patient.id == patient_id) + result = await self.db.execute(stmt) + return result.scalars().first() + + async def create(self, patient: Patient): + self.db.add(patient) + await self.db.commit() + await self.db.refresh(patient) + return patient + + async def update(self, patient: Patient): + await self.db.merge(patient) + await self.db.commit() + return patient + + async def delete(self, patient_id: int): + stmt = select(Patient).filter(Patient.id == patient_id) + result = await self.db.execute(stmt) + patient = result.scalars().first() + + if patient: + await self.db.delete(patient) + await self.db.commit() + return patient + + return None diff --git a/api/app/application/users_repository.py b/api/app/application/users_repository.py index ce7caff..9e89360 100644 --- a/api/app/application/users_repository.py +++ b/api/app/application/users_repository.py @@ -32,5 +32,4 @@ class UsersRepository: self.db.add(user) await self.db.commit() await self.db.refresh(user) - return user diff --git a/api/app/domain/entities/patient.py b/api/app/domain/entities/patient.py new file mode 100644 index 0000000..b622a1e --- /dev/null +++ b/api/app/domain/entities/patient.py @@ -0,0 +1,16 @@ +from typing import Optional + +from pydantic import BaseModel + + +class PatientEntity(BaseModel): + id: Optional[int] + first_name: str + last_name: str + patronymic: Optional[str] + birthday: str + address: Optional[str] + email: Optional[str] + phone: Optional[str] + diagnosis: Optional[str] + correction: Optional[str] diff --git a/api/app/domain/entities/register.py b/api/app/domain/entities/register.py index ad923f3..e44bc47 100644 --- a/api/app/domain/entities/register.py +++ b/api/app/domain/entities/register.py @@ -1,6 +1,6 @@ from typing import Optional -from pydantic import BaseModel, Field +from pydantic import BaseModel class RegisterEntity(BaseModel): diff --git a/api/app/domain/entities/user.py b/api/app/domain/entities/user.py index 159c0e8..d602870 100644 --- a/api/app/domain/entities/user.py +++ b/api/app/domain/entities/user.py @@ -1,6 +1,6 @@ from typing import Optional -from pydantic import BaseModel, Field +from pydantic import BaseModel class UserEntity(BaseModel): diff --git a/api/app/infrastructure/patient_service.py b/api/app/infrastructure/patient_service.py new file mode 100644 index 0000000..886fa91 --- /dev/null +++ b/api/app/infrastructure/patient_service.py @@ -0,0 +1,84 @@ +from typing import Optional + +from sqlalchemy.ext.asyncio import AsyncSession + +from app.application.patients_repository import PatientsRepository +from app.domain.entities.patient import PatientEntity +from app.domain.models import Patient + + +class PatientService: + def __init__(self, db: AsyncSession): + self.patient_repository = PatientsRepository(db) + + async def get_all_patients(self) -> list[PatientEntity]: + patients = await self.patient_repository.get_all() + return [ + PatientEntity( + id=patient.id, + first_name=patient.first_name, + last_name=patient.last_name, + patronymic=patient.patronymic, + birthday=patient.birthday, + address=patient.address, + email=patient.email, + phone=patient.phone, + diagnosis=patient.diagnosis, + correction=patient.correction, + ) + for patient in patients + ] + + async def create_patient(self, patient: PatientEntity) -> PatientEntity: + patient_model = Patient( + first_name=patient.first_name, + last_name=patient.last_name, + patronymic=patient.patronymic, + birthday=patient.birthday, + address=patient.address, + email=patient.email, + phone=patient.phone, + diagnosis=patient.diagnosis, + correction=patient.correction, + ) + await self.patient_repository.create(patient_model) + return PatientEntity( + id=patient_model.id, + first_name=patient_model.first_name, + last_name=patient_model.last_name, + patronymic=patient_model.patronymic, + birthday=patient_model.birthday, + address=patient_model.address, + email=patient_model.email, + phone=patient_model.phone, + diagnosis=patient_model.diagnosis, + correction=patient_model.correction, + ) + + async def update_patient(self, user_id: int, patient: PatientEntity) -> Optional[PatientEntity]: + patient_model = await self.patient_repository.get_by_id(user_id) + if patient_model: + patient_model.first_name = patient.first_name + patient_model.last_name = patient.last_name + patient_model.patronymic = patient.patronymic + patient_model.birthday = patient.birthday + patient_model.address = patient.address + patient_model.email = patient.email + patient_model.phone = patient.phone + patient_model.diagnosis = patient.diagnosis + patient_model.correction = patient.correction + await self.patient_repository.update(patient_model) + return PatientEntity( + id=patient_model.id, + first_name=patient_model.first_name, + last_name=patient_model.last_name, + patronymic=patient_model.patronymic, + birthday=patient_model.birthday, + address=patient_model.address, + email=patient_model.email, + phone=patient_model.phone, + diagnosis=patient_model.diagnosis, + correction=patient_model.correction, + ) + + return None diff --git a/web-app/src/pages/PatientsPage.jsx b/web-app/src/pages/PatientsPage.jsx new file mode 100644 index 0000000..b8701fa --- /dev/null +++ b/web-app/src/pages/PatientsPage.jsx @@ -0,0 +1,5 @@ +const PatientsPage = () => { + + return ( + ) +} \ No newline at end of file