philomena/lib/philomena_web/controllers/admin/artist_link_controller.ex

51 lines
1.2 KiB
Elixir
Raw Permalink Normal View History

defmodule PhilomenaWeb.Admin.ArtistLinkController do
2019-12-10 02:21:49 +01:00
use PhilomenaWeb, :controller
alias Philomena.ArtistLinks.ArtistLink
2019-12-10 02:21:49 +01:00
alias Philomena.Repo
import Ecto.Query
plug :verify_authorized
def index(conn, %{"all" => _value}) do
load_links(ArtistLink, conn)
2019-12-10 02:21:49 +01:00
end
def index(conn, %{"q" => query}) do
query = "%#{query}%"
ArtistLink
2019-12-10 02:21:49 +01:00
|> join(:inner, [ul], _ in assoc(ul, :user))
|> where([ul, u], ilike(u.name, ^query) or ilike(ul.uri, ^query))
|> load_links(conn)
end
def index(conn, _params) do
ArtistLink
2019-12-10 02:21:49 +01:00
|> where([u], u.aasm_state in ^["unverified", "link_verified", "contacted"])
|> load_links(conn)
end
defp load_links(queryable, conn) do
links =
queryable
|> order_by(desc: :created_at)
2020-01-11 05:20:19 +01:00
|> preload([
:tag,
:verified_by_user,
:contacted_by_user,
user: [:linked_tags, awards: :badge]
])
2019-12-10 02:21:49 +01:00
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", title: "Admin - Artist Links", artist_links: links)
2019-12-10 02:21:49 +01:00
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, %ArtistLink{}) do
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-10 02:21:49 +01:00
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
2019-12-16 20:24:38 +01:00
end