delete repeated code

This commit is contained in:
Berenice Medel 2022-01-18 11:12:44 -06:00
parent 2c709ffe2a
commit bd4d57377b
2 changed files with 3 additions and 20 deletions

View file

@ -337,28 +337,11 @@ defmodule LiveBeats.MediaLibrary do
end end
def delete_song(%Song{} = song) do def delete_song(%Song{} = song) do
case File.rm(song.mp3_filepath) do delete_song_file(song)
:ok ->
:ok
{:error, reason} ->
Logger.info(
"unable to delete song #{song.id} at #{song.mp3_filepath}, got: #{inspect(reason)}"
)
end
Ecto.Multi.new() Ecto.Multi.new()
|> Ecto.Multi.delete(:delete, song) |> Ecto.Multi.delete(:delete, song)
|> Ecto.Multi.update_all( |> update_user_songs_count(song.user_id, -1)
:update_songs_count,
fn _ ->
from(u in Accounts.User,
where: u.id == ^song.user_id,
update: [inc: [songs_count: -1]]
)
end,
[]
)
|> Repo.transaction() |> Repo.transaction()
|> case do |> case do
{:ok, _} -> :ok {:ok, _} -> :ok

View file

@ -78,7 +78,7 @@ defmodule LiveBeats.MediaLibraryTest do
%{creation_dates: creation_dates} %{creation_dates: creation_dates}
end 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] creation_dates: [one_month_ago, three_months_ago, four_months_ago]
} do } do
user = user_fixture() user = user_fixture()