2017-12-08 09:58:14 +01:00
|
|
|
defmodule EventosWeb.AccountController do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Accounts
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use EventosWeb, :controller
|
|
|
|
|
|
|
|
alias Eventos.Accounts
|
|
|
|
alias Eventos.Accounts.Account
|
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
|
|
|
|
accounts = Accounts.list_accounts()
|
2018-01-13 23:33:03 +01:00
|
|
|
render(conn, "index.json", accounts: accounts)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
2018-01-13 23:33:03 +01:00
|
|
|
account = Accounts.get_account_with_everything!(id)
|
|
|
|
render(conn, "show.json", account: account)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "account" => account_params}) do
|
|
|
|
account = Accounts.get_account!(id)
|
|
|
|
|
2018-01-13 23:33:03 +01:00
|
|
|
with {:ok, %Account{} = account} <- Accounts.update_account(account, account_params) do
|
|
|
|
render(conn, "show.json", account: account)
|
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)
|
|
|
|
if Guardian.Plug.current_resource(conn).account.id == id do
|
|
|
|
account = Accounts.get_account!(id)
|
|
|
|
with {:ok, %Account{}} <- Accounts.delete_account(account) do
|
|
|
|
send_resp(conn, :no_content, "")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
send_resp(conn, 401, "")
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
end
|