Skip to content

Commit

Permalink
some fixes & add test data
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim2121512 committed Dec 18, 2024
1 parent f5f474d commit 6a23551
Show file tree
Hide file tree
Showing 14 changed files with 1,507 additions and 101 deletions.
30 changes: 30 additions & 0 deletions backend/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: '3.8'

services:
test_db:
container_name: test_mongodb
hostname: test_mongodb
build:
context: ../db
dockerfile: Dockerfile
image: test_mongodb_app:0.8
env_file:
- env/test/.env.test.db
- env/.env.collections
networks:
- test_network

test_backend:
build:
context: .
dockerfile: Dockerfile.test
container_name: test_backend
image: test_backend_app:0.8
depends_on:
- test_db
networks:
- test_network

networks:
test_network:
driver: bridge
7 changes: 3 additions & 4 deletions backend/src/core/entities/tool/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ class Tool(BaseModel):
default=None,
description="Tool features dictionary"
)
reviews: List[ObjectIdStr] = Field(
default=None,
description="List of review identifiers for this tool from the review collection"
)
rating: float = Field(
default=0.0,
description="Average tool rating"
)
reviews_count: int = Field(
default=0
)
ordersNumber: int = Field(
default=0,
description="Total number of orders"
Expand Down
2 changes: 1 addition & 1 deletion backend/src/core/entities/users/base_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class BaseUserPrivateSummary(BaseModel):
...,
description="User's last name",
)
phone: str = Field(
phone: Optional[str] = Field(
...,
description="User's phone number",
)
Expand Down
4 changes: 4 additions & 0 deletions backend/src/core/repositories/tool_repos/itool_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ async def count_tools(

@abstractmethod
async def get_tools_summaries_by_ids(self, tool_ids: List[str]) -> List[ToolSummary]:
pass

@abstractmethod
async def update_rating(self, tool_id: str, new_rating: float) -> None:
pass
1 change: 1 addition & 0 deletions backend/src/core/services/order_service/order_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def create_order(self, order: OrderCreate) -> OrderCreated:
if not await self.tool_repo.exists(tool.name):
raise ResourceNotFoundError("Provided tool doesn't exist", details={"tool name": tool.name})
tools = [await self.tool_repo.get_tool_by_id(tool_id) for tool_id in order.tools]

new_order = Order(
tools=list(map(str_to_objectId, order.tools)),
start_leasing=order.start_leasing,
Expand Down
1 change: 1 addition & 0 deletions backend/src/core/services/review_service/review_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async def create_review(self, review: ReviewCreate) -> ReviewCreated:
)
)

await self.tool_repo.update_rating(tool_id=review.toolId, new_rating=review.rating)

return ReviewCreated(
review_id=review_id
Expand Down
3 changes: 1 addition & 2 deletions backend/src/infrastructure/api/order_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ async def get_paginated_orders_for_worker(
)
async def get_order(
order_id: str,
order_service: OrderService = Depends(get_order_service),
token: str = Depends(oauth2_scheme)
order_service: OrderService = Depends(get_order_service)
):
return await order_service.get_order_by_id(order_id)
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def search(
pipeline = [
match_stage,
{"$addFields": {"score": {"$meta": "textScore"}}},
{"$sort": {"score": -1}},
{"$sort": {"score": -1, "_id": 1}},
{"$skip": skip},
{"$limit": page_size},
{"$project": {
Expand Down Expand Up @@ -219,5 +219,19 @@ async def get_tools_summaries_by_ids(self, tool_ids: List[str]) -> List[ToolSumm
results.append(ToolSummary(**doc))

return results
except PyMongoError:
raise DatabaseError()

async def update_rating(self, tool_id: str, new_rating: float) -> None:
try:
tool_data = await self.tool_collection.find_one({"_id": str_to_objectId(tool_id)}, {"rating": 1, "reviews_count": 1})

updated_reviews_count = tool_data["reviews_count"] + 1
updated_rating = (tool_data["rating"] * tool_data["reviews_count"] + new_rating) / updated_reviews_count

await self.tool_collection.update_one(
{"_id": str_to_objectId(tool_id)},
{"$set": {"rating": updated_rating, "reviews_count": updated_reviews_count}}
)
except PyMongoError:
raise DatabaseError()
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ async def test_create_tool_success(tool_repo, tool):
assert saved_tool["totalPrice"] == tool.totalPrice
assert saved_tool["images"] == tool.images
assert saved_tool["features"] == tool.features
assert saved_tool["reviews"] == tool.reviews
assert saved_tool["rating"] == tool.rating
assert saved_tool["ordersNumber"] == tool.ordersNumber
assert saved_tool["category"] == tool.category
Expand Down
5 changes: 5 additions & 0 deletions db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM mongo:7.0

COPY init-mongo.js /docker-entrypoint-initdb.d/

CMD ["mongod"]
Loading

0 comments on commit 6a23551

Please sign in to comment.