diff --git a/lib/gecko_api/tasks.ex b/lib/gecko_api/tasks.ex index b2d3fe0..450bb45 100644 --- a/lib/gecko_api/tasks.ex +++ b/lib/gecko_api/tasks.ex @@ -2,10 +2,11 @@ defmodule GeckoApi.Tasks do @moduledoc """ This module provides function delegations to create, get, and update task data. """ - alias GeckoApi.Tasks.{Create, Get, Update, Delete} + alias GeckoApi.Tasks.{Create, Get, GetAll, Update, Delete} defdelegate create_task(task_params), to: Create, as: :call defdelegate get_task(task_id), to: Get, as: :call + defdelegate get_tasks(user_id), to: GetAll, as: :call defdelegate delete_task(task_id), to: Delete, as: :call defdelegate update_task(task_params), to: Update, as: :call end diff --git a/lib/gecko_api/tasks/get_all.ex b/lib/gecko_api/tasks/get_all.ex new file mode 100644 index 0000000..acba3de --- /dev/null +++ b/lib/gecko_api/tasks/get_all.ex @@ -0,0 +1,13 @@ +defmodule GeckoApi.Tasks.GetAll do + @moduledoc """ + Provides a function call/1 to get all tasks. + """ + alias GeckoApi.Repo + alias GeckoApi.Tasks.Task + import Ecto.Query + + def call(user_id) do + from(t in Task, where: t.user_id == ^user_id) + |> Repo.all() + end +end diff --git a/lib/gecko_api_web/controllers/tasks_controller.ex b/lib/gecko_api_web/controllers/tasks_controller.ex index 5954a0f..ad31e59 100644 --- a/lib/gecko_api_web/controllers/tasks_controller.ex +++ b/lib/gecko_api_web/controllers/tasks_controller.ex @@ -33,6 +33,14 @@ defmodule GeckoApiWeb.TasksController do end end + def show_all(conn, _options) do + %User{id: user_id} = Guardian.Plug.current_resource(conn) + + conn + |> put_status(:ok) + |> render(:show_all, tasks: Tasks.get_tasks(user_id)) + end + def update(conn, params) do with {:ok, task} <- Tasks.update_task(params) do conn diff --git a/lib/gecko_api_web/controllers/tasks_json.ex b/lib/gecko_api_web/controllers/tasks_json.ex index 4b52955..2873c7e 100644 --- a/lib/gecko_api_web/controllers/tasks_json.ex +++ b/lib/gecko_api_web/controllers/tasks_json.ex @@ -10,7 +10,11 @@ defmodule GeckoApiWeb.TasksJSON do end def show(%{task: task}), do: data(task) + + def show_all(%{tasks: tasks}), do: Enum.map(tasks, fn task -> data(task) end) + def update(%{task: task}), do: %{message: "Task updated!", task: data(task)} + def delete(%{task: task}), do: %{message: "Task deleted!", task: data(task)} defp data(%Task{} = task) do diff --git a/lib/gecko_api_web/router.ex b/lib/gecko_api_web/router.ex index 87cebc7..7238dff 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 + get "/tasks/all", TasksController, :show_all put "/tasks", TasksController, :update delete "/tasks", TasksController, :delete end diff --git a/test/gecko_api/tasks/get_all_test.exs b/test/gecko_api/tasks/get_all_test.exs new file mode 100644 index 0000000..a9000a8 --- /dev/null +++ b/test/gecko_api/tasks/get_all_test.exs @@ -0,0 +1,27 @@ +defmodule GeckoApi.Tasks.GetAllTest do + use GeckoApi.DataCase + + alias GeckoApi.{Users, Tasks} + + describe "call/1" do + test "Returns a list of user tasks." do + user_params = %{ + name: "Lewis Hamilton", + email: "lh44@ferrari.com", + password: "lewisinferrari" + } + {:ok, user} = Users.create_user(user_params) + + task_params = %{ + title: "Create tests of of tasks feature.", + description: "The tests includes all modules and controllers.", + user_id: user.id + } + {:ok, _result} = Tasks.create_task(task_params) + + task_list = Tasks.GetAll.call(user.id) + + assert length(task_list) > 0 + end + end +end diff --git a/test/gecko_api_web/controllers/tasks_controller_test.exs b/test/gecko_api_web/controllers/tasks_controller_test.exs index a0f203e..a01acee 100644 --- a/test/gecko_api_web/controllers/tasks_controller_test.exs +++ b/test/gecko_api_web/controllers/tasks_controller_test.exs @@ -99,6 +99,24 @@ defmodule GeckoApiWeb.TasksControllerTest do end end + describe "get_all/1" do + test "Returns a list of user tasks.", %{conn: conn, user: user} do + 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, _task} = Tasks.create_task(task_params) + + response = + conn + |> get("/api/tasks/all") + |> json_response(:ok) + + assert length(response) > 0 + end + end + describe "update/2" do test "Returns a message and the updated task if all parameters to be updated are valid.", %{conn: conn, task: task} do update_params = %{