Skip to content

Commit

Permalink
update paginated get responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim2121512 committed Dec 17, 2024
1 parent 09c7357 commit 0e5c55b
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 13 deletions.
11 changes: 10 additions & 1 deletion backend/src/core/entities/review/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pydantic import BaseModel, Field
from typing import Optional
from src.core.entities.object_id_str import ObjectIdStr

from typing import List

class Review(BaseModel):
toolId: ObjectIdStr = Field(
Expand Down Expand Up @@ -108,3 +108,12 @@ class ReviewPaginated(ReviewSummary):
...,
description="Tool name"
)

class PaginatedReviewsResponse(BaseModel):
reviews: List[ReviewPaginated] = Field(
...
)
totalNumber: int = Field(
...
)

8 changes: 8 additions & 0 deletions backend/src/core/entities/users/worker/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ class WorkerPaginated(BaseModel):
date: datetime = Field(
default=None,
description="Employee start date",
)

class PaginatedWorkersResponse(BaseModel):
workers: List[WorkerPaginated] = Field(
...
)
totalNumber: int = Field(
...
)
11 changes: 11 additions & 0 deletions backend/src/core/repositories/review_repos/ireview_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ async def get_paginated_reviews(
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None
) -> List[Review]:
pass

@abstractmethod
async def count_reviews(
self,
tool_ids: Optional[List[str]] = None,
reviewer_ids: Optional[List[str]] = None,
rating: Optional[int] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None
) -> int:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ async def get_paginated_workers(
phone: Optional[str] = None,
jobTitle: Optional[str] = None
) -> List[WorkerPaginated]:
pass

@abstractmethod
async def count_workers(
self,
email: Optional[str] = None,
name: Optional[str] = None,
surname: Optional[str] = None,
phone: Optional[str] = None,
jobTitle: Optional[str] = None
) -> int:
pass
17 changes: 14 additions & 3 deletions backend/src/core/services/review_service/review_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from src.core.entities.review.review import ReviewCreate, ReviewCreated, ReviewSummary, Review, ReviewPaginated
from src.core.entities.review.review import ReviewCreate, ReviewCreated, ReviewSummary, Review, ReviewPaginated, PaginatedReviewsResponse
from typing import List, Optional
from src.core.exceptions.client_error import ResourceNotFoundError, ResourceAlreadyExistsError, PaymentStateError
from src.core.repositories.order_repos.iorder_repository import IOrderRepository
Expand Down Expand Up @@ -75,7 +75,7 @@ async def get_paginated_reviews(
rating: Optional[int] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None
) -> List[ReviewPaginated]:
) -> PaginatedReviewsResponse:
tool_ids = None
if tool_name:
tool_ids = await self.tool_repo.get_ids_by_name(tool_name)
Expand All @@ -97,6 +97,14 @@ async def get_paginated_reviews(
end_date=end_date
)

total_count = await self.review_repo.count_reviews(
tool_ids=tool_ids,
reviewer_ids=reviewer_ids,
rating=rating,
start_date=start_date,
end_date=end_date
)

result = []
for review in reviews:
tool_name = await self.tool_repo.get_name_by_id(objectId_to_str(review.toolId))
Expand All @@ -110,5 +118,8 @@ async def get_paginated_reviews(
tool_name=tool_name
))

return result
return PaginatedReviewsResponse(
reviews=result,
totalNumber=total_count
)

19 changes: 16 additions & 3 deletions backend/src/core/services/worker_service/worker_service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from src.core.entities.users.base_user import UpdateUser, UpdatedUser, UpdateUserPassword, UpdatedUserPassword
from src.core.entities.users.worker.worker import WorkerInDB, WorkerPrivateSummary, WorkerPaginated
from src.core.entities.users.worker.worker import WorkerInDB, WorkerPrivateSummary, WorkerPaginated, PaginatedWorkersResponse
from src.core.exceptions.client_error import ResourceNotFoundError, InvalidPasswordProvided
from src.core.repositories.users_repos.worker_repos.iworker_repository import IWorkerRepository
from typing import List, Optional
Expand Down Expand Up @@ -52,13 +52,26 @@ async def get_paginated_workers(
surname: Optional[str] = None,
phone: Optional[str] = None,
jobTitle: Optional[str] = None,
) -> List[WorkerPaginated]:
return await self.worker_repo.get_paginated_workers(
) -> PaginatedWorkersResponse:
total_count = await self.worker_repo.count_workers(
email=email,
name=name,
surname=surname,
phone=phone,
jobTitle=jobTitle
)

workers = await self.worker_repo.get_paginated_workers(
page=page,
page_size=page_size,
email=email,
name=name,
surname=surname,
phone=phone,
jobTitle=jobTitle
)

return PaginatedWorkersResponse(
workers=workers,
totalNumber=total_count
)
4 changes: 2 additions & 2 deletions backend/src/infrastructure/api/review_controller.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi import APIRouter, Depends, Query
from typing import List, Optional
from fastapi.security import OAuth2PasswordBearer
from src.core.entities.review.review import ReviewCreated, ReviewCreate, ReviewPaginated
from src.core.entities.review.review import ReviewCreated, ReviewCreate, ReviewPaginated, PaginatedReviewsResponse
from src.core.services.review_service.review_service import ReviewService
from src.infrastructure.api.security.role_required import is_self, is_worker
from src.infrastructure.services_instances import get_review_service
Expand All @@ -28,7 +28,7 @@ async def create_review(
@review_router.get(
path="/paginated",
status_code=200,
response_model=List[ReviewPaginated]
response_model=PaginatedReviewsResponse
)
async def get_reviews_paginated(
page: PositiveInt = Query(1),
Expand Down
8 changes: 4 additions & 4 deletions backend/src/infrastructure/api/worker_contoller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pydantic import PositiveInt

from src.core.entities.users.base_user import UpdatedUser, UpdateUser, UpdatedUserPassword, UpdateUserPassword
from src.core.entities.users.worker.worker import WorkerPrivateSummary, WorkerPaginated
from src.core.entities.users.worker.worker import WorkerPrivateSummary, WorkerPaginated, PaginatedWorkersResponse
from src.core.services.worker_service.worker_service import WorkerService
from src.infrastructure.api.security.role_required import is_worker, is_self
from src.infrastructure.services_instances import get_worker_service
Expand All @@ -17,7 +17,7 @@
@worker_router.get(
path="/paginated",
status_code=200,
response_model=List[WorkerPaginated]
response_model=PaginatedWorkersResponse
)
async def get_workers_paginated(
page: PositiveInt = Query(1),
Expand All @@ -28,9 +28,9 @@ async def get_workers_paginated(
phone: Optional[str] = Query(None),
jobTitle: Optional[str] = Query(None),
worker_service: WorkerService = Depends(get_worker_service),
#token: str = Depends(oauth2_scheme)
token: str = Depends(oauth2_scheme)
):
#is_worker(token)
is_worker(token)
return await worker_service.get_paginated_workers(
page=page,
page_size=page_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,34 @@ async def get_paginated_reviews(
return [Review(**review) for review in reviews]
except PyMongoError:
raise DatabaseError()


async def count_reviews(
self,
tool_ids: Optional[List[str]] = None,
reviewer_ids: Optional[List[str]] = None,
rating: Optional[int] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None
) -> int:
try:
filters = {}

if tool_ids is not None:
filters["toolId"] = {"$in": [str_to_objectId(tool_id) for tool_id in tool_ids]}
if reviewer_ids is not None:
filters["reviewerId"] = {"$in": [str_to_objectId(reviewer_id) for reviewer_id in reviewer_ids]}
if rating is not None:
filters["rating"] = rating
if start_date or end_date:
filters["date"] = {}
if start_date:
filters["date"]["$gte"] = start_date
if end_date:
filters["date"]["$lte"] = end_date

count = await self.review_collection.count_documents(filters)

return count
except PyMongoError:
raise DatabaseError()
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,33 @@ async def get_paginated_workers(

workers = await cursor.to_list(length=page_size)
return [WorkerPaginated(**worker) for worker in workers]
except PyMongoError:
raise DatabaseError()

async def count_workers(
self,
email: Optional[str] = None,
name: Optional[str] = None,
surname: Optional[str] = None,
phone: Optional[str] = None,
jobTitle: Optional[str] = None
) -> int:
try:
filters = {}

if email:
filters['email'] = {"$regex": email, "$options": "i"}
if name:
filters['name'] = {"$regex": name, "$options": "i"}
if surname:
filters['surname'] = {"$regex": surname, "$options": "i"}
if phone:
filters['phone'] = {"$regex": phone, "$options": "i"}
if jobTitle:
filters['jobTitle'] = {"$regex": jobTitle, "$options": "i"}

count = await self.worker_collection.count_documents(filters)

return count
except PyMongoError:
raise DatabaseError()

0 comments on commit 0e5c55b

Please sign in to comment.