From d901a2ec56aa48066b38e25370fc462150298392 Mon Sep 17 00:00:00 2001 From: Guilherme Azevedo Date: Thu, 9 May 2024 22:59:05 -0300 Subject: [PATCH 1/3] :sparkles: feat: delete task. --- lib/gecko_api/tasks.ex | 3 ++- lib/gecko_api/tasks/delete.ex | 14 ++++++++++++++ lib/gecko_api_web/controllers/tasks_controller.ex | 8 ++++++++ lib/gecko_api_web/controllers/tasks_json.ex | 2 ++ lib/gecko_api_web/router.ex | 1 + 5 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 lib/gecko_api/tasks/delete.ex diff --git a/lib/gecko_api/tasks.ex b/lib/gecko_api/tasks.ex index dabf3dc..a4eb5cf 100644 --- a/lib/gecko_api/tasks.ex +++ b/lib/gecko_api/tasks.ex @@ -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 diff --git a/lib/gecko_api/tasks/delete.ex b/lib/gecko_api/tasks/delete.ex new file mode 100644 index 0000000..19c0534 --- /dev/null +++ b/lib/gecko_api/tasks/delete.ex @@ -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 diff --git a/lib/gecko_api_web/controllers/tasks_controller.ex b/lib/gecko_api_web/controllers/tasks_controller.ex index 5f371fe..fdc8dfb 100644 --- a/lib/gecko_api_web/controllers/tasks_controller.ex +++ b/lib/gecko_api_web/controllers/tasks_controller.ex @@ -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 diff --git a/lib/gecko_api_web/controllers/tasks_json.ex b/lib/gecko_api_web/controllers/tasks_json.ex index 91ac653..ad1d7d2 100644 --- a/lib/gecko_api_web/controllers/tasks_json.ex +++ b/lib/gecko_api_web/controllers/tasks_json.ex @@ -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, diff --git a/lib/gecko_api_web/router.ex b/lib/gecko_api_web/router.ex index c12ad4e..32c5efe 100644 --- a/lib/gecko_api_web/router.ex +++ b/lib/gecko_api_web/router.ex @@ -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 From 2bbad24be478b6948648df8c04371d1a48873523 Mon Sep 17 00:00:00 2001 From: Guilherme Azevedo Date: Thu, 9 May 2024 23:11:36 -0300 Subject: [PATCH 2/3] test: delete task module tests added. --- test/gecko_api/tasks/delete_test.exs | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 test/gecko_api/tasks/delete_test.exs diff --git a/test/gecko_api/tasks/delete_test.exs b/test/gecko_api/tasks/delete_test.exs new file mode 100644 index 0000000..27f6009 --- /dev/null +++ b/test/gecko_api/tasks/delete_test.exs @@ -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: "lh44@ferrari.com", + 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 From 23528224f9a6e142297ac31a206e9512c0161e52 Mon Sep 17 00:00:00 2001 From: Guilherme Azevedo Date: Thu, 9 May 2024 23:16:26 -0300 Subject: [PATCH 3/3] test: delete task action tests added. --- .../controllers/tasks_controller_test.exs | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/test/gecko_api_web/controllers/tasks_controller_test.exs b/test/gecko_api_web/controllers/tasks_controller_test.exs index 7a184eb..6412113 100644 --- a/test/gecko_api_web/controllers/tasks_controller_test.exs +++ b/test/gecko_api_web/controllers/tasks_controller_test.exs @@ -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 @@ -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