2019-08-29 03:33:58 +02:00
|
|
|
defmodule PhilomenaWeb.SearchController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Images.{Image, Query}
|
|
|
|
alias Pow.Plug
|
|
|
|
|
2019-08-29 03:35:01 +02:00
|
|
|
import Ecto.Query
|
2019-08-29 03:33:58 +02:00
|
|
|
|
|
|
|
def index(conn, params) do
|
|
|
|
filter = conn.assigns[:compiled_filter]
|
|
|
|
user = conn |> Plug.current_user()
|
|
|
|
|
|
|
|
with {:ok, query} <- Query.compile(user, params["q"]) do
|
2019-08-29 03:35:01 +02:00
|
|
|
images =
|
2019-08-29 03:33:58 +02:00
|
|
|
Image.search_records(
|
2019-08-29 03:35:01 +02:00
|
|
|
%{
|
2019-10-06 23:45:26 +02:00
|
|
|
query: %{bool: %{must: query, must_not: [filter, %{term: %{hidden_from_users: true}}]}},
|
2019-08-29 03:35:01 +02:00
|
|
|
sort: %{created_at: :desc}
|
|
|
|
},
|
2019-10-09 01:19:57 +02:00
|
|
|
conn.assigns.pagination,
|
2019-08-29 03:35:01 +02:00
|
|
|
Image |> preload(:tags)
|
2019-08-29 03:33:58 +02:00
|
|
|
)
|
|
|
|
|
2019-08-29 03:44:15 +02:00
|
|
|
conn
|
|
|
|
|> put_view(PhilomenaWeb.ImageView)
|
|
|
|
|> render("index.html", images: images, search_query: params["q"])
|
2019-08-29 03:35:01 +02:00
|
|
|
else
|
|
|
|
{:error, msg} ->
|
2019-08-29 03:44:15 +02:00
|
|
|
conn
|
|
|
|
|> put_view(PhilomenaWeb.ImageView)
|
|
|
|
|> render("index.html",
|
2019-08-29 03:35:01 +02:00
|
|
|
images: [],
|
|
|
|
error: msg,
|
|
|
|
search_query: params["q"]
|
|
|
|
)
|
|
|
|
end
|
2019-08-29 03:33:58 +02:00
|
|
|
end
|
|
|
|
end
|