diff --git a/lib/wiki_web/controllers/page_controller.ex b/lib/wiki_web/controllers/page_controller.ex index 09659f6..d94d2f1 100644 --- a/lib/wiki_web/controllers/page_controller.ex +++ b/lib/wiki_web/controllers/page_controller.ex @@ -4,6 +4,7 @@ defmodule WikiWeb.PageController do alias Wiki.Repo alias Wiki.Content alias Wiki.Content.{Page, Upload} + import Ecto.Query plug :get_page when action in [ @@ -163,4 +164,11 @@ defmodule WikiWeb.PageController do |> put_flash(:info, "Page deleted successfully.") |> redirect(to: Routes.page_path(conn, :index)) end + + def random(conn, _params) do + user = conn.assigns.current_user + page_ids = Repo.all(from p in Page, where: p.user_id == ^user.id, select: p.id) + random_page = Enum.random(page_ids) + redirect(conn, to: Routes.page_path(conn, :show, random_page)) + end end diff --git a/lib/wiki_web/router.ex b/lib/wiki_web/router.ex index 5bc701d..f93dc1c 100644 --- a/lib/wiki_web/router.ex +++ b/lib/wiki_web/router.ex @@ -62,6 +62,7 @@ defmodule WikiWeb.Router do resources "/pages", PageController, except: [:index] get "/", PageController, :index + get "/random", PageController, :random post "/pages/:id/uploads", PageController, :create_upload get "/pages/:id/uploads/:upload_id", PageController, :get_upload delete "/pages/:id/uploads/:upload_id", PageController, :delete_upload diff --git a/lib/wiki_web/templates/layout/app.html.eex b/lib/wiki_web/templates/layout/app.html.eex index 0043046..ef1f233 100644 --- a/lib/wiki_web/templates/layout/app.html.eex +++ b/lib/wiki_web/templates/layout/app.html.eex @@ -16,6 +16,7 @@
  • Home
  • <%= if @current_user do %>
  • <%= link "New Page", to: Routes.page_path(@conn, :new) %>
  • +
  • <%= link "Random", to: Routes.page_path(@conn, :random) %>
  • <% end %> <%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
  • <%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %>