philomena/lib/philomena_web/controllers/image_controller.ex

45 lines
1.1 KiB
Elixir
Raw Normal View History

2019-08-18 18:17:05 +02:00
defmodule PhilomenaWeb.ImageController do
use PhilomenaWeb, :controller
2019-08-19 04:03:12 +02:00
alias Philomena.{Images, Images.Image}
import Ecto.Query
2019-08-18 18:17:05 +02:00
2019-08-29 03:14:54 +02:00
plug ImageFilter
2019-08-18 18:17:05 +02:00
def index(conn, _params) do
2019-08-29 03:14:54 +02:00
query = conn.assigns[:compiled_filter]
2019-08-19 04:03:12 +02:00
images =
Image.search_records(
%{
2019-08-29 03:14:54 +02:00
query: %{bool: %{must_not: query}},
sort: %{created_at: :desc}
2019-08-19 04:03:12 +02:00
},
Image |> preload(:tags)
)
2019-08-18 18:17:05 +02:00
render(conn, "index.html", images: images)
end
def new(conn, _params) do
changeset = Images.change_image(%Image{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"image" => image_params}) do
case Images.create_image(image_params) do
{:ok, image} ->
conn
|> put_flash(:info, "Image created successfully.")
|> redirect(to: Routes.image_path(conn, :show, image))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
image = Images.get_image!(id)
render(conn, "show.html", image: image)
end
end