-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: map entity fields to request and response models
- Loading branch information
Showing
36 changed files
with
333 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Optional, Type | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
def filter_fields( | ||
name: Optional[str] = None, | ||
include: Optional[list[str]] = None, | ||
exclude: Optional[list[str]] = None, | ||
): | ||
"""Return a decorator to filter model fields""" | ||
|
||
def decorator(cls: Type[BaseModel]): | ||
if name: | ||
cls.__name__ = name | ||
|
||
include_ = set(cls.__fields__.keys()) | ||
|
||
if include is not None: | ||
include_ &= set(include) | ||
|
||
exclude_ = set() | ||
if exclude is not None: | ||
exclude_ = set(exclude) | ||
if include and exclude and set(include) & set(exclude): | ||
raise ValueError("include and exclude cannot contain the same fields") | ||
|
||
for field in list(cls.__fields__): | ||
if field not in include_ or field in exclude_: | ||
del cls.__fields__[field] | ||
return cls | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from common.entity_mapper import filter_fields | ||
from entities.TodoItem import TodoItem | ||
|
||
|
||
@filter_fields(name="TodoItem", exclude=["user_id"]) | ||
class TodoItemResponseModel(TodoItem): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,23 @@ | ||
import uuid | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from common.entity_mapper import filter_fields | ||
from data_providers.repository_interfaces.TodoRepositoryInterface import ( | ||
TodoRepositoryInterface, | ||
) | ||
from entities.TodoItem import TodoItem | ||
from features.todo.shared_models import TodoItemResponseModel | ||
|
||
|
||
class AddTodoRequest(BaseModel): | ||
title: str = Field( | ||
..., | ||
title="The title of the item", | ||
max_length=300, | ||
min_length=1, | ||
example="Read about clean architecture", | ||
) | ||
|
||
|
||
class AddTodoResponse(BaseModel): | ||
id: str = Field(example="vytxeTZskVKR7C7WgdSP3d") | ||
title: str = Field(example="Read about clean architecture") | ||
is_completed: bool = False | ||
|
||
@staticmethod | ||
def from_entity(todo_item: TodoItem) -> "AddTodoResponse": | ||
return AddTodoResponse(id=todo_item.id, title=todo_item.title, is_completed=todo_item.is_completed) | ||
@filter_fields(name="AddTodo", include=["title"], exclude=[]) | ||
class AddTodoRequestModel(TodoItem): | ||
pass | ||
|
||
|
||
def add_todo_use_case( | ||
data: AddTodoRequest, | ||
data: AddTodoRequestModel, | ||
user_id: str, | ||
todo_repository: TodoRepositoryInterface, | ||
) -> AddTodoResponse: | ||
) -> TodoItemResponseModel: | ||
todo_item = TodoItem(id=str(uuid.uuid4()), title=data.title, user_id=user_id) | ||
todo_repository.create(todo_item) | ||
return AddTodoResponse.from_entity(todo_item) | ||
return TodoItemResponseModel.parse_obj(todo_item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,17 @@ | ||
from typing import List | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from data_providers.repository_interfaces.TodoRepositoryInterface import ( | ||
TodoRepositoryInterface, | ||
) | ||
from entities.TodoItem import TodoItem | ||
|
||
|
||
class GetTodoAllResponse(BaseModel): | ||
id: str = Field(...) | ||
title: str = Field(...) | ||
is_completed: bool | ||
|
||
@staticmethod | ||
def from_entity(todo_item: TodoItem): | ||
return GetTodoAllResponse(id=todo_item.id, title=todo_item.title, is_completed=todo_item.is_completed) | ||
from features.todo.shared_models import TodoItemResponseModel | ||
|
||
|
||
def get_todo_all_use_case( | ||
user_id: str, | ||
todo_repository: TodoRepositoryInterface, | ||
) -> List[GetTodoAllResponse]: | ||
) -> List[TodoItemResponseModel]: | ||
return [ | ||
GetTodoAllResponse.from_entity(todo_item) | ||
TodoItemResponseModel.parse_obj(todo_item) | ||
for todo_item in todo_repository.get_all() | ||
if todo_item.user_id == user_id | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,14 @@ | ||
from typing import cast | ||
|
||
from pydantic import BaseModel, Field | ||
|
||
from common.exceptions import MissingPrivilegeException | ||
from data_providers.repository_interfaces.TodoRepositoryInterface import ( | ||
TodoRepositoryInterface, | ||
) | ||
from entities.TodoItem import TodoItem | ||
|
||
|
||
class GetTodoByIdResponse(BaseModel): | ||
id: str = Field(...) | ||
title: str = Field(...) | ||
is_completed: bool = False | ||
|
||
@staticmethod | ||
def from_entity(todo_item: TodoItem) -> "GetTodoByIdResponse": | ||
return GetTodoByIdResponse(id=todo_item.id, title=todo_item.title, is_completed=todo_item.is_completed) | ||
from features.todo.shared_models import TodoItemResponseModel | ||
|
||
|
||
def get_todo_by_id_use_case(id: str, user_id: str, todo_repository: TodoRepositoryInterface) -> GetTodoByIdResponse: | ||
def get_todo_by_id_use_case(id: str, user_id: str, todo_repository: TodoRepositoryInterface) -> TodoItemResponseModel: | ||
todo_item = todo_repository.get(id) | ||
if todo_item.user_id != user_id: | ||
raise MissingPrivilegeException | ||
return GetTodoByIdResponse.from_entity(cast(TodoItem, todo_item)) | ||
return TodoItemResponseModel.parse_obj(cast(TodoItemResponseModel, todo_item)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,26 @@ | ||
from pydantic import BaseModel, Field | ||
|
||
from common.entity_mapper import filter_fields | ||
from common.exceptions import MissingPrivilegeException | ||
from data_providers.repository_interfaces.TodoRepositoryInterface import ( | ||
TodoRepositoryInterface, | ||
) | ||
from entities.TodoItem import TodoItem | ||
from features.todo.shared_models import TodoItemResponseModel | ||
|
||
|
||
class UpdateTodoRequest(BaseModel): | ||
title: str = Field( | ||
"", | ||
title="The title of the item", | ||
max_length=300, | ||
min_length=1, | ||
) | ||
is_completed: bool | ||
|
||
|
||
class UpdateTodoResponse(BaseModel): | ||
success: bool = Field(...) | ||
@filter_fields(name="UpdateTodo", include=["title", "is_completed"], exclude=[]) | ||
class UpdateTodoRequest(TodoItem): | ||
pass | ||
|
||
|
||
def update_todo_use_case( | ||
id: str, | ||
data: UpdateTodoRequest, | ||
user_id: str, | ||
todo_repository: TodoRepositoryInterface, | ||
) -> UpdateTodoResponse: | ||
) -> TodoItemResponseModel: | ||
todo_item = todo_repository.get(id) | ||
if todo_item.user_id != user_id: | ||
raise MissingPrivilegeException | ||
|
||
updated_todo_item = TodoItem(id=todo_item.id, title=data.title, is_completed=data.is_completed, user_id=user_id) | ||
if todo_repository.update(updated_todo_item): | ||
return UpdateTodoResponse(success=True) | ||
return UpdateTodoResponse(success=False) | ||
todo_repository.update(updated_todo_item) | ||
return TodoItemResponseModel.parse_obj(updated_todo_item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.