add entity mapper
parent
e5b36a27c3
commit
4041087c28
|
@ -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"]),
|
||||
)
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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"]),
|
||||
)
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue