Skip to content

Commit

Permalink
refactor: remove delete response (need to rebase after PR)
Browse files Browse the repository at this point in the history
  • Loading branch information
eoaksnes committed Nov 18, 2022
1 parent 8a9fcc5 commit 357f1eb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api/src/features/todo/todo_feature.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

from fastapi import APIRouter, Depends
from starlette import status
from starlette.responses import JSONResponse

from authentication.authentication import auth_with_jwt
Expand All @@ -10,7 +11,6 @@
from data_providers.repository_interfaces.TodoRepositoryInterface import (
TodoRepositoryInterface,
)
from features.todo.use_cases.delete_todo_by_id import DeleteTodoResponse

from .shared_models import TodoItemResponseModel
from .use_cases.add_todo import AddTodoRequestModel, add_todo_use_case
Expand Down Expand Up @@ -42,14 +42,14 @@ def get_todo_by_id(
return get_todo_by_id_use_case(id=id, user_id=user.user_id, todo_repository=todo_repository).dict()


@router.delete("/{id}", operation_id="delete_by_id", response_model=DeleteTodoResponse)
@router.delete("/{id}", operation_id="delete_by_id", status_code=status.HTTP_204_NO_CONTENT)
@create_response(JSONResponse)
def delete_todo_by_id(
id: str,
user: User = Depends(auth_with_jwt),
todo_repository: TodoRepositoryInterface = Depends(get_todo_repository),
):
return delete_todo_use_case(id=id, user_id=user.user_id, todo_repository=todo_repository).dict()
return delete_todo_use_case(id=id, user_id=user.user_id, todo_repository=todo_repository)


@router.get("", operation_id="get_all", response_model=List[TodoItemResponseModel])
Expand Down
9 changes: 1 addition & 8 deletions api/src/features/todo/use_cases/delete_todo_by_id.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
from pydantic import BaseModel, Field

from common.exceptions import MissingPrivilegeException, NotFoundException
from data_providers.repository_interfaces.TodoRepositoryInterface import (
TodoRepositoryInterface,
)


class DeleteTodoResponse(BaseModel):
success: bool = Field(...)


def delete_todo_use_case(id: str, user_id: str, todo_repository: TodoRepositoryInterface) -> DeleteTodoResponse:
def delete_todo_use_case(id: str, user_id: str, todo_repository: TodoRepositoryInterface) -> None:
todo_item = todo_repository.get(id)
if todo_item is None:
raise NotFoundException
if todo_item.user_id != user_id:
raise MissingPrivilegeException
todo_repository.delete(id)
return DeleteTodoResponse(success=True)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

def test_delete_todo_should_return_success(todo_repository: TodoRepositoryInterface):
id = "dh2109"
result = delete_todo_use_case(id=id, user_id="xyz", todo_repository=todo_repository)
assert result.success
try:
delete_todo_use_case(id=id, user_id="xyz", todo_repository=todo_repository)
except Exception:
assert False


def test_delete_todo_should_return_not_success(todo_repository: TodoRepositoryInterface):
Expand Down

0 comments on commit 357f1eb

Please sign in to comment.