Hash :
865c60de
Author :
Thomas de Grivel
Date :
2021-11-16T00:22:17
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 81 82 83 84 85 86 87 88 89 90 91 92
defmodule KmxgitWeb.Admin.OrganisationController do
use KmxgitWeb, :controller
alias Kmxgit.OrganisationManager
alias Kmxgit.OrganisationManager.Organisation
alias KmxgitWeb.ErrorView
def index(conn, _params) do
organisations = OrganisationManager.list_organisations
conn
|> render("index.html", organisations: organisations)
end
def new(conn, _params) do
changeset = OrganisationManager.change_organisation(%Organisation{})
conn
|> assign(:action, Routes.admin_organisation_path(conn, :create))
|> assign(:changeset, changeset)
|> render("new.html")
end
def create(conn, params) do
case OrganisationManager.create_organisation(params["organisation"]) do
{:ok, organisation} ->
conn
|> redirect(to: Routes.organisation_path(conn, :show, organisation.slug))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> render("new.html", changeset: changeset,
action: Routes.admin_organisation_path(conn, :create))
end
end
defp not_found(conn) do
conn
|> put_status(:not_found)
|> put_view(ErrorView)
|> render(:"404")
end
def show(conn, params) do
organisation = OrganisationManager.get_organisation(params["id"])
if organisation do
conn
|> render("show.html", organisation: organisation)
else
not_found(conn)
end
end
def edit(conn, params) do
organisation = OrganisationManager.get_organisation(params["id"])
if organisation do
changeset = OrganisationManager.change_organisation(organisation)
conn
|> render("edit.html", changeset: changeset,
action: Routes.admin_organisation_path(conn, :update, organisation))
else
not_found(conn)
end
end
def update(conn, params) do
organisation = OrganisationManager.get_organisation(params["id"])
if organisation do
case OrganisationManager.update_organisation(organisation, params["organisation"]) do
{:ok, org} ->
conn
|> redirect(to: Routes.admin_organisation_path(conn, :show, org))
{:error, changeset} ->
IO.inspect(changeset)
conn
|> render("edit.html", changeset: changeset,
action: Routes.admin_organisation_path(conn, :update, organisation))
end
else
not_found(conn)
end
end
def delete(conn, params) do
organisation = OrganisationManager.get_organisation(params["id"])
if organisation do
{:ok, _} = OrganisationManager.delete_organisation(organisation)
conn
|> redirect(to: Routes.admin_organisation_path(conn, :index))
else
not_found(conn)
end
end
end