Skip to content

Commit

Permalink
ResponseDTO 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
riroan committed Aug 24, 2024
1 parent b33edad commit 0eac125
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 40 deletions.
16 changes: 8 additions & 8 deletions dto/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)


Expand Down
16 changes: 16 additions & 0 deletions dto/like.py
Original file line number Diff line number Diff line change
@@ -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
)
18 changes: 9 additions & 9 deletions dto/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion orm/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))

Expand Down
22 changes: 16 additions & 6 deletions presentation/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 9 additions & 4 deletions presentation/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
12 changes: 9 additions & 3 deletions presentation/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
10 changes: 6 additions & 4 deletions service/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -37,23 +37,25 @@ 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)
obj = repository.get_by_id(command.cart_id)
if obj is None:
raise NotFoundException
obj.count = command.count
return obj


async def delete_cart(
Expand Down
6 changes: 4 additions & 2 deletions service/like.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -44,3 +45,4 @@ async def dislike(
repository.add(obj)
elif obj.is_like == LikeStatus.LIKE:
obj.is_like = LikeStatus.DISLIKE
return obj
8 changes: 5 additions & 3 deletions service/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0eac125

Please sign in to comment.