philomena/lib/philomena_web/controllers/search_controller.ex

40 lines
1,002 B
Elixir
Raw Normal View History

2019-08-29 03:33:58 +02:00
defmodule PhilomenaWeb.SearchController do
use PhilomenaWeb, :controller
alias Philomena.Images.{Image, Query}
2019-11-17 03:20:33 +01:00
alias Philomena.Interactions
2019-08-29 03:33:58 +02:00
2019-08-29 03:35:01 +02:00
import Ecto.Query
2019-08-29 03:33:58 +02:00
def index(conn, params) do
2019-11-17 03:20:33 +01:00
filter = conn.assigns.compiled_filter
user = conn.assigns.current_user
2019-08-29 03:33:58 +02:00
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-11-17 03:20:33 +01:00
interactions =
Interactions.user_interactions(images, user)
2019-08-29 03:44:15 +02:00
conn
2019-11-17 03:20:33 +01:00
|> render("index.html", images: images, search_query: params["q"], interactions: interactions)
2019-08-29 03:35:01 +02:00
else
2019-11-15 16:40:10 +01:00
{:error, msg} ->
2019-08-29 03:44:15 +02:00
conn
|> render("index.html",
2019-08-29 03:35:01 +02:00
images: [],
2019-11-15 16:40:10 +01:00
error: msg,
2019-08-29 03:35:01 +02:00
search_query: params["q"]
)
end
2019-08-29 03:33:58 +02:00
end
end