Skip to content

Commit

Permalink
Add task index endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Mar 26, 2024
1 parent cb43dfb commit 500a7e7
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lib/lynx/module/task_module.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2023 Clivern. All rights reserved.
# Use of this source code is governed by the MIT
# license that can be found in the LICENSE file.

defmodule Lynx.Module.TaskModule do
@moduledoc """
User Module
"""

alias Lynx.Context.TaskContext

@doc """
Get task by UUID
"""
def get_task_by_uuid(uuid) do
case TaskContext.get_task_by_uuid(uuid) do
nil ->
{:not_found, "Task with UUID #{uuid} not found"}

task ->
{:ok, task}
end
end
end
40 changes: 40 additions & 0 deletions lib/lynx_web/controllers/task_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,44 @@ defmodule LynxWeb.TaskController do
"""

use LynxWeb, :controller

require Logger

alias Lynx.Module.TaskModule

plug :regular_user when action in [:index]

defp regular_user(conn, _opts) do
Logger.info("Validate user permissions")

if not conn.assigns[:is_logged] do
Logger.info("User doesn't have the right access permissions")

conn
|> put_status(:forbidden)
|> render("error.json", %{message: "Forbidden Access"})
|> halt
else
Logger.info("User has the right access permissions")

conn
end
end

@doc """
Index Action Endpoint
"""
def index(conn, %{"uuid" => uuid}) do
case TaskModule.get_task_by_uuid(uuid) do
{:not_found, msg} ->
conn
|> put_status(:not_found)
|> render("error.json", %{message: msg})

{:ok, task} ->
conn
|> put_status(:ok)
|> render("index.json", %{task: task})
end
end
end
28 changes: 28 additions & 0 deletions lib/lynx_web/views/task_view.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2023 Clivern. All rights reserved.
# Use of this source code is governed by the MIT
# license that can be found in the LICENSE file.

defmodule LynxWeb.TaskView do
use LynxWeb, :view

# Render task
def render("index.json", %{task: task}) do
render_task(task)
end

# Render errors
def render("error.json", %{message: message}) do
%{errorMessage: message}
end

# Format task
defp render_task(task) do
%{
id: task.uuid,
status: task.status,
runAt: task.run_at,
createdAt: task.inserted_at,
updatedAt: task.updated_at
}
end
end

0 comments on commit 500a7e7

Please sign in to comment.