2019-11-17 00:01:42 +01:00
|
|
|
defmodule PhilomenaWeb.Image.VoteController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-11-17 03:20:33 +01:00
|
|
|
alias Philomena.{Images, Images.Image}
|
|
|
|
alias Philomena.ImageVotes
|
|
|
|
alias Philomena.Repo
|
|
|
|
alias Ecto.Multi
|
2019-11-17 00:01:42 +01:00
|
|
|
|
|
|
|
plug PhilomenaWeb.Plugs.FilterBannedUsers
|
2019-11-17 03:53:41 +01:00
|
|
|
plug PhilomenaWeb.Plugs.CanaryMapPlug, create: :vote, delete: :vote
|
2019-11-17 00:01:42 +01:00
|
|
|
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
|
|
|
|
|
2019-11-17 03:20:33 +01:00
|
|
|
def create(conn, params) do
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
|
|
|
Multi.append(
|
|
|
|
ImageVotes.delete_vote_transaction(image, user),
|
|
|
|
ImageVotes.create_vote_transaction(image, user, params["up"] == true)
|
|
|
|
)
|
|
|
|
|> Repo.transaction()
|
|
|
|
|> case do
|
|
|
|
{:ok, _result} ->
|
|
|
|
image =
|
|
|
|
Images.get_image!(image.id)
|
|
|
|
|> Images.reindex_image()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> json(Image.interaction_data(image))
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.put_status(409)
|
|
|
|
|> json(%{})
|
|
|
|
end
|
2019-11-17 00:01:42 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
2019-11-17 03:20:33 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
image = conn.assigns.image
|
|
|
|
|
|
|
|
ImageVotes.delete_vote_transaction(image, user)
|
|
|
|
|> Repo.transaction()
|
|
|
|
|> case do
|
|
|
|
{:ok, _result} ->
|
|
|
|
image =
|
|
|
|
Images.get_image!(image.id)
|
|
|
|
|> Images.reindex_image()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> json(Image.interaction_data(image))
|
|
|
|
|
|
|
|
_error ->
|
|
|
|
conn
|
|
|
|
|> Plug.Conn.put_status(409)
|
|
|
|
|> json(%{})
|
|
|
|
end
|
2019-11-17 00:01:42 +01:00
|
|
|
end
|
|
|
|
end
|