Hash :
7d4e6756
Author :
Thomas de Grivel
Date :
2021-11-18T23:06:28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
defmodule KmxgitWeb.OrganisationController do
use KmxgitWeb, :controller
alias Kmxgit.OrganisationManager
def new(conn, _params) do
_ = conn.assigns.current_user
changeset = OrganisationManager.change_organisation
conn
|> assign(:action, Routes.organisation_path(conn, :create))
|> assign(:changeset, changeset)
|> render("new.html")
end
def create(conn, params) do
current_user = conn.assigns.current_user
case OrganisationManager.create_organisation(current_user, params["organisation"]) do
{:ok, organisation} ->
conn
|> redirect(to: Routes.slug_path(conn, :show, organisation.slug.slug))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> assign(:action, Routes.organisation_path(conn, :create))
|> assign(:changeset, changeset)
|> render("new.html")
end
end
defp not_found(conn) do
conn
|> put_status(:not_found)
|> put_view(ErrorView)
|> render(:"404")
end
def show(conn, params) do
org = OrganisationManager.get_organisation_by_slug(params["slug"])
if org do
conn
|> assign(:current_organisation, org)
|> assign(:org, org)
|> render("show.html")
else
not_found(conn)
end
end
def edit(conn, params) do
org = OrganisationManager.get_organisation_by_slug(params["slug"])
changeset = OrganisationManager.change_organisation(org)
if org do
conn
|> assign(:action, Routes.organisation_path(conn, :update, org.slug.slug))
|> assign(:changeset, changeset)
|> assign(:current_organisation, org)
|> render("edit.html")
else
not_found(conn)
end
end
def update(conn, params) do
organisation = OrganisationManager.get_organisation_by_slug(params["slug"])
if organisation do
case OrganisationManager.update_organisation(organisation, params["organisation"]) do
{:ok, org} ->
conn
|> redirect(to: Routes.slug_path(conn, :show, org.slug.slug))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> render("edit.html", changeset: changeset,
action: Routes.organisation_path(conn, :update, organisation.slug.slug))
end
else
not_found(conn)
end
end
end