-
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.
docs: add examples and update documentation
- Loading branch information
Showing
10 changed files
with
336 additions
and
67 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
__pycache__ | ||
dist | ||
dist | ||
.pytest_cache |
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,3 @@ | ||
from .register_commands import register | ||
|
||
register() |
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,18 @@ | ||
from datetime import date | ||
from turbobus.command import Command, CommandHandler, kw_only_frozen | ||
|
||
|
||
@kw_only_frozen | ||
class CalculateAgeCommand(Command[int]): | ||
|
||
birthdate: str | date | ||
|
||
|
||
class CalculateAgeHandler(CommandHandler[CalculateAgeCommand]): | ||
|
||
def execute(self, cmd: CalculateAgeCommand) -> int: | ||
birthdate: date = cmd.birthdate if isinstance(cmd.birthdate, date) else date.fromisoformat(cmd.birthdate) | ||
|
||
today = date.today() | ||
age = today.year - birthdate.year - ((today.month, today.day) < (birthdate.month, birthdate.day)) | ||
return age |
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,9 @@ | ||
# Will execute the register function on the __init__ of the module | ||
# To ensure that all commands and handlers are registered | ||
|
||
from examples.calculate_age.context.calculate_age import CalculateAgeCommand, CalculateAgeHandler | ||
from turbobus.constants import Provider | ||
|
||
|
||
def register(): | ||
Provider.set(CalculateAgeCommand, CalculateAgeHandler) |
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 turbobus.command import CommandBus | ||
from context.calculate_age import CalculateAgeCommand | ||
|
||
if __name__ == '__main__': | ||
bus = CommandBus() | ||
|
||
result = bus.execute( | ||
CalculateAgeCommand(birthdate='1994-03-09') | ||
) | ||
|
||
print(f'You are {result} years old') |
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,80 @@ | ||
from abc import ABC, abstractmethod | ||
from dataclasses import field | ||
import uuid | ||
from turbobus.command import Command, CommandBus, CommandHandler, kw_only_frozen | ||
from turbobus.constants import Provider | ||
from turbobus.injection import inject | ||
|
||
|
||
# This is a simple Entity to represent a User | ||
@kw_only_frozen | ||
class UserEntity: | ||
id: uuid.UUID = field(default_factory=uuid.uuid4) | ||
name: str | ||
email: str | ||
|
||
|
||
# We need to define the repository interface | ||
# to save and retrieve users | ||
class UserRepository(ABC): | ||
|
||
@abstractmethod | ||
def get_by_id(self, id: uuid.UUID) -> UserEntity | None: | ||
"""Get user by id""" | ||
|
||
@abstractmethod | ||
def save(self, user: UserEntity) -> None: | ||
"""Save user""" | ||
|
||
|
||
# This is an in-memory implementation of the UserRepository | ||
class UserRepositoryInMemory(UserRepository): | ||
|
||
def __init__(self): | ||
self._users: dict[uuid.UUID, UserEntity] = {} | ||
|
||
def get_by_id(self, id: uuid.UUID) -> UserEntity | None: | ||
return self._users.get(id) | ||
|
||
def save(self, user: UserEntity) -> None: | ||
self._users[user.id] = user | ||
|
||
|
||
# Let's create a command to create a user account | ||
@kw_only_frozen | ||
class CreateUserAccount(Command[None]): | ||
name: str | ||
email: str | ||
|
||
|
||
# @inject is used to inject the dependencies | ||
@inject | ||
@kw_only_frozen | ||
class CreateUserAccountHandler(CommandHandler[CreateUserAccount]): | ||
|
||
user_repository: UserRepository | ||
|
||
def execute(self, cmd: CreateUserAccount) -> None: | ||
user = UserEntity(name=cmd.name, email=cmd.email) | ||
|
||
# It's unnecessary to retrieve the user from the repository | ||
# this is just to demonstrate that the user was saved | ||
self.user_repository.save(user) | ||
user = self.user_repository.get_by_id(user.id) | ||
|
||
if user is None: | ||
raise Exception('User not found') | ||
|
||
print(f'Welcome {user.name}!') | ||
|
||
|
||
Provider.set(UserRepository, UserRepositoryInMemory) | ||
Provider.set(CreateUserAccount, CreateUserAccountHandler) | ||
|
||
|
||
if __name__ == '__main__': | ||
bus = CommandBus() | ||
|
||
bus.execute( | ||
CreateUserAccount(name='Christopher Flores', email='cafadev@outlook.com') | ||
) |
Oops, something went wrong.