From 235f4ddd96d24c0f1d2218e73a6e6c1116beb713 Mon Sep 17 00:00:00 2001 From: Robert Long Date: Thu, 5 Sep 2019 17:10:35 -0700 Subject: [PATCH] Check for existing project asset before adding it --- .../api/v1/project_assets_controller.ex | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/ret_web/controllers/api/v1/project_assets_controller.ex b/lib/ret_web/controllers/api/v1/project_assets_controller.ex index 8fbef49a8..c234d6888 100644 --- a/lib/ret_web/controllers/api/v1/project_assets_controller.ex +++ b/lib/ret_web/controllers/api/v1/project_assets_controller.ex @@ -19,10 +19,21 @@ defmodule RetWeb.Api.V1.ProjectAssetsController do account = Guardian.Plug.current_resource(conn) with %Project{} = project <- Project.project_by_sid_for_account(project_sid, account), - %Asset{} = asset <- Asset.asset_by_sid_for_account(asset_sid, account), - {:ok, _} <- Project.add_asset_to_project(project, asset) do - asset = Repo.preload(asset, [:asset_owned_file, :thumbnail_owned_file]) - render(conn, "show.json", asset: asset) + %Asset{} = asset <- Asset.asset_by_sid_for_account(asset_sid, account) do + + project_asset = Repo.get_by(ProjectAsset, [project_id: project.project_id, asset_id: asset.asset_id]) + + if is_nil(project_asset) do + case Project.add_asset_to_project(project, asset) do + {:error, error} -> render_error_json(conn, error) + %ProjectAsset{} = project_asset -> + asset = Repo.preload(asset, [:asset_owned_file, :thumbnail_owned_file]) + render(conn, "show.json", asset: asset) + end + else + asset = Repo.preload(asset, [:asset_owned_file, :thumbnail_owned_file]) + render(conn, "show.json", asset: asset) + end else {:error, error} -> render_error_json(conn, error) nil -> render_error_json(conn, :not_found)