mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-12-17 22:47:59 +01:00
add primitive search controller
This commit is contained in:
parent
7bda48cbfa
commit
1a44260211
2 changed files with 32 additions and 0 deletions
31
lib/philomena_web/controllers/search_controller.ex
Normal file
31
lib/philomena_web/controllers/search_controller.ex
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
defmodule PhilomenaWeb.SearchController do
|
||||||
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
|
alias Philomena.Images.{Image, Query}
|
||||||
|
alias Pow.Plug
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
|
plug ImageFilter
|
||||||
|
|
||||||
|
def index(conn, params) do
|
||||||
|
filter = conn.assigns[:compiled_filter]
|
||||||
|
user = conn |> Plug.current_user()
|
||||||
|
|
||||||
|
with {:ok, query} <- Query.compile(user, params["q"]) do
|
||||||
|
images =
|
||||||
|
Image.search_records(
|
||||||
|
%{
|
||||||
|
query: %{bool: %{must: query, must_not: filter}},
|
||||||
|
sort: %{created_at: :desc}
|
||||||
|
},
|
||||||
|
Image |> preload(:tags)
|
||||||
|
)
|
||||||
|
|
||||||
|
render(conn, PhilomenaWeb.ImageView, "index.html", images: images, search_query: params["q"])
|
||||||
|
else
|
||||||
|
{:error, msg} ->
|
||||||
|
render(conn, PhilomenaWeb.ImageView, "index.html", images: [], error: msg, search_query: params["q"])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -27,6 +27,7 @@ defmodule PhilomenaWeb.Router do
|
||||||
|
|
||||||
resources "/images", ImageController, only: [:index, :show]
|
resources "/images", ImageController, only: [:index, :show]
|
||||||
resources "/tags", TagController, only: [:index, :show]
|
resources "/tags", TagController, only: [:index, :show]
|
||||||
|
resources "/search", SearchController, only: [:index]
|
||||||
|
|
||||||
get "/:id", ImageController, :show
|
get "/:id", ImageController, :show
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue