2018-05-30 14:27:21 +02:00
|
|
|
defmodule EventosWeb.BotController do
|
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Actors
|
2018-06-14 17:25:55 +02:00
|
|
|
alias Eventos.Actors.{Bot, Actor}
|
2018-05-30 14:27:21 +02:00
|
|
|
|
2018-07-27 10:45:35 +02:00
|
|
|
action_fallback(EventosWeb.FallbackController)
|
2018-05-30 14:27:21 +02:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
bots = Actors.list_bots()
|
|
|
|
render(conn, "index.json", bots: bots)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"bot" => bot_params}) do
|
2018-06-14 17:25:55 +02:00
|
|
|
with user <- Guardian.Plug.current_resource(conn),
|
2018-05-30 14:27:21 +02:00
|
|
|
bot_params <- Map.put(bot_params, "user_id", user.id),
|
2018-07-27 10:45:35 +02:00
|
|
|
%Actor{} = actor <-
|
|
|
|
Actors.register_bot_account(%{name: bot_params["name"], summary: bot_params["summary"]}),
|
2018-05-30 14:27:21 +02:00
|
|
|
bot_params <- Map.put(bot_params, "actor_id", actor.id),
|
|
|
|
{:ok, %Bot{} = bot} <- Actors.create_bot(bot_params) do
|
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
|
|
|
|> put_resp_header("location", bot_path(conn, :show, bot))
|
|
|
|
|> render("show.json", bot: bot)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
|
|
|
bot = Actors.get_bot!(id)
|
|
|
|
render(conn, "show.json", bot: bot)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "bot" => bot_params}) do
|
|
|
|
bot = Actors.get_bot!(id)
|
|
|
|
|
|
|
|
with {:ok, %Bot{} = bot} <- Actors.update_bot(bot, bot_params) do
|
|
|
|
render(conn, "show.json", bot: bot)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
bot = Actors.get_bot!(id)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-05-30 14:27:21 +02:00
|
|
|
with {:ok, %Bot{}} <- Actors.delete_bot(bot) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|