Skip to content

Commit

Permalink
Merge pull request #7 from azevedoguigo/feature/delete-task
Browse files Browse the repository at this point in the history
Feature/delete task
  • Loading branch information
azevedoguigo committed May 10, 2024
2 parents 4af46d0 + 2352822 commit 2f45db3
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/gecko_api/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ defmodule GeckoApi.Tasks do
@moduledoc """
This module provides function delegations to create, get, and update task data.
"""
alias GeckoApi.Tasks.{Create, Get}
alias GeckoApi.Tasks.{Create, Get, Delete}

defdelegate create_task(task_params), to: Create, as: :call
defdelegate get_task(task_id), to: Get, as: :call
defdelegate delete_task(task_id), to: Delete, as: :call
end
14 changes: 14 additions & 0 deletions lib/gecko_api/tasks/delete.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
defmodule GeckoApi.Tasks.Delete do
@moduledoc """
Provides a function call/1 to delete task by id.
"""
alias GeckoApi.Tasks
alias GeckoApi.Repo

def call(task_id) do
case Tasks.get_task(task_id) do
{:error, error_data} -> {:error, error_data}
{:ok, task} -> Repo.delete(task)
end
end
end
8 changes: 8 additions & 0 deletions lib/gecko_api_web/controllers/tasks_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,12 @@ defmodule GeckoApiWeb.TasksController do
|> render(:show, task: task)
end
end

def delete(conn, %{"id" => id}) do
with {:ok, task} <- Tasks.delete_task(id) do
conn
|> put_status(:ok)
|> render(:delete, task: task)
end
end
end
2 changes: 2 additions & 0 deletions lib/gecko_api_web/controllers/tasks_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ defmodule GeckoApiWeb.TasksJSON do

def show(%{task: task}), do: data(task)

def delete(%{task: task}), do: %{message: "Task deleted!", task: data(task)}

defp data(%Task{} = task) do
%{
id: task.id,
Expand Down
1 change: 1 addition & 0 deletions lib/gecko_api_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule GeckoApiWeb.Router do

post "/tasks", TasksController, :create
get "/tasks", TasksController, :show
delete "/tasks/", TasksController, :delete
end

# Enable LiveDashboard and Swoosh mailbox preview in development
Expand Down
51 changes: 51 additions & 0 deletions test/gecko_api/tasks/delete_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule GeckoApi.Tasks.DeleteTest do
use GeckoApi.DataCase

alias GeckoApi.Users
alias GeckoApi.Tasks
alias GeckoApi.Tasks.{Task, Delete}

setup do
user_params = %{
name: "Lewis Hamilton",
email: "[email protected]",
password: "lewisinferrari"
}

{:ok, user} = Users.create_user(user_params)

{:ok, user: user}
end

describe "call/1" do
test "Delete a task when the id is valid and belongs to an existing task.", %{user: user} do
user_id = user.id
task_params = %{
title: "Create geck api unit tests.",
description: "Unit tests are useful to ensure scalability and security in API development.",
user_id: user_id
}
{:ok, created_task} = Tasks.create_task(task_params)

{:ok, task} = Delete.call(created_task.id)

assert %Task{
title: "Create geck api unit tests.",
description: "Unit tests are useful to ensure scalability and security in API development.",
user_id: ^user_id
} = task
end

test "Returns a tuple with :error and error data if the id is invalid." do
{:error, error_data} = Delete.call("invalid_id")

assert %{message: "Invalid task ID!", status_code: :bad_request} == error_data
end

test "Returns a tuple with :error and error data if the id does not belong to a task." do
{:error, error_data} = Delete.call(Ecto.UUID.generate())

assert %{message: "Task does not exists!", status_code: :not_found} == error_data
end
end
end
49 changes: 48 additions & 1 deletion test/gecko_api_web/controllers/tasks_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule GeckoApiWeb.TasksControllerTest do
end
end

describe "get/1" do
describe "get/2" do
test "Returns the task data if the id is valid and belongs to a task.", %{conn: conn, user: user} do
user_id = user.id

Expand Down Expand Up @@ -101,4 +101,51 @@ defmodule GeckoApiWeb.TasksControllerTest do
assert %{"message" => "Task does not exists!", "status" => 404} == response
end
end

describe "delete/2" do
test "Returns the task data and message if the id is valid and belongs to a task.", %{conn: conn, user: user} do
user_id = user.id

task_params = %{
title: "Create tests of of tasks feature.",
description: "The tests includes all modules and controllers.",
user_id: user_id
}

{:ok, task} = Tasks.create_task(task_params)

response =
conn
|> delete("/api/tasks/?id=#{task.id}")
|> json_response(:ok)

assert %{
"message" => "Task deleted!",
"task" => %{
"completed" => false,
"description" => "The tests includes all modules and controllers.",
"title" => "Create tests of of tasks feature.",
"user_id" => ^user_id
}
} = response
end

test "Returns an error message and a status code if the task id is invalid.", %{conn: conn} do
response =
conn
|> delete("/api/tasks/?id=invalid_id")
|> json_response(:bad_request)

assert %{"message" => "Invalid task ID!", "status" => 400} == response
end

test "Returns an error message and a status code if the id does not belong to any task", %{conn: conn} do
response =
conn
|> delete("/api/tasks/?id=#{Ecto.UUID.generate()}")
|> json_response(:not_found)

assert %{"message" => "Task does not exists!", "status" => 404} == response
end
end
end

0 comments on commit 2f45db3

Please sign in to comment.