Skip to content

Commit

Permalink
refactor: extract dto in special files
Browse files Browse the repository at this point in the history
  • Loading branch information
andiserg committed Feb 10, 2024
1 parent c892ada commit 61a2ab9
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/costy/adapters/db/category_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from sqlalchemy import delete, or_, select
from sqlalchemy.ext.asyncio import AsyncSession

from costy.application.category.read_available_categories import CategoryDTO
from costy.application.category.dto import CategoryDTO
from costy.application.common.category_gateway import (
CategoriesReader,
CategoryDeleter,
Expand Down Expand Up @@ -37,7 +37,7 @@ async def delete_category(self, category_id: CategoryId) -> None:
)
await self.session.execute(query)

async def find_categories(self, user_id: UserId) -> list[CategoryDTO]: # type: ignore # noqa
async def find_categories(self, user_id: UserId) -> list[CategoryDTO]:
filter_expr = or_(
Category.user_id == user_id, # type: ignore
Category.user_id == None # type: ignore # noqa: E711
Expand Down
8 changes: 1 addition & 7 deletions src/costy/application/category/create_category.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
from dataclasses import dataclass

from costy.domain.models.category import CategoryId, CategoryType
from costy.domain.services.category import CategoryService

from ..common.category_gateway import CategorySaver
from ..common.id_provider import IdProvider
from ..common.interactor import Interactor
from ..common.uow import UoW


@dataclass
class NewCategoryDTO:
name: str
from .dto import NewCategoryDTO


class CreateCategory(Interactor[NewCategoryDTO, CategoryId]):
Expand Down
20 changes: 20 additions & 0 deletions src/costy/application/category/dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from dataclasses import dataclass

from costy.domain.models.category import CategoryId, CategoryType


@dataclass
class NewCategoryDTO:
name: str


@dataclass
class ReadAvailableCategoriesDTO:
...


@dataclass
class CategoryDTO:
id: CategoryId | None
name: str
kind: CategoryType
17 changes: 2 additions & 15 deletions src/costy/application/category/read_available_categories.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
from dataclasses import dataclass
from typing import List, Optional

from costy.domain.models.category import CategoryId, CategoryType
from costy.domain.services.category import CategoryService

from ..common.category_gateway import CategoriesReader
from ..common.id_provider import IdProvider
from ..common.interactor import Interactor
from ..common.uow import UoW


@dataclass
class ReadAvailableCategoriesDTO:
...


@dataclass
class CategoryDTO:
id: CategoryId | None
name: str
kind: CategoryType
from .dto import CategoryDTO, ReadAvailableCategoriesDTO


class ReadAvailableCategories(Interactor[None, List[CategoryDTO]]):
Expand All @@ -39,4 +26,4 @@ async def __call__(
self, data: Optional[ReadAvailableCategoriesDTO] = None
) -> List[CategoryDTO]:
user_id = await self.id_provider.get_current_user_id()
return await self.category_db_gateway.find_categories(user_id) # type: ignore # noqa
return await self.category_db_gateway.find_categories(user_id)
4 changes: 2 additions & 2 deletions src/costy/application/common/category_gateway.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import abstractmethod
from typing import Protocol, runtime_checkable

# from costy.application.category.read_available_categories import CategoryDTO
from costy.application.category.dto import CategoryDTO
from costy.domain.models.category import Category, CategoryId
from costy.domain.models.user import UserId

Expand All @@ -23,7 +23,7 @@ async def get_category(self, category_id: CategoryId) -> Category | None:
@runtime_checkable
class CategoriesReader(Protocol):
@abstractmethod
async def find_categories(self, user_id: UserId) -> list[Category]:
async def find_categories(self, user_id: UserId) -> list[CategoryDTO]:
raise NotImplementedError


Expand Down
12 changes: 1 addition & 11 deletions src/costy/application/operation/create_operation.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
from dataclasses import dataclass

from costy.application.common.id_provider import IdProvider
from costy.application.common.interactor import Interactor
from costy.application.common.operation_gateway import OperationSaver
from costy.application.common.uow import UoW
from costy.domain.models.category import CategoryId
from costy.application.operation.dto import NewOperationDTO
from costy.domain.models.operation import OperationId
from costy.domain.services.operation import OperationService


@dataclass
class NewOperationDTO:
amount: int
description: str | None
time: int
category_id: CategoryId


class CreateOperation(Interactor[NewOperationDTO, OperationId]):
def __init__(
self,
Expand Down
17 changes: 17 additions & 0 deletions src/costy/application/operation/dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from dataclasses import dataclass

from costy.domain.models.category import CategoryId


@dataclass
class NewOperationDTO:
amount: int
description: str | None
time: int
category_id: CategoryId


@dataclass
class ListOperationDTO:
from_time: int | None
to_time: int | None
8 changes: 1 addition & 7 deletions src/costy/application/operation/read_list_operation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from dataclasses import dataclass
from typing import List

from costy.domain.models.operation import Operation
Expand All @@ -8,12 +7,7 @@
from ..common.interactor import Interactor
from ..common.operation_gateway import OperationsReader
from ..common.uow import UoW


@dataclass
class ListOperationDTO:
from_time: int | None
to_time: int | None
from .dto import ListOperationDTO


class ReadListOperation(Interactor[ListOperationDTO, List[Operation]]):
Expand Down
9 changes: 1 addition & 8 deletions src/costy/application/user/create_user.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
from dataclasses import dataclass

from costy.domain.models.user import UserId
from costy.domain.services.user import UserService

from ..common.interactor import Interactor
from ..common.uow import UoW
from ..common.user_gateway import UserSaver


@dataclass
class NewUserDTO:
email: str
password: str
from .dto import NewUserDTO


class CreateUser(Interactor[NewUserDTO, UserId]):
Expand Down
7 changes: 7 additions & 0 deletions src/costy/application/user/dto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from dataclasses import dataclass


@dataclass
class NewUserDTO:
email: str
password: str

0 comments on commit 61a2ab9

Please sign in to comment.