-
Couldn't load subscription status.
- Fork 0
9️⃣ CHAPTER_09 messagebus #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,6 +1,6 @@ | ||
| from typing import Any | ||
|
|
||
|
|
||
| def send_mail(*args: Any) -> None: | ||
| def send(*args: Any) -> None: | ||
| """Send an email to the user.""" | ||
| print("Sending email to user") | ||
| print("Sending email to user", *args) |
This file contains hidden or 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 hidden or 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,10 +1,33 @@ | ||
| from dataclasses import dataclass | ||
| from datetime import date | ||
| from uuid import UUID | ||
|
|
||
|
|
||
| class Event: | ||
| pass | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchCreated(Event): | ||
| id: UUID | ||
| sku: str | ||
| qty: int | ||
| eta: date = None | ||
|
|
||
|
|
||
| @dataclass | ||
| class BatchQuantityChanged(Event): | ||
| id: UUID | ||
| qty: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class AllocationRequired(Event): | ||
| order_id: UUID | ||
| sku: str | ||
| qty: int | ||
|
|
||
|
|
||
| @dataclass | ||
| class OutOfStock(Event): | ||
| sku: str |
This file contains hidden or 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 hidden or 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 hidden or 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,55 @@ | ||
| from uuid import UUID | ||
|
|
||
| from app.allocation.adapters import email | ||
| from app.allocation.adapters.repository import AbstractProductRepository | ||
| from app.allocation.domain import events, models | ||
| from app.allocation.service_layer import unit_of_work | ||
|
|
||
|
|
||
| class InvalidSku(Exception): | ||
| pass | ||
|
|
||
|
|
||
| async def add_batch( | ||
| event: events.BatchCreated, | ||
| uow: unit_of_work.AbstractUnitOfWork[AbstractProductRepository], | ||
| ) -> None: | ||
| async with uow: | ||
| product = await uow.repo.get(event.sku) | ||
| if product is None: | ||
| product = models.Product(sku=event.sku, batches=[]) | ||
| await uow.repo.add(product) | ||
| product.batches.append( | ||
| models.Batch(id=event.id, sku=event.sku, qty=event.qty, eta=event.eta) | ||
| ) | ||
| await uow.commit() | ||
|
|
||
|
|
||
| async def allocate( | ||
| event: events.AllocationRequired, | ||
| uow: unit_of_work.AbstractUnitOfWork[AbstractProductRepository], | ||
| ) -> UUID: | ||
| line = models.OrderLine(id=event.order_id, sku=event.sku, qty=event.qty) | ||
| async with uow: | ||
| product = await uow.repo.get(line.sku) | ||
| if product is None: | ||
| raise InvalidSku(f"Invalid sku {line.sku}") | ||
| batch_id = product.allocate(line) | ||
| await uow.commit() | ||
| return batch_id | ||
|
|
||
|
|
||
| async def change_batch_quantity( | ||
| event: events.BatchQuantityChanged, | ||
| uow: unit_of_work.AbstractUnitOfWork[AbstractProductRepository], | ||
| ) -> None: | ||
| async with uow: | ||
| product = await uow.repo.get_by_batch_id(event.id) | ||
| product.change_batch_quantity(event.id, event.qty) | ||
| await uow.commit() | ||
|
|
||
|
|
||
| def send_out_of_stock_notification( | ||
| event: events.OutOfStock, uow: unit_of_work.AbstractUnitOfWork[AbstractProductRepository] | ||
| ) -> None: | ||
| email.send("stock@made.com", f"Out of stock for {event.sku}") |
This file contains hidden or 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,18 +1,30 @@ | ||
| from app.allocation.adapters import email | ||
| from app.allocation.domain import events | ||
|
|
||
|
|
||
| async def handle(event: events.Event) -> None: | ||
| if isinstance(event, events.OutOfStock): | ||
| await send_out_of_stock_notification(event) | ||
| else: | ||
| raise Exception(f"Unknown event {event}") | ||
| from typing import Any | ||
|
|
||
|
|
||
| async def send_out_of_stock_notification(event: events.OutOfStock) -> None: | ||
| email.send_mail("stock@made.com", f"Out of stock for {event.sku}") | ||
| from app.allocation.domain import events | ||
| from app.allocation.service_layer import handlers, unit_of_work | ||
|
|
||
|
|
||
| HANDLERS = { | ||
| events.OutOfStock: [send_out_of_stock_notification], | ||
| } | ||
| # TODO: 이렇게 하는거 맞나? | ||
| async def handle( | ||
| event: events.Event, | ||
| uow: unit_of_work.AbstractUnitOfWork[unit_of_work.AbstractProductRepository], | ||
| ) -> list[Any]: | ||
| results = [] | ||
| queue = [event] | ||
| while queue: | ||
| event = queue.pop(0) | ||
| result = None | ||
| if isinstance(event, events.OutOfStock): | ||
| handlers.send_out_of_stock_notification(event, uow) | ||
| elif isinstance(event, events.BatchQuantityChanged): | ||
| await handlers.change_batch_quantity(event, uow) | ||
| elif isinstance(event, events.AllocationRequired): | ||
| result = await handlers.allocate(event, uow) | ||
| elif isinstance(event, events.BatchCreated): | ||
| await handlers.add_batch(event, uow) | ||
| else: | ||
| raise Exception(f"Unknown event {event}") | ||
| if result: | ||
| results.append(result) | ||
| queue.extend(uow.collect_new_events()) | ||
| return results |
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
정적분석시에는 batches가 orm이 아니기 때문에 타입에러 발생