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
|
|
|
|
|
|
|
plug ImageFilter
|
|
|
|
|
|
|
|
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
|
|
|
%{
|
|
|
|
query: %{bool: %{must: query, must_not: filter}},
|
|
|
|
sort: %{created_at: :desc}
|
|
|
|
},
|
|
|
|
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
|