diff --git a/dto/product.py b/dto/product.py index 714636b..0f09163 100644 --- a/dto/product.py +++ b/dto/product.py @@ -12,15 +12,29 @@ class ProductDto(BaseModel): class ProductResponseDto(ProductDto): id: int + like_count: int + dislike_count: int + is_like: bool + is_dislike: bool @staticmethod - def from_entity(product: Product): + def from_entity( + product: Product, + like_count: int, + dislike_count: int, + is_like: bool, + is_dislike: bool, + ): return ProductResponseDto( id=product.id, name=product.name, image_path=product.image_path, price=product.price, - summary=product.summary + summary=product.summary, + like_count=like_count, + dislike_count=dislike_count, + is_like=is_like, + is_dislike=is_dislike, ) diff --git a/presentation/product.py b/presentation/product.py index 0883fa7..5dc29eb 100644 --- a/presentation/product.py +++ b/presentation/product.py @@ -10,15 +10,15 @@ api = APIRouter() -@api.get("/{product_id}", status_code=status.HTTP_200_OK) +@api.get("/{product_id}/{user_id}", status_code=status.HTTP_200_OK) async def show_product( product_id: int, + user_id: int, session: Annotated[Session, Depends(get_session)] -): - product = await product_service.show_product( - product_id, session +) -> ProductResponseDto: + return await product_service.show_product( + product_id, user_id, session ) - return product @api.get("", status_code=status.HTTP_200_OK) diff --git a/repository/like.py b/repository/like.py index dcd56a5..762d7d3 100644 --- a/repository/like.py +++ b/repository/like.py @@ -16,3 +16,13 @@ def get_by_user_id_and_product_id( self.model.user_id == user_id, self.model.product_id == product_id ).first() + + def get_by_product_id( + self, + product_id: int + ) -> list[Like]: + return self.session.query( + self.model + ).filter( + self.model.product_id == product_id + ).all() diff --git a/service/product.py b/service/product.py index 8fcb5a3..ad2638c 100644 --- a/service/product.py +++ b/service/product.py @@ -1,19 +1,38 @@ from sqlalchemy.orm.session import Session -from dto.product import ProductDto +from const import LikeStatus +from dto.product import ProductDto, ProductResponseDto from orm.product import Product +from repository.like import LikeRepository from repository.product import ProductRepository async def show_product( product_id: int, + user_id: int, session: Session, ): - repository = ProductRepository(session) - product = repository.get_greater_than_id(product_id) + product_repository = ProductRepository(session) + like_repository = LikeRepository(session) + product = product_repository.get_greater_than_id(product_id) if product is None: - product = repository.get_greater_than_id(0) - return product + product = product_repository.get_greater_than_id(0) + + likes = like_repository.get_by_product_id(product.id) + like_user_ids = [ + like.user_id for like in likes if like.is_like == LikeStatus.LIKE + ] + dislike_user_ids = [ + like.user_id for like in likes if like.is_like == LikeStatus.DISLIKE + ] + + return ProductResponseDto.from_entity( + product=product, + like_count=len(like_user_ids), + dislike_count=len(dislike_user_ids), + is_like=user_id in like_user_ids, + is_dislike=user_id in dislike_user_ids + ) async def list_product(