Skip to content

Commit

Permalink
product 조회 시 추가 정보 제공
Browse files Browse the repository at this point in the history
  • Loading branch information
riroan committed Aug 24, 2024
1 parent 6b84202 commit fdc39f8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
18 changes: 16 additions & 2 deletions dto/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
10 changes: 5 additions & 5 deletions presentation/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions repository/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
29 changes: 24 additions & 5 deletions service/product.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down

0 comments on commit fdc39f8

Please sign in to comment.