2018-10-11 17:37:39 +02:00
|
|
|
defmodule MobilizonWeb.GroupController do
|
2018-05-30 18:59:13 +02:00
|
|
|
@moduledoc """
|
|
|
|
Controller for Groups
|
|
|
|
"""
|
2018-10-11 17:37:39 +02:00
|
|
|
use MobilizonWeb, :controller
|
2018-05-30 18:59:13 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Actors
|
|
|
|
alias Mobilizon.Actors.{Actor, Member}
|
2018-05-30 18:59:13 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
action_fallback(MobilizonWeb.FallbackController)
|
2018-05-30 18:59:13 +02:00
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
groups = Actors.list_groups()
|
2018-10-11 17:37:39 +02:00
|
|
|
render(conn, MobilizonWeb.ActorView, "index.json", actors: groups)
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"group" => group_params}) do
|
2018-08-24 11:34:00 +02:00
|
|
|
with {:ok, %Actor{} = group} <- Actors.create_group(group_params),
|
|
|
|
{:ok, %Member{} = member} <-
|
|
|
|
Actors.create_member(%{
|
|
|
|
"parent_id" => group.id,
|
|
|
|
"actor_id" => Actors.get_local_actor_by_name(group_params["actor_admin"]).id,
|
|
|
|
"role" => 2
|
|
|
|
}) do
|
2018-05-30 18:59:13 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
|
|
|
|> put_resp_header("location", actor_path(conn, :show, group))
|
2018-10-11 17:37:39 +02:00
|
|
|
|> render(MobilizonWeb.ActorView, "actor_basic.json", actor: group)
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|
|
|
|
end
|
2018-06-06 17:42:53 +02:00
|
|
|
|
2018-08-01 14:45:18 +02:00
|
|
|
def join(conn, %{"name" => group_name, "actor_name" => actor_name}) do
|
2018-08-03 10:16:22 +02:00
|
|
|
with %Actor{} = group <- Actors.get_group_by_name(group_name),
|
|
|
|
%Actor{} = actor <- Actors.get_local_actor_by_name(actor_name),
|
2018-08-24 11:34:00 +02:00
|
|
|
{:ok, %Member{} = member} <-
|
2018-07-27 10:45:35 +02:00
|
|
|
Actors.create_member(%{"parent_id" => group.id, "actor_id" => actor.id}) do
|
2018-06-06 17:42:53 +02:00
|
|
|
conn
|
|
|
|
|> put_status(:created)
|
2018-10-11 17:37:39 +02:00
|
|
|
|> render(MobilizonWeb.MemberView, "member.json", member: member)
|
2018-06-06 17:42:53 +02:00
|
|
|
else
|
2018-08-03 10:16:22 +02:00
|
|
|
nil ->
|
|
|
|
conn
|
|
|
|
|> put_status(:not_found)
|
2018-10-11 17:47:02 +02:00
|
|
|
|> render(MobilizonWeb.ErrorView, "not_found.json",
|
|
|
|
details: "group or actor doesn't exist"
|
|
|
|
)
|
2018-08-03 10:19:28 +02:00
|
|
|
|
2018-06-06 17:42:53 +02:00
|
|
|
err ->
|
2018-08-01 14:45:18 +02:00
|
|
|
require Logger
|
2018-07-27 10:45:35 +02:00
|
|
|
Logger.debug(inspect(err))
|
2018-06-06 17:42:53 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-30 18:59:13 +02:00
|
|
|
end
|