philomena/lib/philomena_web/controllers/search_controller.ex

50 lines
1.3 KiB
Elixir
Raw Permalink 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
alias Philomena.Images.Image
alias PhilomenaQuery.Search
2019-11-17 03:20:33 +01:00
alias Philomena.Interactions
import Ecto.Query
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"]) do
2019-12-05 22:57:59 +01:00
{:ok, {images, tags}} ->
images =
search_function(custom_ordering?(conn)).(
images,
preload(Image, [:sources, tags: :aliases])
)
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: [],
2024-04-08 19:03:20 +02:00
tags: [],
2020-01-11 05:20:19 +01:00
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 search_function(true), do: &Search.search_records_with_hits/2
defp search_function(_custom), do: &Search.search_records/2
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