From 0eac125df5b81e0efd718ee0b459a0b4b2301847 Mon Sep 17 00:00:00 2001 From: riroan Date: Sun, 25 Aug 2024 03:37:11 +0900 Subject: [PATCH] =?UTF-8?q?ResponseDTO=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dto/cart.py | 16 ++++++++-------- dto/like.py | 16 ++++++++++++++++ dto/product.py | 18 +++++++++--------- orm/like.py | 2 +- presentation/cart.py | 22 ++++++++++++++++------ presentation/like.py | 13 +++++++++---- presentation/product.py | 12 +++++++++--- service/cart.py | 10 ++++++---- service/like.py | 6 ++++-- service/product.py | 8 +++++--- 10 files changed, 83 insertions(+), 40 deletions(-) diff --git a/dto/cart.py b/dto/cart.py index 04ecfe9..d6fd2b7 100644 --- a/dto/cart.py +++ b/dto/cart.py @@ -13,21 +13,21 @@ class CartUpdateDto(BaseModel): class CartResponseDto(BaseModel): - id: int - name: str - image_path: str - price: int + id: int | None count: int + name: str | None + image_path: str | None + price: int | None @staticmethod def from_entity(cart: Cart): product = cart.product return CartResponseDto( id=cart.id, - name=product.name, - image_path=product.image_path, - price=product.price, - count=cart.count + count=cart.count, + name=product and product.name, + image_path=product and product.image_path, + price=product and product.price, ) diff --git a/dto/like.py b/dto/like.py index 7aa17da..0a166e8 100644 --- a/dto/like.py +++ b/dto/like.py @@ -1,6 +1,22 @@ from pydantic import BaseModel +from orm.like import Like + class LikeDto(BaseModel): user_id: int product_id: int + + +class LikeResponseDto(BaseModel): + is_like: int + user_id: int + product_id: int + + @staticmethod + def from_entity(like: Like): + return LikeResponseDto( + is_like=like.is_like, + user_id=like.user_id, + product_id=like.product_id + ) diff --git a/dto/product.py b/dto/product.py index 3d28458..82ccd51 100644 --- a/dto/product.py +++ b/dto/product.py @@ -11,19 +11,19 @@ class ProductDto(BaseModel): class ProductResponseDto(ProductDto): - id: int - like_count: int - dislike_count: int - is_like: bool - is_dislike: bool + id: int | None + like_count: int | None + dislike_count: int | None + is_like: bool | None + is_dislike: bool | None @staticmethod def from_entity( product: Product, - like_count: int = 0, - dislike_count: int = 0, - is_like: bool = False, - is_dislike: bool = False, + like_count: int | None = None, + dislike_count: int | None = None, + is_like: bool | None = None, + is_dislike: bool | None = None, ): return ProductResponseDto( id=product.id, diff --git a/orm/like.py b/orm/like.py index 78629c2..d1b4b4c 100644 --- a/orm/like.py +++ b/orm/like.py @@ -7,7 +7,7 @@ class Like(Base): __tablename__ = "like_history" - is_like: Mapped[str] = mapped_column(Integer) + is_like: Mapped[int] = mapped_column(Integer) user_id: Mapped[int] = mapped_column(Integer, ForeignKey("user.id")) product_id: Mapped[int] = mapped_column(Integer, ForeignKey("product.id")) diff --git a/presentation/cart.py b/presentation/cart.py index eee0a18..d8ae401 100644 --- a/presentation/cart.py +++ b/presentation/cart.py @@ -33,20 +33,30 @@ async def list_cart( ) -@api.post("", status_code=status.HTTP_201_CREATED) +@api.post( + "", + status_code=status.HTTP_201_CREATED, + response_model_exclude_none=True +) async def add_cart( command: Annotated[AddCartCommand, Depends(add_cart_command)], messagebus: Annotated[MessageBus, Depends(get_messagebus)] -): - return await messagebus.handle(command) +) -> CartResponseDto: + cart = await messagebus.handle(command) + return CartResponseDto.from_entity(cart) -@api.put("/{cart_id}", status_code=status.HTTP_202_ACCEPTED) +@api.put( + "/{cart_id}", + status_code=status.HTTP_202_ACCEPTED, + response_model_exclude_none=True +) async def update_cart( command: Annotated[UpdateCartCommand, Depends(update_cart_command)], messagebus: Annotated[MessageBus, Depends(get_messagebus)] -): - return await messagebus.handle(command) +) -> CartResponseDto: + cart = await messagebus.handle(command) + return CartResponseDto.from_entity(cart) @api.delete("/{cart_id}", status_code=status.HTTP_204_NO_CONTENT) diff --git a/presentation/like.py b/presentation/like.py index b8c1208..6f09b44 100644 --- a/presentation/like.py +++ b/presentation/like.py @@ -5,6 +5,7 @@ from command.like import DislikeCommand, LikeCommand from depends import get_messagebus from depends.like import dislike_command, like_command +from dto.like import LikeResponseDto from messagebus import MessageBus api = APIRouter() @@ -14,13 +15,17 @@ async def like( command: Annotated[LikeCommand, Depends(like_command)], messagebus: Annotated[MessageBus, Depends(get_messagebus)] -): - return await messagebus.handle(command) +) -> LikeResponseDto: + like_obj = await messagebus.handle(command) + + return LikeResponseDto.from_entity(like_obj) @api.post("/dislike", status_code=status.HTTP_201_CREATED) async def dislike( command: Annotated[DislikeCommand, Depends(dislike_command)], messagebus: Annotated[MessageBus, Depends(get_messagebus)] -): - return await messagebus.handle(command) +) -> LikeResponseDto: + like_obj = await messagebus.handle(command) + + return LikeResponseDto.from_entity(like_obj) diff --git a/presentation/product.py b/presentation/product.py index dc48d69..ffd2ce9 100644 --- a/presentation/product.py +++ b/presentation/product.py @@ -38,9 +38,15 @@ async def list_product( return ProductResponseModel(products=product_responses) -@api.post("", status_code=status.HTTP_201_CREATED) +@api.post( + "", + status_code=status.HTTP_201_CREATED, + response_model_exclude_none=True +) async def add_product( command: Annotated[AddProductCommand, Depends(add_product_command)], messagebus: Annotated[MessageBus, Depends(get_messagebus)] -): - return await messagebus.handle(command) +) -> ProductResponseDto: + product = await messagebus.handle(command) + + return ProductResponseDto.from_entity(product) diff --git a/service/cart.py b/service/cart.py index fdaf0de..47385a4 100644 --- a/service/cart.py +++ b/service/cart.py @@ -9,7 +9,7 @@ async def list_cart( user_id: int, session: Session -): +) -> list[Cart]: repository = CartRepository(session) cart_products = repository.get_by_user_id(user_id) @@ -19,7 +19,7 @@ async def list_cart( async def add_cart( command: AddCartCommand, session: Session -): +) -> Cart: repository = CartRepository(session) obj = repository.get_by_user_id_and_product_id( user_id=command.user_id, @@ -37,16 +37,17 @@ async def add_cart( cart_id=obj.id, count=obj.count + 1 ) - await update_cart( + obj = await update_cart( update_command, session ) + return obj async def update_cart( command: UpdateCartCommand, session: Session -): +) -> Cart: if command.count <= 0: raise BadRequestException repository = CartRepository(session) @@ -54,6 +55,7 @@ async def update_cart( if obj is None: raise NotFoundException obj.count = command.count + return obj async def delete_cart( diff --git a/service/like.py b/service/like.py index cc3195a..2712e80 100644 --- a/service/like.py +++ b/service/like.py @@ -9,7 +9,7 @@ async def like( command: LikeCommand, session: Session -): +) -> Like: repository = LikeRepository(session) obj = repository.get_by_user_id_and_product_id( user_id=command.user_id, @@ -24,12 +24,13 @@ async def like( repository.add(obj) elif obj.is_like == LikeStatus.DISLIKE: obj.is_like = LikeStatus.LIKE + return obj async def dislike( command: DislikeCommand, session: Session -): +) -> Like: repository = LikeRepository(session) obj = repository.get_by_user_id_and_product_id( user_id=command.user_id, @@ -44,3 +45,4 @@ async def dislike( repository.add(obj) elif obj.is_like == LikeStatus.LIKE: obj.is_like = LikeStatus.DISLIKE + return obj diff --git a/service/product.py b/service/product.py index dcefac4..0bedda9 100644 --- a/service/product.py +++ b/service/product.py @@ -48,12 +48,14 @@ async def list_product( async def add_product( command: AddProductCommand, session: Session -): +) -> Product: repository = ProductRepository(session) - obj = Product( + product = Product( name=command.name, image_path=command.image_path, price=command.price, summary=command.summary ) - repository.add(obj) + repository.add(product) + + return product