From b08c469950fef7f7e6def1597986b70022e4b686 Mon Sep 17 00:00:00 2001 From: Andrei Duvakin Date: Wed, 19 Feb 2025 20:13:46 +0500 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=81=D0=BB=D0=BE=D0=B8=20=D0=B4=D0=BB=D1=8F=20=D1=82=D0=B8?= =?UTF-8?q?=D0=BF=D0=BE=D0=B2=20=D0=BB=D0=B8=D0=BD=D0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/application/lens_types_repository.py | 16 ++++++++------ api/app/application/lenses_repository.py | 14 +++++++----- api/app/application/patients_repository.py | 14 +++++++----- api/app/application/roles_repository.py | 6 +++-- api/app/application/users_repository.py | 4 ++-- api/app/controllers/lens_types_router.py | 23 ++++++++++++++++++++ api/app/domain/entities/lens_type.py | 8 +++++++ api/app/infrastructure/lens_types_service.py | 19 ++++++++++++++++ api/app/main.py | 6 +++-- 9 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 api/app/controllers/lens_types_router.py create mode 100644 api/app/domain/entities/lens_type.py create mode 100644 api/app/infrastructure/lens_types_service.py diff --git a/api/app/application/lens_types_repository.py b/api/app/application/lens_types_repository.py index e05715b..3edf2c5 100644 --- a/api/app/application/lens_types_repository.py +++ b/api/app/application/lens_types_repository.py @@ -1,4 +1,6 @@ -from sqlalchemy import select +from typing import Optional, Sequence + +from sqlalchemy import select, Row, RowMapping from sqlalchemy.ext.asyncio import AsyncSession from app.domain.models import LensType @@ -8,28 +10,28 @@ class LensTypesRepository: def __init__(self, db: AsyncSession): self.db = db - async def get_all(self): + async def get_all(self) -> Sequence[LensType]: stmt = select(LensType) result = await self.db.execute(stmt) return result.scalars().all() - async def get_by_id(self, lens_type_id: int): + async def get_by_id(self, lens_type_id: int) -> Optional[LensType]: stmt = select(LensType).filter(LensType.id == lens_type_id) result = await self.db.execute(stmt) return result.scalars().first() - async def create(self, lens_type: LensType): + async def create(self, lens_type: LensType) -> LensType: self.db.add(lens_type) await self.db.commit() await self.db.refresh(lens_type) return lens_type - async def update(self, lens_type: LensType): + async def update(self, lens_type: LensType) -> LensType: await self.db.merge(lens_type) await self.db.commit() return lens_type - async def delete(self, lens_type_id: int): + async def delete(self, lens_type_id: int) -> Row[LensType] | RowMapping | None: stmt = select(LensType).filter(LensType.id == lens_type_id) result = await self.db.execute(stmt) lens_type = result.scalars().first() @@ -39,4 +41,4 @@ class LensTypesRepository: await self.db.commit() return lens_type - return None \ No newline at end of file + return None diff --git a/api/app/application/lenses_repository.py b/api/app/application/lenses_repository.py index 1fe8b21..6d5878d 100644 --- a/api/app/application/lenses_repository.py +++ b/api/app/application/lenses_repository.py @@ -1,4 +1,6 @@ -from sqlalchemy import select +from typing import Sequence, Optional + +from sqlalchemy import select, Row, RowMapping from sqlalchemy.ext.asyncio import AsyncSession from app.domain.models import Lens @@ -8,28 +10,28 @@ class LensesRepository: def __init__(self, db: AsyncSession): self.db = db - async def get_all(self): + async def get_all(self) -> Sequence[Lens]: stmt = select(Lens) result = await self.db.execute(stmt) return result.scalars().all() - async def get_by_id(self, lens_id: int): + async def get_by_id(self, lens_id: int) -> Optional[Lens]: stmt = select(Lens).filter(Lens.id == lens_id) result = await self.db.execute(stmt) return result.scalars().first() - async def create(self, lens: Lens): + async def create(self, lens: Lens) -> Lens: self.db.add(lens) await self.db.commit() await self.db.refresh(lens) return lens - async def update(self, lens: Lens): + async def update(self, lens: Lens) -> Lens: await self.db.merge(lens) await self.db.commit() return lens - async def delete(self, lens_id: int): + async def delete(self, lens_id: int) -> Row[Lens] | RowMapping | None: stmt = select(Lens).filter(Lens.id == lens_id) result = await self.db.execute(stmt) lens = result.scalars().first() diff --git a/api/app/application/patients_repository.py b/api/app/application/patients_repository.py index 65cc79a..8b70cc9 100644 --- a/api/app/application/patients_repository.py +++ b/api/app/application/patients_repository.py @@ -1,4 +1,6 @@ -from sqlalchemy import select +from typing import Sequence + +from sqlalchemy import select, Row, RowMapping from sqlalchemy.ext.asyncio import AsyncSession from app.domain.models import Patient @@ -8,28 +10,28 @@ class PatientsRepository: def __init__(self, db: AsyncSession): self.db = db - async def get_all(self): + async def get_all(self) -> Sequence[Patient]: stmt = select(Patient) result = await self.db.execute(stmt) return result.scalars().all() - async def get_by_id(self, patient_id: int): + async def get_by_id(self, patient_id: int) -> Patient: stmt = select(Patient).filter(Patient.id == patient_id) result = await self.db.execute(stmt) return result.scalars().first() - async def create(self, patient: Patient): + 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): + async def update(self, patient: Patient) -> Patient: await self.db.merge(patient) await self.db.commit() return patient - async def delete(self, patient_id: int): + async def delete(self, patient_id: int) -> Row[Patient] | RowMapping | None: stmt = select(Patient).filter(Patient.id == patient_id) result = await self.db.execute(stmt) patient = result.scalars().first() diff --git a/api/app/application/roles_repository.py b/api/app/application/roles_repository.py index 8409951..d91cad9 100644 --- a/api/app/application/roles_repository.py +++ b/api/app/application/roles_repository.py @@ -1,3 +1,5 @@ +from typing import Optional, Sequence + from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select @@ -8,12 +10,12 @@ class RolesRepository: def __init__(self, db: AsyncSession): self.db = db - async def get_all(self): + async def get_all(self) -> Sequence[Role]: stmt = select(Role) result = await self.db.execute(stmt) return result.scalars().all() - async def get_by_id(self, role_id: int): + async def get_by_id(self, role_id: int) -> Optional[Role]: stmt = select(Role).filter(Role.id == role_id) result = await self.db.execute(stmt) return result.scalars().first() diff --git a/api/app/application/users_repository.py b/api/app/application/users_repository.py index 1cc5958..c982642 100644 --- a/api/app/application/users_repository.py +++ b/api/app/application/users_repository.py @@ -1,4 +1,4 @@ -from typing import Optional +from typing import Optional, Sequence from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.future import select @@ -11,7 +11,7 @@ class UsersRepository: def __init__(self, db: AsyncSession): self.db = db - async def get_all(self): + async def get_all(self) -> Sequence[User]: stmt = select(User) result = await self.db.execute(stmt) return result.scalars().all() diff --git a/api/app/controllers/lens_types_router.py b/api/app/controllers/lens_types_router.py new file mode 100644 index 0000000..1e92cd2 --- /dev/null +++ b/api/app/controllers/lens_types_router.py @@ -0,0 +1,23 @@ +from fastapi import APIRouter, Depends +from sqlalchemy.ext.asyncio import AsyncSession + +from app.database.session import get_db +from app.domain.entities.lens_type import LensTypeEntity +from app.infrastructure.dependencies import get_current_user +from app.infrastructure.lens_types_service import LensTypesService + +router = APIRouter() + + +@router.get( + "/lens_types/", + response_model=list[LensTypeEntity], + summary="Get all lens types", + description="Returns a list of all lens types", +) +async def get_all_lens_types( + db: AsyncSession = Depends(get_db), + user=Depends(get_current_user), +): + lens_types_service = LensTypesService(db) + return await lens_types_service.get_all_lens_types() diff --git a/api/app/domain/entities/lens_type.py b/api/app/domain/entities/lens_type.py new file mode 100644 index 0000000..78a69bc --- /dev/null +++ b/api/app/domain/entities/lens_type.py @@ -0,0 +1,8 @@ +from typing import Optional + +from pydantic import BaseModel + + +class LensTypeEntity(BaseModel): + id: Optional[int] = None + title: str diff --git a/api/app/infrastructure/lens_types_service.py b/api/app/infrastructure/lens_types_service.py new file mode 100644 index 0000000..43e3097 --- /dev/null +++ b/api/app/infrastructure/lens_types_service.py @@ -0,0 +1,19 @@ +from sqlalchemy.ext.asyncio import AsyncSession + +from app.application.lens_types_repository import LensTypesRepository +from app.domain.entities.lens_type import LensTypeEntity + + +class LensTypesService: + def __init__(self, db: AsyncSession): + self.lens_types_repository = LensTypesRepository(db) + + async def get_all_lens_types(self) -> list[LensTypeEntity]: + lens_types = await self.lens_types_repository.get_all() + return [ + LensTypeEntity( + id=lens_type.id, + title=lens_type.title, + ) + for lens_type in lens_types + ] diff --git a/api/app/main.py b/api/app/main.py index 600b1e9..1503558 100644 --- a/api/app/main.py +++ b/api/app/main.py @@ -4,7 +4,8 @@ from starlette.middleware.cors import CORSMiddleware from app.controllers.auth_router import router as auth_router from app.controllers.register_routes import router as register_router from app.controllers.patients_router import router as patients_router -from app.controllers.lenses_router import router as lens_router +from app.controllers.lenses_router import router as lenses_router +from app.controllers.lens_types_router import router as lens_types_router from app.settings import settings @@ -22,7 +23,8 @@ def start_app(): api_app.include_router(auth_router, prefix=settings.APP_PREFIX, tags=['auth']) api_app.include_router(register_router, prefix=settings.APP_PREFIX, tags=['register']) api_app.include_router(patients_router, prefix=settings.APP_PREFIX, tags=['patients']) - api_app.include_router(lens_router, prefix=settings.APP_PREFIX, tags=['lenses']) + api_app.include_router(lenses_router, prefix=settings.APP_PREFIX, tags=['lenses']) + api_app.include_router(lens_types_router, prefix=settings.APP_PREFIX, tags=['lens_types']) return api_app