сделал получение всех невыданных линз

This commit is contained in:
Андрей Дувакин 2025-03-10 07:20:20 +05:00
parent 9190723abd
commit e539a9a37b
4 changed files with 37 additions and 0 deletions

View File

@ -15,6 +15,11 @@ class LensesRepository:
result = await self.db.execute(stmt)
return result.scalars().all()
async def get_all_not_issued(self) -> Sequence[Lens]:
stmt = select(Lens).filter(Lens.issued == False)
result = await self.db.execute(stmt)
return result.scalars().all()
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)

View File

@ -23,6 +23,20 @@ async def get_all_lenses(
return await lenses_service.get_all_lenses()
@router.get(
"/lenses/not_issued/",
response_model=list[LensEntity],
summary="Get all not issued lenses",
description="Returns a list of all not issued lenses",
)
async def get_all_not_issued_lenses(
db: AsyncSession = Depends(get_db),
user=Depends(get_current_user),
):
lenses_service = LensesService(db)
return await lenses_service.get_all_not_issued_lenses()
@router.post(
"/lenses/",
response_model=LensEntity,

View File

@ -55,10 +55,20 @@ class LensIssuesService:
detail='The lens with this ID was not found',
)
if lens.issued:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail='The lens is already issued',
)
lens_issue_model = self.entity_to_model(lens_issue)
await self.lens_issues_repository.create(lens_issue_model)
lens.issued = True
await self.lenses_repository.update(lens)
return self.model_to_entity(lens_issue_model)
@staticmethod

View File

@ -24,6 +24,14 @@ class LensesService:
for lens in lenses
]
async def get_all_not_issued_lenses(self) -> list[LensEntity]:
lenses = await self.lenses_repository.get_all_not_issued()
return [
self.model_to_entity(lens)
for lens in lenses
]
async def create_lens(self, lens: LensEntity) -> LensEntity:
lens_type = await self.lens_types_repository.get_by_id(lens.type_id)