philomena/lib/philomena_web/controllers/image/favorite_controller.ex

33 lines
786 B
Elixir
Raw Normal View History

2019-12-20 18:26:38 +01:00
defmodule PhilomenaWeb.Image.FavoriteController do
use PhilomenaWeb, :controller
alias Philomena.Images.Image
alias Philomena.Repo
2020-01-11 05:20:19 +01:00
plug :load_and_authorize_resource,
model: Image,
id_name: "image_id",
persisted: true,
preload: [faves: :user]
2019-12-20 18:26:38 +01:00
plug :load_votes_if_authorized
def index(conn, _params) do
render(conn, "index.html", layout: false)
end
defp load_votes_if_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :tamper, conn.assigns.image) do
2019-12-20 18:26:38 +01:00
true ->
2019-12-30 13:50:02 +01:00
image = Repo.preload(conn.assigns.image, upvotes: :user, downvotes: :user, hides: :user)
2019-12-20 18:26:38 +01:00
conn
|> assign(:image, image)
|> assign(:has_votes, true)
false ->
assign(conn, :has_votes, false)
end
end
end