From 0cf1bf1c2d834cb4f147dbd174da0a5b6d60acd5 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Wed, 22 May 2019 20:37:14 -0400 Subject: [PATCH] Added functionality to delete empty directorie after updating songs #45 --- Controllers/Managers/DirectoryManager.cs | 42 ++++++++++++++++++ Controllers/Managers/SongManager.cs | 55 ++++++++++++++++++------ Controllers/SongController.cs | 5 +-- 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/Controllers/Managers/DirectoryManager.cs b/Controllers/Managers/DirectoryManager.cs index 2d11c5e..6cbe8db 100644 --- a/Controllers/Managers/DirectoryManager.cs +++ b/Controllers/Managers/DirectoryManager.cs @@ -87,7 +87,49 @@ namespace Icarus.Controllers.Managers Console.WriteLine($"An error occurred {exMsg}"); } } + public void DeleteEmptyDirectories(Song song) + { + try + { + var albumDirectory = AlbumDirectory(song); + var artistDirectory = ArtistDirectory(song); + if (IsDirectoryEmpty(albumDirectory)) + { + Directory.Delete(albumDirectory); + _logger.Info("Album directory deleted"); + } + if (IsDirectoryEmpty(artistDirectory)) + { + Directory.Delete(artistDirectory); + _logger.Info("Artist directory deleted"); + } + } + catch (Exception ex) + { + var msg = ex.Message; + _logger.Error(msg, "An error occurred"); + } + } + + public string RetrieveAlbumPath(Song song) + { + _logger.Info("Retrieving album song path"); + + var albumPath = string.Empty; + albumPath = AlbumDirectory(song); + + return albumPath; + } + public string RetrieveArtistPath(Song song) + { + _logger.Info("Retrieving artist path"); + + var artistPath = string.Empty; + artistPath = ArtistDirectory(song); + + return artistPath; + } public string GenerateSongPath(Song song) { _logger.Info("Generating song path"); diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 9cbdb39..15181f8 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -94,11 +94,12 @@ namespace Icarus.Controllers.Managers #region Methods - // TODO: This method should update the Song, Album, and Artist records in the database public SongResult UpdateSong(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore, ArtistStoreContext artistStore, GenreStoreContext genreStore, YearStoreContext yearStore) { + var result = new SongResult(); + try { var oldSongRecord = songStore.GetSong(song); @@ -109,7 +110,6 @@ namespace Icarus.Controllers.Managers var updatedSong = updateMetadata.UpdatedSongRecord; - // TODO: Add the following methods var updatedAlbum = UpdateAlbumInDatabase(oldSongRecord, updatedSong, albumStore); oldSongRecord.AlbumId = updatedAlbum.AlbumId; var updatedArtist = UpdateArtistInDatabase(oldSongRecord, updatedSong, artistStore); @@ -119,15 +119,20 @@ namespace Icarus.Controllers.Managers var updatedYear = UpdateYearInDatabase(oldSongRecord, updatedSong, yearStore); oldSongRecord.YearId = updatedYear.YearId; - UpdateSongInDatabase(oldSongRecord, updatedSong, songStore); + UpdateSongInDatabase(ref oldSongRecord, ref updatedSong, songStore, ref result); + + DeleteEmptyDirectories(ref oldSongRecord, ref updatedSong); } catch (Exception ex) { var msg = ex.Message; _logger.Error(msg, "An error occurred"); + + result.Message = $"An error occurred: {msg}"; + result.SongTitle = song.Title; } - return new SongResult(); + return result; } public bool DeleteSongFromFileSystem(Song songMetaData) @@ -585,6 +590,14 @@ namespace Icarus.Controllers.Managers return false; } + private void DeleteEmptyDirectories(ref Song oldSong, ref Song updatedSong) + { + DirectoryManager mgr = new DirectoryManager(_config); + + _logger.Info("Checking to see if there are any directories to delete"); + mgr.DeleteEmptyDirectories(oldSong); + } + private void Initialize() { try @@ -968,22 +981,34 @@ namespace Icarus.Controllers.Managers return yearStore.GetSongYear(newSongRecord); } - private void UpdateSongInDatabase(Song oldSongRecord, Song newSongRecord, MusicStoreContext songStore) + private void UpdateSongInDatabase(ref Song oldSongRecord, ref Song newSongRecord, MusicStoreContext songStore, + ref SongResult result) { - var updatedSongRecord = oldSongRecord; + var updatedSongRecord = new Song + { + Id = oldSongRecord.Id, + Title = oldSongRecord.Title, + Artist = oldSongRecord.Artist, + AlbumTitle = oldSongRecord.AlbumTitle, + Genre = oldSongRecord.Genre, + Year = oldSongRecord.Year, + Filename = oldSongRecord.Filename, + SongPath = oldSongRecord.SongPath, + ArtistId = oldSongRecord.ArtistId, + AlbumId = oldSongRecord.AlbumId, + GenreId = oldSongRecord.GenreId, + YearId = oldSongRecord.YearId + + }; var artistOrAlbumChanged = false; - if (!SongRecordChanged(updatedSongRecord, newSongRecord)) + if (!SongRecordChanged(oldSongRecord, newSongRecord)) { _logger.Info("No change to the song record"); return; } _logger.Info("Changes to song record found"); - Console.WriteLine($"Song id {updatedSongRecord.Id}"); - Console.WriteLine("Before updated song values"); - - MetadataRetriever.PrintMetadata(updatedSongRecord); if (!string.IsNullOrEmpty(newSongRecord.Title)) { @@ -1010,9 +1035,6 @@ namespace Icarus.Controllers.Managers _logger.Info("Applied changes to song record"); - Console.WriteLine("Updated song values\n"); - MetadataRetriever.PrintMetadata(updatedSongRecord); - if (artistOrAlbumChanged) { _logger.Info("Change to song's album or artist"); @@ -1060,6 +1082,11 @@ namespace Icarus.Controllers.Managers { songStore.SaveSong(updatedSongRecord); } + + newSongRecord = updatedSongRecord; + + result.Message = "Successfully updated song"; + result.SongTitle = updatedSongRecord.Title; } private async Task PopulateSongDetails() diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 45d2aa5..24c70e5 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -121,12 +121,11 @@ namespace Icarus.Controllers Message = "Song does not exist" }); } - // TODO: Provide functionality for the UpdateSong(...) method - // before removing the below return statement + var songRes = _songMgr.UpdateSong(song, context, albumStore, artistStore, genreStore, yearStore); - return Ok("song exists"); + return Ok(songRes); } } }