philomena/lib/philomena_web/controllers/search_controller.ex

25 lines
853 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
2019-12-05 22:57:59 +01:00
{:ok, {images, tags}} ->
2019-12-01 03:22:05 +01:00
interactions =
Interactions.user_interactions(images, user)
2019-08-29 03:33:58 +02:00
2019-12-01 03:22:05 +01:00
conn
2019-12-16 20:24:38 +01:00
|> render("index.html", title: "Searching for #{params["q"]}", images: images, tags: tags, 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-16 20:24:38 +01:00
render(conn, "index.html", title: "Searching for #{params["q"]}", 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