Add random page

This commit is contained in:
Shadowfacts 2020-08-15 15:51:52 -04:00
parent 76ddd3dcb5
commit 5235403a19
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 10 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -16,6 +16,7 @@
<li><a href="/">Home</a></li>
<%= if @current_user do %>
<li><%= link "New Page", to: Routes.page_path(@conn, :new) %></li>
<li><%= link "Random", to: Routes.page_path(@conn, :random) %></li>
<% end %>
<%= if function_exported?(Routes, :live_dashboard_path, 2) do %>
<li><%= link "LiveDashboard", to: Routes.live_dashboard_path(@conn, :home) %></li>