2018-05-18 09:56:21 +02:00
|
|
|
defmodule EventosWeb.ActorController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2018-05-18 09:56:21 +02:00
|
|
|
Controller for Actors
|
2018-01-14 17:56:50 +01:00
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use EventosWeb, :controller
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors
|
|
|
|
alias Eventos.Actors.Actor
|
2018-01-13 23:33:03 +01:00
|
|
|
|
|
|
|
action_fallback EventosWeb.FallbackController
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
2018-05-18 09:56:21 +02:00
|
|
|
actors = Actors.list_actors()
|
|
|
|
render(conn, "index.json", actors: actors)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
2018-05-18 09:56:21 +02:00
|
|
|
actor = Actors.get_actor_with_everything!(id)
|
|
|
|
render(conn, "show.json", actor: actor)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
def update(conn, %{"id" => id, "actor" => actor_params}) do
|
|
|
|
actor = Actors.get_actor!(id)
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-05-18 09:56:21 +02:00
|
|
|
with {:ok, %Actor{} = actor} <- Actors.update_actor(actor, actor_params) do
|
|
|
|
render(conn, "show.json", actor: actor)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
def delete(conn, %{"id" => id_str}) do
|
|
|
|
{id, _} = Integer.parse(id_str)
|
2018-05-18 09:56:21 +02:00
|
|
|
if Guardian.Plug.current_resource(conn).actor.id == id do
|
|
|
|
actor = Actors.get_actor!(id)
|
|
|
|
with {:ok, %Actor{}} <- Actors.delete_actor(actor) do
|
2018-01-13 23:33:03 +01:00
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
send_resp(conn, 401, "")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|