From 285cc8d28bc0cb42aed0a85b61b3b34a6b627264 Mon Sep 17 00:00:00 2001 From: neodarz Date: Fri, 17 Feb 2017 10:23:19 +0100 Subject: Add test if file is delete in database before delete the file --- web/controllers/audio_file_controller.ex | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/web/controllers/audio_file_controller.ex b/web/controllers/audio_file_controller.ex index 2c34e86..784c92c 100644 --- a/web/controllers/audio_file_controller.ex +++ b/web/controllers/audio_file_controller.ex @@ -78,15 +78,22 @@ defmodule TheTranscriberBackend.AudioFileController do end def delete(conn, %{"id" => id}) do + path = "/media/phoenix_test/" audio_file = Repo.get!(AudioFile, id) + cond do + audio_file !=nil -> + # Here we use delete! (with a bang) because we expect + # it to always work (and if it does not, it will raise). + File.rm("#{path}#{audio_file.id}_#{audio_file.audio_path}") + Repo.delete!(audio_file) - # Here we use delete! (with a bang) because we expect - # it to always work (and if it does not, it will raise). - File.rm(audio_file.audio_path) - Repo.delete!(audio_file) - - conn - |> put_flash(:info, "Audio file deleted successfully.") - |> redirect(to: audio_file_path(conn, :index)) + conn + |> put_flash(:info, "Audio file deleted successfully.") + |> redirect(to: audio_file_path(conn, :index)) + audio_file == nil -> + conn + |> put_flash(:info, "File not found in database !") + |> redirect(to: audio_file_path(conn, :index)) + end end end -- cgit v1.2.1