diff --git a/api/app/application/lenses_repository.py b/api/app/application/lenses_repository.py index f087fdd..f8f14ae 100644 --- a/api/app/application/lenses_repository.py +++ b/api/app/application/lenses_repository.py @@ -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) diff --git a/api/app/controllers/lenses_router.py b/api/app/controllers/lenses_router.py index b83a081..7eaafa6 100644 --- a/api/app/controllers/lenses_router.py +++ b/api/app/controllers/lenses_router.py @@ -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, diff --git a/api/app/infrastructure/lens_issues_service.py b/api/app/infrastructure/lens_issues_service.py index e209377..ac1b9be 100644 --- a/api/app/infrastructure/lens_issues_service.py +++ b/api/app/infrastructure/lens_issues_service.py @@ -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 diff --git a/api/app/infrastructure/lenses_service.py b/api/app/infrastructure/lenses_service.py index c499dca..3c18881 100644 --- a/api/app/infrastructure/lenses_service.py +++ b/api/app/infrastructure/lenses_service.py @@ -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)