correctly return error on gallery unique constraint

This commit is contained in:
byte[] 2020-09-07 23:47:19 -04:00
parent c1d54de22a
commit f2c1dbab0b
3 changed files with 39 additions and 17 deletions

View file

@ -169,6 +169,17 @@ defmodule Philomena.Galleries do
{:ok, count} {:ok, count}
end) end)
|> Repo.transaction() |> Repo.transaction()
|> case do
{:ok, result} ->
Images.reindex_image(image)
notify_gallery(gallery)
reindex_gallery(gallery)
{:ok, result}
error ->
error
end
end end
def remove_image_from_gallery(gallery, image) do def remove_image_from_gallery(gallery, image) do
@ -201,6 +212,16 @@ defmodule Philomena.Galleries do
{:ok, count} {:ok, count}
end) end)
|> Repo.transaction() |> Repo.transaction()
|> case do
{:ok, result} ->
Images.reindex_image(image)
reindex_gallery(gallery)
{:ok, result}
error ->
error
end
end end
defp last_position(gallery_id) do defp last_position(gallery_id) do

View file

@ -19,6 +19,7 @@ defmodule Philomena.Galleries.Interaction do
|> cast(attrs, [:image_id, :position]) |> cast(attrs, [:image_id, :position])
|> validate_required([:image_id, :position]) |> validate_required([:image_id, :position])
|> foreign_key_constraint(:image_id, name: :fk_rails_bb5ebe2a77) |> foreign_key_constraint(:image_id, name: :fk_rails_bb5ebe2a77)
|> unique_constraint(:image_id, name: :index_gallery_interactions_on_gallery_id_and_image_id)
|> case do |> case do
%{valid?: false, changes: changes} = changeset when changes == %{} -> %{valid?: false, changes: changes} = changeset when changes == %{} ->
%{changeset | action: :ignore} %{changeset | action: :ignore}

View file

@ -1,10 +1,9 @@
defmodule PhilomenaWeb.Gallery.ImageController do defmodule PhilomenaWeb.Gallery.ImageController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias Philomena.Images.Image
alias Philomena.Galleries.Gallery alias Philomena.Galleries.Gallery
alias Philomena.Images
alias Philomena.Galleries alias Philomena.Galleries
alias Philomena.Images.Image
plug PhilomenaWeb.FilterBannedUsersPlug plug PhilomenaWeb.FilterBannedUsersPlug
@ -15,25 +14,26 @@ defmodule PhilomenaWeb.Gallery.ImageController do
plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true
def create(conn, _params) do def create(conn, _params) do
gallery = conn.assigns.gallery case Galleries.add_image_to_gallery(conn.assigns.gallery, conn.assigns.image) do
image = conn.assigns.image {:ok, _gallery} ->
json(conn, %{})
{:ok, _gallery} = Galleries.add_image_to_gallery(gallery, image) _error ->
Galleries.notify_gallery(gallery) conn
Galleries.reindex_gallery(gallery) |> put_status(:bad_request)
Images.reindex_image(image) |> json(%{})
end
json(conn, %{})
end end
def delete(conn, _params) do def delete(conn, _params) do
gallery = conn.assigns.gallery case Galleries.remove_image_from_gallery(conn.assigns.gallery, conn.assigns.image) do
image = conn.assigns.image {:ok, _gallery} ->
json(conn, %{})
{:ok, _gallery} = Galleries.remove_image_from_gallery(gallery, image) _error ->
Galleries.reindex_gallery(gallery) conn
Images.reindex_image(image) |> put_status(:bad_request)
|> json(%{})
json(conn, %{}) end
end end
end end