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

View file

@ -19,6 +19,7 @@ defmodule Philomena.Galleries.Interaction do
|> cast(attrs, [:image_id, :position])
|> validate_required([:image_id, :position])
|> 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
%{valid?: false, changes: changes} = changeset when changes == %{} ->
%{changeset | action: :ignore}

View file

@ -1,10 +1,9 @@
defmodule PhilomenaWeb.Gallery.ImageController do
use PhilomenaWeb, :controller
alias Philomena.Images.Image
alias Philomena.Galleries.Gallery
alias Philomena.Images
alias Philomena.Galleries
alias Philomena.Images.Image
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
def create(conn, _params) do
gallery = conn.assigns.gallery
image = conn.assigns.image
case Galleries.add_image_to_gallery(conn.assigns.gallery, conn.assigns.image) do
{:ok, _gallery} ->
json(conn, %{})
{:ok, _gallery} = Galleries.add_image_to_gallery(gallery, image)
Galleries.notify_gallery(gallery)
Galleries.reindex_gallery(gallery)
Images.reindex_image(image)
json(conn, %{})
_error ->
conn
|> put_status(:bad_request)
|> json(%{})
end
end
def delete(conn, _params) do
gallery = conn.assigns.gallery
image = conn.assigns.image
case Galleries.remove_image_from_gallery(conn.assigns.gallery, conn.assigns.image) do
{:ok, _gallery} ->
json(conn, %{})
{:ok, _gallery} = Galleries.remove_image_from_gallery(gallery, image)
Galleries.reindex_gallery(gallery)
Images.reindex_image(image)
json(conn, %{})
_error ->
conn
|> put_status(:bad_request)
|> json(%{})
end
end
end