Skip to content

Commit

Permalink
Refactoring create
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman505050 committed Nov 27, 2024
1 parent d155950 commit c98bcc1
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 140 deletions.
9 changes: 3 additions & 6 deletions src/core/application/transaction/dto/operation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
from pydantic import BaseModel, Field
from typing import Literal
from uuid import UUID

from core.domain.transaction.entities.operation import OperationEntity
from core.domain.transaction.enums.operation import (
get_operation_type_string,
)
from core.domain.transaction.enums.operation import OperationType


class CreateOperationDTO(BaseModel):
operation_name: str = Field(min_length=3, max_length=64)
operation_type: Literal["income", "expense", "investment"]
operation_type: OperationType


class OperationDTO(CreateOperationDTO):
Expand All @@ -21,5 +18,5 @@ def from_entity(entity: OperationEntity) -> "OperationDTO":
return OperationDTO(
operation_id=entity.operation_id,
operation_name=entity.operation_name,
operation_type=get_operation_type_string(entity.operation_type),
operation_type=entity.operation_type,
)
11 changes: 3 additions & 8 deletions src/core/application/transaction/dto/transaction.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from pydantic import BaseModel, Field
from uuid import UUID
from decimal import Decimal
from typing import Literal
import datetime

from core.domain.transaction.entities.transaction import TransactionEntity
from core.domain.transaction.enums.operation import (
get_operation_type_string,
)
from core.domain.transaction.enums.operation import OperationType


class CreateTransactionDTO(BaseModel):
Expand All @@ -26,7 +23,7 @@ class TransactionDTO(BaseModel):
category_name: str
operation_id: UUID
operation_name: str
operation_type: Literal["income", "expense", "investment"]
operation_type: OperationType
currency_id: UUID
currency_name: str
currency_code: str
Expand All @@ -44,9 +41,7 @@ def from_entity(entity: TransactionEntity) -> "TransactionDTO":
category_name=entity.category.category_name,
operation_id=entity.category.operation.operation_id,
operation_name=entity.category.operation.operation_name,
operation_type=get_operation_type_string(
entity.category.operation.operation_type
),
operation_type=entity.category.operation.operation_type,
currency_id=entity.money.currency.currency_id,
currency_name=entity.money.currency.currency_name,
currency_code=entity.money.currency.currency_code,
Expand Down
16 changes: 0 additions & 16 deletions src/core/application/transaction/factories/category.py

This file was deleted.

16 changes: 0 additions & 16 deletions src/core/application/transaction/factories/currency.py

This file was deleted.

18 changes: 0 additions & 18 deletions src/core/application/transaction/factories/operation.py

This file was deleted.

31 changes: 0 additions & 31 deletions src/core/application/transaction/factories/transaction.py

This file was deleted.

10 changes: 7 additions & 3 deletions src/core/application/transaction/use_cases/category/create.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from core.application.transaction.factories.category import CategoryFactory
from uuid import uuid4

from core.domain.transaction.entities.category import CategoryEntity
from core.domain.transaction.repositories.category import ICategoryRepository
from core.application.transaction.dto.category import (
CategoryDTO,
Expand Down Expand Up @@ -29,8 +31,10 @@ async def execute(self, request: CreateCategoryDTO) -> CategoryDTO:
request.operation_id
)

category = CategoryFactory.create(
category_name=request.category_name, operation=operation
category = CategoryEntity(
category_id=uuid4(),
category_name=request.category_name,
operation=operation,
)

category = await self._category_repository.save(category)
Expand Down
7 changes: 5 additions & 2 deletions src/core/application/transaction/use_cases/currency/create.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from uuid import uuid4

from core.application.transaction.dto.currency import (
CreateCurrencyDTO,
CurrencyDTO,
)
from core.application.transaction.factories.currency import CurrencyFactory
from core.domain.transaction.entities.currency import CurrencyEntity
from core.domain.transaction.repositories.currency import ICurrencyRepository


Expand All @@ -18,7 +20,8 @@ async def execute(self, request: CreateCurrencyDTO) -> CurrencyDTO:
already exists.
:return: The created currency.
"""
entity = CurrencyFactory.create(
entity = CurrencyEntity(
currency_id=uuid4(),
currency_name=request.currency_name,
currency_symbol=request.currency_symbol,
currency_code=request.currency_code,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from uuid import uuid4

from core.application.transaction.dto.operation import (
CreateOperationDTO,
OperationDTO,
)
from core.application.transaction.factories.operation import OperationFactory
from core.domain.transaction.entities.operation import OperationEntity
from core.domain.transaction.repositories.operation import IOperationRepository


Expand All @@ -18,7 +20,8 @@ async def execute(self, request: CreateOperationDTO) -> OperationDTO:
already exists.
:return: The created operation.
"""
entity = OperationFactory.create(
entity = OperationEntity(
operation_id=uuid4(),
operation_name=request.operation_name,
operation_type=request.operation_type,
)
Expand Down
13 changes: 7 additions & 6 deletions src/core/application/transaction/use_cases/transaction/create.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from uuid import uuid4

from core.application.transaction.dto.transaction import (
CreateTransactionDTO,
TransactionDTO,
)
from core.application.transaction.factories.transaction import (
TransactionFactory,
)
from core.domain.transaction.entities.transaction import TransactionEntity
from core.domain.transaction.repositories.transaction import (
ITransactionRepository,
)
from core.domain.transaction.repositories.category import ICategoryRepository
from core.domain.transaction.repositories.currency import ICurrencyRepository
from core.domain.transaction.value_objects.money import Money


class CreateTransactionUseCase:
Expand Down Expand Up @@ -39,11 +40,11 @@ async def execute(self, request: CreateTransactionDTO) -> TransactionDTO:
currency_id=request.currency_id
)

entity = TransactionFactory.create(
entity = TransactionEntity(
transaction_id=uuid4(),
user_id=request.user_id,
category=category,
currency=currency,
amount=request.amount,
money=Money(amount=request.amount, currency=currency),
date=request.date,
description=request.description,
)
Expand Down
33 changes: 3 additions & 30 deletions src/core/domain/transaction/enums/operation.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
from enum import Enum
from typing import Literal, Dict


class OperationType(Enum):
INCOME = 0
EXPENSE = 1
INVESTMENT = 2


def get_operation_type_string(
operation_type: OperationType,
) -> Literal["income", "expense", "investment"]:
mapping: dict[
OperationType, Literal["income", "expense", "investment"]
] = {
OperationType.INCOME: "income",
OperationType.EXPENSE: "expense",
OperationType.INVESTMENT: "investment",
}
return mapping[operation_type]


def get_operation_type_enum(
operation_type: Literal["income", "expense", "investment"],
) -> OperationType:
mapping: Dict[
Literal["income", "expense", "investment"], OperationType
] = {
"income": OperationType.INCOME,
"expense": OperationType.EXPENSE,
"investment": OperationType.INVESTMENT,
}
return mapping[operation_type]
INCOME = "income"
EXPENSE = "expense"
INVESTMENT = "investment"
13 changes: 11 additions & 2 deletions src/core/infrastructure/database/models/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Operation(Base):
unique=True,
index=True,
)
operation_type: Mapped[OperationType] = mapped_column(
operation_type: Mapped[int] = mapped_column(
SmallInteger,
nullable=False,
comment="0 - income, 1 - expense, 2 - investment",
Expand All @@ -42,6 +42,15 @@ class Operation(Base):
),
)

@staticmethod
def _get_operation_type(operation_type: int) -> OperationType:
mapping = {
0: OperationType.INCOME,
1: OperationType.EXPENSE,
2: OperationType.INVESTMENT,
}
return mapping[operation_type]

@staticmethod
def from_entity(operation: OperationEntity) -> "Operation":
return Operation(
Expand All @@ -54,5 +63,5 @@ def to_entity(self) -> OperationEntity:
return OperationEntity(
operation_id=self.operation_id,
operation_name=self.operation_name,
operation_type=OperationType(self.operation_type),
operation_type=self._get_operation_type(self.operation_type),
)

0 comments on commit c98bcc1

Please sign in to comment.