philomena/lib/philomena_web/controllers/search_controller.ex

43 lines
1.1 KiB
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-30 03:33:15 +01:00
alias Philomena.ImageSorter
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-11-30 03:33:15 +01:00
sort = ImageSorter.parse_sort(params)
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-11-30 03:33:15 +01:00
query: %{bool: %{must: [query | sort.queries], must_not: [filter, %{term: %{hidden_from_users: true}}]}},
sort: sort.sorts
2019-08-29 03:35:01 +02:00
},
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 23:51:14 +01:00
|> render("index.html", images: images, search_query: params["q"], interactions: interactions, layout_class: "layout--wide")
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
2019-11-18 15:14:26 +01:00
2019-08-29 03:33:58 +02:00
end