добавил слои для типов линз

This commit is contained in:
Андрей Дувакин 2025-02-19 20:13:46 +05:00
parent 699bf958a9
commit b08c469950
9 changed files with 85 additions and 25 deletions

View File

@ -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 sqlalchemy.ext.asyncio import AsyncSession
from app.domain.models import LensType from app.domain.models import LensType
@ -8,28 +10,28 @@ class LensTypesRepository:
def __init__(self, db: AsyncSession): def __init__(self, db: AsyncSession):
self.db = db self.db = db
async def get_all(self): async def get_all(self) -> Sequence[LensType]:
stmt = select(LensType) stmt = select(LensType)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().all() 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) stmt = select(LensType).filter(LensType.id == lens_type_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().first() return result.scalars().first()
async def create(self, lens_type: LensType): async def create(self, lens_type: LensType) -> LensType:
self.db.add(lens_type) self.db.add(lens_type)
await self.db.commit() await self.db.commit()
await self.db.refresh(lens_type) await self.db.refresh(lens_type)
return 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.merge(lens_type)
await self.db.commit() await self.db.commit()
return lens_type 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) stmt = select(LensType).filter(LensType.id == lens_type_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
lens_type = result.scalars().first() lens_type = result.scalars().first()

View File

@ -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 sqlalchemy.ext.asyncio import AsyncSession
from app.domain.models import Lens from app.domain.models import Lens
@ -8,28 +10,28 @@ class LensesRepository:
def __init__(self, db: AsyncSession): def __init__(self, db: AsyncSession):
self.db = db self.db = db
async def get_all(self): async def get_all(self) -> Sequence[Lens]:
stmt = select(Lens) stmt = select(Lens)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().all() 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) stmt = select(Lens).filter(Lens.id == lens_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().first() return result.scalars().first()
async def create(self, lens: Lens): async def create(self, lens: Lens) -> Lens:
self.db.add(lens) self.db.add(lens)
await self.db.commit() await self.db.commit()
await self.db.refresh(lens) await self.db.refresh(lens)
return lens return lens
async def update(self, lens: Lens): async def update(self, lens: Lens) -> Lens:
await self.db.merge(lens) await self.db.merge(lens)
await self.db.commit() await self.db.commit()
return lens 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) stmt = select(Lens).filter(Lens.id == lens_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
lens = result.scalars().first() lens = result.scalars().first()

View File

@ -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 sqlalchemy.ext.asyncio import AsyncSession
from app.domain.models import Patient from app.domain.models import Patient
@ -8,28 +10,28 @@ class PatientsRepository:
def __init__(self, db: AsyncSession): def __init__(self, db: AsyncSession):
self.db = db self.db = db
async def get_all(self): async def get_all(self) -> Sequence[Patient]:
stmt = select(Patient) stmt = select(Patient)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().all() 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) stmt = select(Patient).filter(Patient.id == patient_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().first() return result.scalars().first()
async def create(self, patient: Patient): async def create(self, patient: Patient) -> Patient:
self.db.add(patient) self.db.add(patient)
await self.db.commit() await self.db.commit()
await self.db.refresh(patient) await self.db.refresh(patient)
return patient return patient
async def update(self, patient: Patient): async def update(self, patient: Patient) -> Patient:
await self.db.merge(patient) await self.db.merge(patient)
await self.db.commit() await self.db.commit()
return patient 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) stmt = select(Patient).filter(Patient.id == patient_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
patient = result.scalars().first() patient = result.scalars().first()

View File

@ -1,3 +1,5 @@
from typing import Optional, Sequence
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select from sqlalchemy.future import select
@ -8,12 +10,12 @@ class RolesRepository:
def __init__(self, db: AsyncSession): def __init__(self, db: AsyncSession):
self.db = db self.db = db
async def get_all(self): async def get_all(self) -> Sequence[Role]:
stmt = select(Role) stmt = select(Role)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().all() 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) stmt = select(Role).filter(Role.id == role_id)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().first() return result.scalars().first()

View File

@ -1,4 +1,4 @@
from typing import Optional from typing import Optional, Sequence
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select from sqlalchemy.future import select
@ -11,7 +11,7 @@ class UsersRepository:
def __init__(self, db: AsyncSession): def __init__(self, db: AsyncSession):
self.db = db self.db = db
async def get_all(self): async def get_all(self) -> Sequence[User]:
stmt = select(User) stmt = select(User)
result = await self.db.execute(stmt) result = await self.db.execute(stmt)
return result.scalars().all() return result.scalars().all()

View File

@ -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()

View File

@ -0,0 +1,8 @@
from typing import Optional
from pydantic import BaseModel
class LensTypeEntity(BaseModel):
id: Optional[int] = None
title: str

View File

@ -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
]

View File

@ -4,7 +4,8 @@ from starlette.middleware.cors import CORSMiddleware
from app.controllers.auth_router import router as auth_router from app.controllers.auth_router import router as auth_router
from app.controllers.register_routes import router as register_router from app.controllers.register_routes import router as register_router
from app.controllers.patients_router import router as patients_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 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(auth_router, prefix=settings.APP_PREFIX, tags=['auth'])
api_app.include_router(register_router, prefix=settings.APP_PREFIX, tags=['register']) 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(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 return api_app