Skip to content

Commit

Permalink
add review & worker paginated get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim2121512 committed Dec 16, 2024
1 parent b1734e0 commit d62f9a8
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 31 deletions.
6 changes: 6 additions & 0 deletions backend/src/core/entities/review/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ class ReviewSummary(BaseModel):
min_length=4,
max_length=300
)

class ReviewPaginated(ReviewSummary):
tool_name: str = Field(
...,
description="Tool name"
)
31 changes: 30 additions & 1 deletion backend/src/core/entities/users/worker/worker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

from pydantic import Field
from pydantic import Field, BaseModel, EmailStr
from datetime import datetime
from typing import List, Optional

Expand Down Expand Up @@ -48,6 +48,35 @@ class WorkerPrivateSummary(BaseUserPrivateSummary):
...,
description="Worker's job title",
)
date: datetime = Field(
default=None,
description="Employee start date",
)

class WorkerPaginated(BaseModel):
name: str = Field(
...,
description="Worker's first name"
)
surname: str = Field(
...,
description="Worker's last name"
)
phone: str = Field(
...,
description="Worker's phone number",
pattern=r'^\+?[1-9]\d{1,14}$'
)
email: EmailStr = Field(
...,
description="User's email",
min_length=1,
max_length=100,
)
jobTitle: str = Field(
...,
description="Worker's job title"
)
date: datetime = Field(
default=None,
description="Employee start date",
Expand Down
14 changes: 14 additions & 0 deletions backend/src/core/repositories/review_repos/ireview_repository.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
from src.core.entities.review.review import ReviewCreate, ReviewCreated, ReviewSummary, Review
from typing import List, Optional
from datetime import datetime

class IReviewRepository(ABC):
@abstractmethod
Expand All @@ -13,4 +14,17 @@ async def get_reviews_by_tool_id(self, tool_id: str) -> List[Review]:

@abstractmethod
async def exists(self, tool_id: str, reviewer_id: str) -> bool:
pass

@abstractmethod
async def get_paginated_reviews(
self,
page: int,
page_size: int,
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
) -> List[Review]:
pass
8 changes: 8 additions & 0 deletions backend/src/core/repositories/tool_repos/itool_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ async def search(
@abstractmethod
async def exists_by_id(self, tool_id: str) -> bool:
pass

@abstractmethod
async def get_name_by_id(self, tool_id: str) -> Optional[str]:
pass

@abstractmethod
async def get_ids_by_name(self, name: str) -> List[str]:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ async def get_password_by_id(self, client_id: str) -> str:

@abstractmethod
async def get_full_name(self, client_id: str) -> ClientFullName:
pass

@abstractmethod
async def get_ids_by_fullname(self, name: Optional[str], surname: Optional[str]) -> List[str]:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from src.core.entities.object_id_str import ObjectIdStr
from src.core.entities.users.base_user import UpdateUser, UpdatedUser, UpdatedUserPassword
from src.core.entities.users.worker.worker import Worker, WorkerInDB, WorkerPrivateSummary
from src.core.entities.users.worker.worker import Worker, WorkerInDB, WorkerPrivateSummary, WorkerPaginated


class IWorkerRepository(ABC):
Expand Down Expand Up @@ -45,4 +45,17 @@ async def update_password(self, worker_id: str, new_password: str) -> UpdatedUse

@abstractmethod
async def get_password_by_id(self, worker_id: str) -> str:
pass

@abstractmethod
async def get_paginated_workers(
self,
page: int,
page_size: int,
email: Optional[str] = None,
name: Optional[str] = None,
surname: Optional[str] = None,
phone: Optional[str] = None,
jobTitle: Optional[str] = None
) -> List[WorkerPaginated]:
pass
56 changes: 52 additions & 4 deletions backend/src/core/services/review_service/review_service.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from src.core.entities.review.review import ReviewCreate, ReviewCreated, ReviewSummary, Review
from typing import List
from src.core.entities.review.review import ReviewCreate, ReviewCreated, ReviewSummary, Review, ReviewPaginated
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
from src.core.repositories.review_repos.ireview_repository import IReviewRepository
from src.core.repositories.tool_repos.itool_repository import IToolRepository
from src.core.repositories.users_repos.client_repos.iclient_repository import IClientRepository
from src.infrastructure.repo_implementations.helpers.id_mapper import objectId_to_str, str_to_objectId

from datetime import datetime

class ReviewService:
def __init__(self, review_repo: IReviewRepository, tool_repo: IToolRepository, client_repo: IClientRepository, order_repo: IOrderRepository):
Expand Down Expand Up @@ -63,4 +63,52 @@ async def get_tool_reviews(self, tool_id: str) -> List[ReviewSummary]:
)
review_summaries.append(review_summary)

return review_summaries
return review_summaries

async def get_paginated_reviews(
self,
page: int,
page_size: int,
tool_name: Optional[str] = None,
reviewer_name: Optional[str] = None,
reviewer_surname: Optional[str] = None,
rating: Optional[int] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None
) -> List[ReviewPaginated]:
tool_ids = None
if tool_name:
tool_ids = await self.tool_repo.get_ids_by_name(tool_name)

reviewer_ids = None
if reviewer_name or reviewer_surname:
reviewer_ids = await self.client_repo.get_ids_by_fullname(
name=reviewer_name,
surname=reviewer_surname
)

reviews = await self.review_repo.get_paginated_reviews(
page=page,
page_size=page_size,
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))
full_name = await self.client_repo.get_full_name(objectId_to_str(review.reviewerId))
result.append(ReviewPaginated(
reviewer_name=full_name.name,
reviewer_surname=full_name.surname,
rating=review.rating,
date=review.date,
text=review.text,
tool_name=tool_name
))

return result

26 changes: 23 additions & 3 deletions backend/src/core/services/worker_service/worker_service.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from src.core.entities.users.base_user import UpdateUser, UpdatedUser, UpdateUserPassword, UpdatedUserPassword
from src.core.entities.users.worker.worker import WorkerInDB, WorkerPrivateSummary
from src.core.entities.users.worker.worker import WorkerInDB, WorkerPrivateSummary, WorkerPaginated
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
from typing import List, Optional
from src.configs.paths import Paths
from src.configs.urls import Urls
from src.core.utils.image_decoder.image_decoder import ImageDecoder
Expand Down Expand Up @@ -41,4 +41,24 @@ async def get_private_worker_summary(self, worker_id: str) -> WorkerPrivateSumma
if not await self.worker_repo.exists_by_id(worker_id):
raise ResourceNotFoundError("The client with provided id does not exist", details={"id": worker_id})

return await self.worker_repo.get_private_summary_by_id(worker_id)
return await self.worker_repo.get_private_summary_by_id(worker_id)

async def get_paginated_workers(
self,
page: int,
page_size: int,
email: Optional[str] = None,
name: Optional[str] = None,
surname: Optional[str] = None,
phone: Optional[str] = None,
jobTitle: Optional[str] = None,
) -> List[WorkerPaginated]:
return await self.worker_repo.get_paginated_workers(
page=page,
page_size=page_size,
email=email,
name=name,
surname=surname,
phone=phone,
jobTitle=jobTitle
)
43 changes: 38 additions & 5 deletions backend/src/infrastructure/api/review_controller.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from fastapi import APIRouter, Depends
from typing import List
from fastapi import APIRouter, Depends, Query
from typing import List, Optional
from fastapi.security import OAuth2PasswordBearer
from src.core.entities.review.review import ReviewSummary, ReviewCreated, ReviewCreate
from src.core.entities.review.review import ReviewCreated, ReviewCreate, ReviewPaginated
from src.core.services.review_service.review_service import ReviewService
from src.infrastructure.api.security.role_required import is_self
from src.infrastructure.api.security.role_required import is_self, is_worker
from src.infrastructure.services_instances import get_review_service
from pydantic import PositiveInt
from datetime import datetime

review_router = APIRouter()

Expand All @@ -23,14 +25,45 @@ async def create_review(
is_self(token, str(data.reviewerId))
return await review_service.create_review(data)

@review_router.get(
path="/paginated",
status_code=200,
response_model=List[ReviewPaginated]
)
async def get_reviews_paginated(
page: PositiveInt = Query(1),
page_size: PositiveInt = Query(12),
tool_name: Optional[str] = Query(None),
reviewer_name: Optional[str] = Query(None),
reviewer_surname: Optional[str] = Query(None),
rating: Optional[PositiveInt] = Query(None),
start_date: Optional[datetime] = Query(None),
end_date: Optional[datetime] = Query(None),
review_service: ReviewService = Depends(get_review_service),
token: str = Depends(oauth2_scheme)
):
is_worker(token)
return await review_service.get_paginated_reviews(
page=page,
page_size=page_size,
tool_name=tool_name,
reviewer_name=reviewer_name,
reviewer_surname=reviewer_surname,
rating=rating,
start_date=start_date,
end_date=end_date
)


@review_router.get(
path="/{tool_id}",
status_code=200,
response_model=List[ReviewSummary]
response_model=List[ReviewPaginated]
)
async def get_tool_reviews(
tool_id: str,
review_service: ReviewService = Depends(get_review_service)
):
return await review_service.get_tool_reviews(tool_id)


37 changes: 27 additions & 10 deletions backend/src/infrastructure/api/worker_contoller.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
from fastapi import APIRouter, Depends
from typing import List
from fastapi import APIRouter, Depends, Query
from typing import List, Optional
from pydantic import PositiveInt

from src.core.entities.users.base_user import UpdatedUser, UpdateUser, UpdatedUserPassword, UpdateUserPassword
from src.core.entities.users.worker.worker import WorkerInDB, WorkerPrivateSummary
from src.core.entities.users.worker.worker import WorkerPrivateSummary, WorkerPaginated
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
from fastapi.security import OAuth2PasswordBearer


worker_router = APIRouter()

oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")

@worker_router.get(
path="/",
path="/paginated",
status_code=200,
response_model=List[WorkerPrivateSummary]
response_model=List[WorkerPaginated]
)
async def get_all_workers(
async def get_workers_paginated(
page: PositiveInt = Query(1),
page_size: PositiveInt = Query(12),
email: Optional[str] = Query(None),
name: Optional[str] = Query(None),
surname: Optional[str] = Query(None),
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)
return await worker_service.get_all_workers_summary()
#is_worker(token)
return await worker_service.get_paginated_workers(
page=page,
page_size=page_size,
email=email,
name=name,
surname=surname,
phone=phone,
jobTitle=jobTitle
)


@worker_router.get(
Expand Down
Loading

0 comments on commit d62f9a8

Please sign in to comment.