сделал слои для таблицы выдачи линз
This commit is contained in:
parent
e7184c8e64
commit
fe67771924
27
api/app/application/lens_issues_repository.py
Normal file
27
api/app/application/lens_issues_repository.py
Normal file
@ -0,0 +1,27 @@
|
||||
from typing import Optional, Sequence
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.domain.models import LensIssue
|
||||
|
||||
|
||||
class LensIssuesRepository:
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.db = db
|
||||
|
||||
async def get_all(self) -> Sequence[LensIssue]:
|
||||
stmt = select(LensIssue)
|
||||
result = await self.db.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
async def get_by_id(self, lens_issue_id: int) -> Optional[LensIssue]:
|
||||
stmt = select(LensIssue).filter(LensIssue.id == lens_issue_id)
|
||||
result = await self.db.execute(stmt)
|
||||
return result.scalars().first()
|
||||
|
||||
async def create(self, lens_issue: LensIssue) -> LensIssue:
|
||||
self.db.add(lens_issue)
|
||||
await self.db.commit()
|
||||
await self.db.refresh(lens_issue)
|
||||
return lens_issue
|
||||
38
api/app/controllers/lens_issues_router.py
Normal file
38
api/app/controllers/lens_issues_router.py
Normal file
@ -0,0 +1,38 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.database.session import get_db
|
||||
from app.domain.entities.lens_issues import LensIssueEntity
|
||||
from app.infrastructure.dependencies import get_current_user
|
||||
from app.infrastructure.lens_issues_service import LensIssuesService
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get(
|
||||
"/lens_issues/",
|
||||
response_model=list[LensIssueEntity],
|
||||
summary="Get all lens issues",
|
||||
description="Returns a list of all lens issues",
|
||||
)
|
||||
async def get_all_lens_issues(
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user=Depends(get_current_user),
|
||||
):
|
||||
lens_issues_service = LensIssuesService(db)
|
||||
return await lens_issues_service.get_all_lens_issues()
|
||||
|
||||
|
||||
@router.post(
|
||||
"/lens_issues/",
|
||||
response_model=LensIssueEntity,
|
||||
summary="Create lens issue",
|
||||
description="Creates a new lens issue",
|
||||
)
|
||||
async def create_lens_issue(
|
||||
lens_issue: LensIssueEntity,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
user=Depends(get_current_user),
|
||||
):
|
||||
lens_issues_service = LensIssuesService(db)
|
||||
return await lens_issues_service.create_lens_issue(lens_issue, user.id)
|
||||
13
api/app/domain/entities/lens_issues.py
Normal file
13
api/app/domain/entities/lens_issues.py
Normal file
@ -0,0 +1,13 @@
|
||||
import datetime
|
||||
from typing import Optional
|
||||
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class LensIssueEntity(BaseModel):
|
||||
id: Optional[int] = None
|
||||
issue_date: datetime.date
|
||||
|
||||
patient_id: int
|
||||
doctor_id: Optional[int] = None
|
||||
lens_id: int
|
||||
85
api/app/infrastructure/lens_issues_service.py
Normal file
85
api/app/infrastructure/lens_issues_service.py
Normal file
@ -0,0 +1,85 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import HTTPException
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from starlette import status
|
||||
|
||||
from app.application.lens_issues_repository import LensIssuesRepository
|
||||
from app.application.lenses_repository import LensesRepository
|
||||
from app.application.patients_repository import PatientsRepository
|
||||
from app.application.users_repository import UsersRepository
|
||||
from app.domain.entities.lens_issues import LensIssueEntity
|
||||
from app.domain.models import LensIssue
|
||||
|
||||
|
||||
class LensIssuesService:
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.lens_issues_repository = LensIssuesRepository(db)
|
||||
self.patient_repository = PatientsRepository(db)
|
||||
self.users_repository = UsersRepository(db)
|
||||
self.lenses_repository = LensesRepository(db)
|
||||
|
||||
async def get_all_lens_issues(self) -> list[LensIssueEntity]:
|
||||
lens_issues = await self.lens_issues_repository.get_all()
|
||||
|
||||
return [
|
||||
self.model_to_entity(lens_issue)
|
||||
for lens_issue in lens_issues
|
||||
]
|
||||
|
||||
async def create_lens_issue(self, lens_issue: LensIssueEntity, user_id: int) -> LensIssueEntity:
|
||||
patient = await self.patient_repository.get_by_id(lens_issue.patient_id)
|
||||
|
||||
if not patient:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='The patient with this ID was not found',
|
||||
)
|
||||
|
||||
user = await self.users_repository.get_by_id(user_id)
|
||||
|
||||
if not user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='The user with this ID was not found',
|
||||
)
|
||||
|
||||
lens_issue.doctor_id = user_id
|
||||
|
||||
lens = await self.lenses_repository.get_by_id(lens_issue.lens_id)
|
||||
|
||||
if not lens:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='The lens with this ID was not found',
|
||||
)
|
||||
|
||||
lens_issue_model = self.entity_to_model(lens_issue)
|
||||
|
||||
await self.lens_issues_repository.create(lens_issue_model)
|
||||
|
||||
return self.model_to_entity(lens_issue_model)
|
||||
|
||||
@staticmethod
|
||||
def entity_to_model(lens_issue: LensIssueEntity) -> LensIssue:
|
||||
lens_issue_model = LensIssue(
|
||||
issue_date=lens_issue.issue_date,
|
||||
patient_id=lens_issue.patient_id,
|
||||
doctor_id=lens_issue.doctor_id,
|
||||
lens_id=lens_issue.lens_id,
|
||||
)
|
||||
|
||||
if lens_issue.id is not None:
|
||||
lens_issue_model.id = lens_issue.id
|
||||
|
||||
return lens_issue_model
|
||||
|
||||
@staticmethod
|
||||
def model_to_entity(lens_issue_model: LensIssue) -> LensIssueEntity:
|
||||
return LensIssueEntity(
|
||||
id=lens_issue_model.id,
|
||||
issue_date=lens_issue_model.issue_date,
|
||||
patient_id=lens_issue_model.patient_id,
|
||||
doctor_id=lens_issue_model.doctor_id,
|
||||
lens_id=lens_issue_model.lens_id,
|
||||
)
|
||||
@ -80,7 +80,6 @@ class LensesService:
|
||||
|
||||
@staticmethod
|
||||
def entity_to_model(lens: LensEntity) -> Lens:
|
||||
|
||||
try:
|
||||
side_enum = SideEnum(lens.side)
|
||||
except ValueError:
|
||||
@ -102,7 +101,7 @@ class LensesService:
|
||||
)
|
||||
|
||||
if lens.id is not None:
|
||||
lens.id = lens.id
|
||||
lens_model.id = lens.id
|
||||
|
||||
return lens_model
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ from fastapi import FastAPI
|
||||
from starlette.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.controllers.auth_router import router as auth_router
|
||||
from app.controllers.lens_issues_router import router as lens_issues_router
|
||||
from app.controllers.lens_types_router import router as lens_types_router
|
||||
from app.controllers.lenses_router import router as lenses_router
|
||||
from app.controllers.patients_router import router as patients_router
|
||||
@ -29,6 +30,7 @@ def start_app():
|
||||
api_app.include_router(lens_types_router, prefix=settings.APP_PREFIX, tags=['lens_types'])
|
||||
api_app.include_router(sets_router, prefix=settings.APP_PREFIX, tags=['sets'])
|
||||
api_app.include_router(set_content_router, prefix=settings.APP_PREFIX, tags=['set_content'])
|
||||
api_app.include_router(lens_issues_router, prefix=settings.APP_PREFIX, tags=['lens_issue'])
|
||||
|
||||
return api_app
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user