philomena/lib/philomena_web/controllers/search_controller.ex

37 lines
1,004 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-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-08-29 03:33:58 +02:00
case ImageLoader.search_string(conn, params["q"], include_hits: custom_ordering?(conn)) do
2019-12-05 22:57:59 +01:00
{:ok, {images, tags}} ->
2020-01-11 05:20:19 +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
2020-01-11 05:20:19 +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} ->
2020-01-11 05:20:19 +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
defp custom_ordering?(%{params: %{"sf" => sf}}) when sf != "id", do: true
defp custom_ordering?(_conn), do: false
2019-08-29 03:33:58 +02:00
end