diff --git a/src/clifinance/application/usecases/get_balance.py b/src/clifinance/application/usecases/get_balance.py index 6f8149c..547b671 100644 --- a/src/clifinance/application/usecases/get_balance.py +++ b/src/clifinance/application/usecases/get_balance.py @@ -1,14 +1,18 @@ -# from clifinance.application.dto.balance import Balance -# from clifinance.domain.expense.gateway import ExpenseGateway -# -# -# class GetBalance: -# def __init__( -# self, -# gateway: ExpenseGateway, -# ) -> None: -# self.__gateway = gateway -# -# def __call__(self) -> Balance: -# -# return self.__gateway.get_balance() +from clifinance.application.dto.balance import Balance +from clifinance.domain.expense.gateway import ExpenseGateway +from clifinance.domain.expense.model import ExpenseType + + +class GetBalance: + def __init__( + self, + gateway: ExpenseGateway, + ) -> None: + self.__gateway = gateway + + def __call__(self) -> Balance: + return Balance( + balance=sum(e.amount.value for e in self.__gateway.get_all_expenses()), + imcome_balance=sum(e.amount.value for e in self.__gateway.get_all_expenses_by_type(ExpenseType.INCOME)), + expense_balance=sum(e.amount.value for e in self.__gateway.get_all_expenses_by_type(ExpenseType.EXPENSE)), + ) diff --git a/src/clifinance/main/container.py b/src/clifinance/main/container.py index c55222d..f37452e 100644 --- a/src/clifinance/main/container.py +++ b/src/clifinance/main/container.py @@ -7,8 +7,7 @@ from typing import ParamSpec, TypeVar from clifinance.application.abstractions.protocols.driver import FileDriver from clifinance.application.abstractions.protocols.session import Session from clifinance.application.usecases.add_expense import AddExpense - -# from clifinance.application.usecases.get_balance import GetBalance +from clifinance.application.usecases.get_balance import GetBalance from clifinance.domain.expense.gateway import ExpenseGateway from clifinance.infrastructure.persistence.json_driver import JsonFileDriver from clifinance.infrastructure.persistence.json_gateway import JsonExpenseGateway @@ -17,9 +16,9 @@ from clifinance.infrastructure.session import JsonFileSession class Container: - # @contextmanager - # def provide_get_balance(self) -> Generator[GetBalance, None, None]: - # yield GetBalance(gateway=self.json_gateway()) + @contextmanager + def provide_get_balance(self) -> Generator[GetBalance, None, None]: + yield GetBalance(gateway=self.json_gateway()) @contextmanager def provide_add_expense(self) -> Generator[AddExpense, None, None]: diff --git a/src/clifinance/presentation/cli/commands.py b/src/clifinance/presentation/cli/commands.py index fdde7fb..e3dca5f 100644 --- a/src/clifinance/presentation/cli/commands.py +++ b/src/clifinance/presentation/cli/commands.py @@ -5,8 +5,8 @@ from clifinance.main.container import Container def get_balance(args: argparse.Namespace, ioc: Container) -> None: - # with ioc.provide_get_balance() as usecase: - # print(usecase()) + with ioc.provide_get_balance() as usecase: + print(usecase()) print()