-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
237 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Command: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from dataclasses import dataclass | ||
|
||
from command import Command | ||
|
||
|
||
@dataclass(frozen=True) | ||
class AddCartCommand(Command): | ||
user_id: int | ||
product_id: int | ||
|
||
|
||
@dataclass(frozen=True) | ||
class UpdateCartCommand(Command): | ||
cart_id: int | ||
count: int | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DeleteCartCommand(Command): | ||
cart_id: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from dataclasses import dataclass | ||
|
||
from command import Command | ||
|
||
|
||
@dataclass(frozen=True) | ||
class LikeCommand(Command): | ||
user_id: int | ||
product_id: int | ||
|
||
|
||
@dataclass(frozen=True) | ||
class DislikeCommand(Command): | ||
user_id: int | ||
product_id: int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from dataclasses import dataclass | ||
|
||
from command import Command | ||
|
||
|
||
@dataclass(frozen=True) | ||
class AddProductCommand(Command): | ||
name: str | ||
image_path: str | ||
price: int | ||
summary: str | None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from command.cart import AddCartCommand, DeleteCartCommand, UpdateCartCommand | ||
from dto.cart import CartDto, CartUpdateDto | ||
|
||
|
||
async def add_cart_command(cart: CartDto): | ||
return AddCartCommand( | ||
user_id=cart.user_id, | ||
product_id=cart.product_id | ||
) | ||
|
||
|
||
async def update_cart_command(cart: CartUpdateDto, cart_id: int): | ||
return UpdateCartCommand( | ||
cart_id=cart_id, | ||
count=cart.count | ||
) | ||
|
||
|
||
async def delete_cart_command(cart_id: int): | ||
return DeleteCartCommand( | ||
cart_id=cart_id | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from command.like import DislikeCommand, LikeCommand | ||
from dto.like import LikeDto | ||
|
||
|
||
async def like_command(like_data: LikeDto): | ||
return LikeCommand( | ||
user_id=like_data.user_id, | ||
product_id=like_data.product_id | ||
) | ||
|
||
|
||
async def dislike_command(like_data: LikeDto): | ||
return DislikeCommand( | ||
user_id=like_data.user_id, | ||
product_id=like_data.product_id | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from command.product import AddProductCommand | ||
from dto.product import ProductDto | ||
|
||
|
||
async def add_product_command(product: ProductDto): | ||
return AddProductCommand( | ||
name=product.name, | ||
image_path=product.image_path, | ||
price=product.price, | ||
summary=product.summary | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from collections import deque | ||
|
||
from sqlalchemy.orm.session import Session | ||
|
||
from command import Command | ||
from command.cart import AddCartCommand, DeleteCartCommand, UpdateCartCommand | ||
from command.like import DislikeCommand, LikeCommand | ||
from command.product import AddProductCommand | ||
from service.cart import add_cart, delete_cart, update_cart | ||
from service.like import dislike, like | ||
from service.product import add_product | ||
|
||
Message = Command | ||
|
||
|
||
class MessageBus: | ||
def __init__(self, session: Session): | ||
self.session = session | ||
self.message_queue = None | ||
self.command_handlers = COMMAND_HANDLERS | ||
|
||
async def handle(self, message: Message): | ||
self.message_queue = deque([message]) | ||
while self.message_queue: | ||
target = self.message_queue.popleft() | ||
ret = await self.handle_command(target) | ||
return ret | ||
|
||
async def handle_command(self, command: Command): | ||
handler = self.command_handlers[type(command)] | ||
return await handler(command, self.session) | ||
|
||
|
||
COMMAND_HANDLERS = { | ||
AddCartCommand: add_cart, | ||
UpdateCartCommand: update_cart, | ||
DeleteCartCommand: delete_cart, | ||
LikeCommand: like, | ||
DislikeCommand: dislike, | ||
AddProductCommand: add_product, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,26 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, status | ||
from sqlalchemy.orm.session import Session | ||
|
||
from depends import get_session | ||
from dto.like import LikeDto | ||
from service import like as like_service | ||
from command.like import DislikeCommand, LikeCommand | ||
from depends import get_messagebus | ||
from depends.like import dislike_command, like_command | ||
from messagebus import MessageBus | ||
|
||
api = APIRouter() | ||
|
||
|
||
@api.post("/like", status_code=status.HTTP_201_CREATED) | ||
async def like( | ||
like_data: LikeDto, | ||
session: Annotated[Session, Depends(get_session)] | ||
) -> LikeDto: | ||
await like_service.like(like_data, session) | ||
|
||
return like_data | ||
command: Annotated[LikeCommand, Depends(like_command)], | ||
messagebus: Annotated[MessageBus, Depends(get_messagebus)] | ||
): | ||
return await messagebus.handle(command) | ||
|
||
|
||
@api.post("/dislike", status_code=status.HTTP_201_CREATED) | ||
async def dislike( | ||
like_data: LikeDto, | ||
session: Annotated[Session, Depends(get_session)] | ||
) -> LikeDto: | ||
await like_service.dislike(like_data, session) | ||
|
||
return like_data | ||
command: Annotated[DislikeCommand, Depends(dislike_command)], | ||
messagebus: Annotated[MessageBus, Depends(get_messagebus)] | ||
): | ||
return await messagebus.handle(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.