diff --git a/src/clifinance/domain/expense/model.py b/src/clifinance/domain/expense/model.py index 2cd279b..c94ae50 100644 --- a/src/clifinance/domain/expense/model.py +++ b/src/clifinance/domain/expense/model.py @@ -53,22 +53,3 @@ class Expense(DomainEntity[ExpenseId]): amount=ExpenseAmount(amount), description=ExpenseDescription(description), ) - - def to_json(self) -> dict: - return { - "id": self.id.value, - "type": self.type.value, - "date": self.date.isoformat(), - "amount": self.amount.value, - "description": self.description.value, - } - - @staticmethod - def from_json(data: dict) -> "Expense": - return Expense( - id=ExpenseId(data["id"]), - type=ExpenseType(data["type"]), - date=ExpenseDate.from_isoformat(data["date"]), - amount=ExpenseAmount(data["amount"]), - description=ExpenseDescription(data["description"]), - ) diff --git a/src/clifinance/infrastructure/persistence/json_driver.py b/src/clifinance/infrastructure/persistence/json_driver.py index af0a665..3087a88 100644 --- a/src/clifinance/infrastructure/persistence/json_driver.py +++ b/src/clifinance/infrastructure/persistence/json_driver.py @@ -1,10 +1,14 @@ import json from clifinance.domain.expense.model import Expense +from clifinance.infrastructure.persistence.mappers.expense import ( + expense_from_json, + expense_to_json, +) class JsonFileDriver: - def read(self): + def read(self) -> list[Expense]: try: with open("./app_data.json") as f: data = json.load(f) @@ -15,12 +19,12 @@ class JsonFileDriver: except json.JSONDecodeError: return list() - def write(self, data: list[Expense]): + def write(self, data: list[Expense]) -> None: with open("./app_data.json", "w") as f: json.dump(self.__data_to_json(data), f) - def __data_to_json(self, data: list[Expense]): - return [x.to_json() for x in data] + def __data_to_json(self, data: list[Expense]) -> list[dict]: + return [expense_to_json(x) for x in data] - def __data_from_json(self, data: list[dict]): - return [Expense.from_json(x) for x in data] + def __data_from_json(self, data: list[dict]) -> list[Expense]: + return [expense_from_json(x) for x in data] diff --git a/src/clifinance/infrastructure/persistence/mappers/__init__.py b/src/clifinance/infrastructure/persistence/mappers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/clifinance/infrastructure/persistence/mappers/expense.py b/src/clifinance/infrastructure/persistence/mappers/expense.py new file mode 100644 index 0000000..5f8dcb7 --- /dev/null +++ b/src/clifinance/infrastructure/persistence/mappers/expense.py @@ -0,0 +1,28 @@ +from clifinance.domain.expense.model import ( + Expense, + ExpenseAmount, + ExpenseDate, + ExpenseDescription, + ExpenseId, + ExpenseType, +) + + +def expense_to_json(expense: Expense): + return { + "id": expense.id.value, + "description": expense.description.value, + "amount": expense.amount.value, + "date": expense.date, + "type": expense.type.value, + } + + +def expense_from_json(data: dict): + return Expense( + id=ExpenseId(data["id"]), + description=ExpenseDescription(data["description"]), + amount=ExpenseAmount(data["amount"]), + date=ExpenseDate(data["date"]), + type=ExpenseType(data["type"]), + ) diff --git a/src/clifinance/infrastructure/session.py b/src/clifinance/infrastructure/session.py index 691416d..42a4450 100644 --- a/src/clifinance/infrastructure/session.py +++ b/src/clifinance/infrastructure/session.py @@ -38,11 +38,6 @@ class JsonFileSession(Session): self.data = self.driver.read() def add(self, expense: Expense) -> None: - # if not self.data: - # last_id = -1 - # else: - # last_id = max({x.id for x in self.data if x.id is not None}) - # self.__dirty_new.append(expense) def update(self, expense: Expense) -> None: