diff --git a/Controllers/Managers/DirectoryManager.cs b/Controllers/Managers/DirectoryManager.cs index b46d875..7081ddd 100644 --- a/Controllers/Managers/DirectoryManager.cs +++ b/Controllers/Managers/DirectoryManager.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using Microsoft.Extensions.Configuration; @@ -7,70 +8,105 @@ using Icarus.Models; namespace Icarus.Controllers.Managers { - public class DirectoryManager + public class DirectoryManager + { + #region Fields + private IConfiguration _config; + private Song _song; + private string _rootSongDirectory; + private string _songDirectory; + #endregion + + + #region Properties + public string SongDirectory { - #region Fields - private IConfiguration _config; - private Song _song; - private string _rootSongDirectory; - private string _songDirectory; - #endregion - - - #region Properties - public string SongDirectory - { - get => _songDirectory; - set => _songDirectory = value; - } - #endregion - - - #region Constructors - public DirectoryManager(IConfiguration config, Song song) - { - _config = config; - _song = song; - Initialize(); - } - #endregion - - - #region Methods - public void CreateDirectory() - { - try - { - _songDirectory = CreateDirectoryFromSong(); - - if (!Directory.Exists(_songDirectory)) - { - Directory.CreateDirectory(_songDirectory); - Console.WriteLine("The directory has been created"); - } - - - Console.WriteLine($"The song will be saved in the following" + - $" directory: {_songDirectory}"); - } - catch (Exception ex) - { - Console.WriteLine($"An error occurred {ex.Message}"); - } - } - - void Initialize() - { - _rootSongDirectory = _config.GetValue("RootMusicPath"); - } - - string CreateDirectoryFromSong() - { - string directory = _rootSongDirectory; - directory += $"{_song.Artist}//{_song.Album}//"; - - return directory; - } - #endregion + get => _songDirectory; + set => _songDirectory = value; } + #endregion + + + #region Constructors + public DirectoryManager(IConfiguration config, Song song) + { + _config = config; + _song = song; + Initialize(); + } + #endregion + + + #region Methods + public void CreateDirectory() + { + try + { + _songDirectory = AlbumDirectory(); + + if (!Directory.Exists(_songDirectory)) + { + Directory.CreateDirectory(_songDirectory); + Console.WriteLine("The directory has been created"); + } + + + Console.WriteLine($"The song will be saved in the following" + + $" directory: {_songDirectory}"); + } + catch (Exception ex) + { + Console.WriteLine($"An error occurred {ex.Message}"); + } + } + public void DeleteEmptyDirectories() + { + try + { + var albumDirectory = AlbumDirectory(); + var artistDirectory = ArtistDirectory(); + if (IsDirectoryEmpty(albumDirectory)) + { + Directory.Delete(albumDirectory); + Console.WriteLine($"directory {albumDirectory} deleted"); + } + if (IsDirectoryEmpty(artistDirectory)) + { + Directory.Delete(artistDirectory); + Console.WriteLine($"directory {artistDirectory} deleted"); + } + } + catch (Exception ex) + { + var exMsg = ex.Message; + Console.WriteLine($"An error occurred {exMsg}"); + } + } + + void Initialize() + { + _rootSongDirectory = _config.GetValue("RootMusicPath"); + } + + bool IsDirectoryEmpty(string path) + { + return !Directory.EnumerateFileSystemEntries(path).Any(); + } + + string AlbumDirectory() + { + string directory = _rootSongDirectory; + directory += $"{_song.Artist}//{_song.Album}//"; + + return directory; + } + string ArtistDirectory() + { + var directory = _rootSongDirectory; + directory += $"{_song.Artist}//"; + + return directory; + } + #endregion + } } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index bfc6b66..ef6eba8 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -67,7 +67,7 @@ namespace Icarus.Controllers.Managers { Initialize(); InitializeConnection(); - _song = song; + _song = song; } public SongManager(IConfiguration config) { @@ -86,6 +86,26 @@ namespace Icarus.Controllers.Managers #region Methods + public bool DeleteSongFromFileSystem(Song songMetaData) + { + bool successful = false; + try + { + var songPath = songMetaData.SongPath; + System.IO.File.Delete(songPath); + successful = true; + DirectoryManager dirMgr = new DirectoryManager(_config, songMetaData); + dirMgr.DeleteEmptyDirectories(); + Console.WriteLine("Song successfully deleted"); + } + catch (Exception ex) + { + var exMsg = ex.Message; + } + + return successful; + } + public void SaveSongDetails() { try diff --git a/Controllers/SongCompressedDataControllers.cs b/Controllers/SongCompressedDataControllers.cs index f50a554..45748c0 100644 --- a/Controllers/SongCompressedDataControllers.cs +++ b/Controllers/SongCompressedDataControllers.cs @@ -45,7 +45,9 @@ namespace Icarus.Controllers [HttpGet("{id}")] public async Task Get(int id) { - MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; SongCompression cmp = new SongCompression(_archiveDir); diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 9e4fe75..7430fc4 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -43,7 +43,9 @@ namespace Icarus.Controllers //songs = _songMgr.RetrieveAllSongDetails().Result; Console.WriteLine("Attemtping to retrieve songs"); - MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; songs = context.GetAllSongs(); @@ -54,7 +56,9 @@ namespace Icarus.Controllers [HttpGet("{id}")] public ActionResult Get(int id) { - MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; Song song = context.GetSong(id); return song; @@ -63,8 +67,11 @@ namespace Icarus.Controllers [HttpPost] public void Post([FromBody] Song song) { - // TODO: Replace this call with the one in the MusicContext class - _songMgr.SaveSongDetails(song); + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; + + context.SaveSong(song); } [HttpPut("{id}")] @@ -75,7 +82,10 @@ namespace Icarus.Controllers [HttpDelete("{id}")] public void Delete(int id) { - MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; + context.DeleteSong(id); } } diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index 4b2a604..5f08cfe 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -39,19 +39,12 @@ namespace Icarus.Controllers #endregion - [HttpGet] - public ActionResult> Get() - { - List songs = new List(); - - - return songs; - } - [HttpGet("{id}")] public async Task Get(int id) { - MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; var songMetaData = context.GetSong(id); SongData song = await _songMgr.RetrieveSong(songMetaData); @@ -64,7 +57,10 @@ namespace Icarus.Controllers { try { - MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; + Console.WriteLine("Uploading song..."); var uploads = _songTempDir; @@ -93,6 +89,18 @@ namespace Icarus.Controllers [HttpDelete("{id}")] public void Delete(int id) { + MusicStoreContext context = HttpContext.RequestServices + .GetService(typeof(MusicStoreContext)) + as MusicStoreContext; + + var songMetaData = context.GetSong(id); + + var result = _songMgr.DeleteSongFromFileSystem(songMetaData); + + if (result) + { + context.DeleteSong(songMetaData.Id); + } } } }