philomena/lib/philomena_web/controllers/search_controller.ex

25 lines
755 B
Elixir
Raw Normal View History

2019-08-29 03:33:58 +02:00
defmodule PhilomenaWeb.SearchController do
use PhilomenaWeb, :controller
2019-12-01 03:22:05 +01:00
alias PhilomenaWeb.ImageLoader
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
def index(conn, params) do
2019-11-17 03:20:33 +01:00
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
2019-12-01 03:22:05 +01:00
case ImageLoader.search_string(conn, params["q"], sorts: sort.sorts, queries: sort.queries) do
{:ok, images} ->
interactions =
Interactions.user_interactions(images, user)
2019-08-29 03:33:58 +02:00
2019-12-01 03:22:05 +01:00
conn
|> render("index.html", images: images, search_query: params["q"], interactions: interactions, layout_class: "layout--wide")
2019-11-17 03:20:33 +01:00
2019-11-15 16:40:10 +01:00
{:error, msg} ->
2019-12-01 03:22:05 +01:00
render(conn, "index.html", images: [], error: msg, search_query: params["q"])
2019-08-29 03:35:01 +02:00
end
2019-08-29 03:33:58 +02:00
end
end