Skip to content

Commit

Permalink
Merge pull request #12 from azevedoguigo/development
Browse files Browse the repository at this point in the history
v0.4.0
  • Loading branch information
azevedoguigo committed May 13, 2024
2 parents ebaf447 + c2ee0e1 commit 8aa7bd9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/gecko_api/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions lib/gecko_api/tasks/get_all.ex
Original file line number Diff line number Diff line change
@@ -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
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 @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/gecko_api_web/controllers/tasks_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
get "/tasks/all", TasksController, :show_all
put "/tasks", TasksController, :update
delete "/tasks", TasksController, :delete
end
Expand Down
27 changes: 27 additions & 0 deletions test/gecko_api/tasks/get_all_test.exs
Original file line number Diff line number Diff line change
@@ -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: "[email protected]",
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
18 changes: 18 additions & 0 deletions test/gecko_api_web/controllers/tasks_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = %{
Expand Down

0 comments on commit 8aa7bd9

Please sign in to comment.