philomena/lib/philomena_web/controllers/page_controller.ex

57 lines
1.8 KiB
Elixir
Raw Permalink Normal View History

2019-08-15 02:32:32 +02:00
defmodule PhilomenaWeb.PageController do
use PhilomenaWeb, :controller
2019-11-29 07:35:15 +01:00
alias Philomena.StaticPages.StaticPage
2019-12-15 18:33:37 +01:00
alias Philomena.StaticPages
alias PhilomenaWeb.MarkdownRenderer
2019-11-29 07:35:15 +01:00
2019-12-15 18:33:37 +01:00
plug :load_and_authorize_resource, model: StaticPage, id_field: "slug"
def index(conn, _params) do
2019-12-16 20:24:38 +01:00
render(conn, "index.html", title: "Pages")
2019-12-15 18:33:37 +01:00
end
2019-11-29 07:35:15 +01:00
def show(conn, _params) do
rendered = MarkdownRenderer.render_unsafe(conn.assigns.static_page.body, conn)
render(conn, "show.html", title: conn.assigns.static_page.title, rendered: rendered)
2019-12-15 18:33:37 +01:00
end
def new(conn, _params) do
changeset = StaticPages.change_static_page(%StaticPage{})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New Page", changeset: changeset)
2019-12-15 18:33:37 +01:00
end
def create(conn, %{"static_page" => static_page_params}) do
case StaticPages.create_static_page(conn.assigns.current_user, static_page_params) do
{:ok, %{static_page: static_page}} ->
conn
|> put_flash(:info, "Static page successfully created.")
|> redirect(to: ~p"/pages/#{static_page}")
2019-12-15 18:33:37 +01:00
{:error, :static_page, changeset, _changes} ->
render(conn, "new.html", changeset: changeset)
end
end
def edit(conn, _params) do
changeset = StaticPages.change_static_page(conn.assigns.static_page)
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing Page", changeset: changeset)
2019-12-15 18:33:37 +01:00
end
def update(conn, %{"static_page" => static_page_params}) do
2020-01-11 05:20:19 +01:00
case StaticPages.update_static_page(
conn.assigns.static_page,
conn.assigns.current_user,
static_page_params
) do
2019-12-15 18:33:37 +01:00
{:ok, %{static_page: static_page}} ->
conn
|> put_flash(:info, "Static page successfully updated.")
|> redirect(to: ~p"/pages/#{static_page}")
2019-12-15 18:33:37 +01:00
{:error, :static_page, changeset, _changes} ->
render(conn, "edit.html", changeset: changeset)
end
2019-08-15 02:32:32 +02:00
end
end