mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-23 20:18:00 +01:00
correctly return error on gallery unique constraint
This commit is contained in:
parent
c1d54de22a
commit
f2c1dbab0b
3 changed files with 39 additions and 17 deletions
|
@ -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
|
||||
|
|
|
@ -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}
|
||||
|
|
|
@ -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
|
||||
|
||||
{:ok, _gallery} = Galleries.add_image_to_gallery(gallery, image)
|
||||
Galleries.notify_gallery(gallery)
|
||||
Galleries.reindex_gallery(gallery)
|
||||
Images.reindex_image(image)
|
||||
|
||||
case Galleries.add_image_to_gallery(conn.assigns.gallery, conn.assigns.image) do
|
||||
{:ok, _gallery} ->
|
||||
json(conn, %{})
|
||||
|
||||
_error ->
|
||||
conn
|
||||
|> put_status(:bad_request)
|
||||
|> json(%{})
|
||||
end
|
||||
end
|
||||
|
||||
def delete(conn, _params) do
|
||||
gallery = conn.assigns.gallery
|
||||
image = conn.assigns.image
|
||||
|
||||
{:ok, _gallery} = Galleries.remove_image_from_gallery(gallery, image)
|
||||
Galleries.reindex_gallery(gallery)
|
||||
Images.reindex_image(image)
|
||||
|
||||
case Galleries.remove_image_from_gallery(conn.assigns.gallery, conn.assigns.image) do
|
||||
{:ok, _gallery} ->
|
||||
json(conn, %{})
|
||||
|
||||
_error ->
|
||||
conn
|
||||
|> put_status(:bad_request)
|
||||
|> json(%{})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue