From bd4d57377b616a5f763d1d26a52126c0b67537bc Mon Sep 17 00:00:00 2001 From: Berenice Medel Date: Tue, 18 Jan 2022 11:12:44 -0600 Subject: [PATCH] delete repeated code --- lib/live_beats/media_library.ex | 21 ++------------------- test/live_beats/media_library_test.exs | 2 +- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/lib/live_beats/media_library.ex b/lib/live_beats/media_library.ex index 3d6101c..767d5c5 100644 --- a/lib/live_beats/media_library.ex +++ b/lib/live_beats/media_library.ex @@ -337,28 +337,11 @@ defmodule LiveBeats.MediaLibrary do end def delete_song(%Song{} = song) do - case File.rm(song.mp3_filepath) do - :ok -> - :ok - - {:error, reason} -> - Logger.info( - "unable to delete song #{song.id} at #{song.mp3_filepath}, got: #{inspect(reason)}" - ) - end + delete_song_file(song) Ecto.Multi.new() |> Ecto.Multi.delete(:delete, song) - |> Ecto.Multi.update_all( - :update_songs_count, - fn _ -> - from(u in Accounts.User, - where: u.id == ^song.user_id, - update: [inc: [songs_count: -1]] - ) - end, - [] - ) + |> update_user_songs_count(song.user_id, -1) |> Repo.transaction() |> case do {:ok, _} -> :ok diff --git a/test/live_beats/media_library_test.exs b/test/live_beats/media_library_test.exs index ec9e72a..66eae19 100644 --- a/test/live_beats/media_library_test.exs +++ b/test/live_beats/media_library_test.exs @@ -78,7 +78,7 @@ defmodule LiveBeats.MediaLibraryTest do %{creation_dates: creation_dates} end - test "deletes the song expired before the required interval", %{ + test "deletes the songs expired before the required interval", %{ creation_dates: [one_month_ago, three_months_ago, four_months_ago] } do user = user_fixture()