Provided solution for #18

This commit is contained in:
amazing-username
2019-04-14 20:10:02 +00:00
parent 9f0d23f420
commit 3d5e776422
5 changed files with 158 additions and 82 deletions
+38 -2
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
@@ -41,7 +42,7 @@ namespace Icarus.Controllers.Managers
{ {
try try
{ {
_songDirectory = CreateDirectoryFromSong(); _songDirectory = AlbumDirectory();
if (!Directory.Exists(_songDirectory)) if (!Directory.Exists(_songDirectory))
{ {
@@ -58,19 +59,54 @@ namespace Icarus.Controllers.Managers
Console.WriteLine($"An error occurred {ex.Message}"); 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() void Initialize()
{ {
_rootSongDirectory = _config.GetValue<string>("RootMusicPath"); _rootSongDirectory = _config.GetValue<string>("RootMusicPath");
} }
string CreateDirectoryFromSong() bool IsDirectoryEmpty(string path)
{
return !Directory.EnumerateFileSystemEntries(path).Any();
}
string AlbumDirectory()
{ {
string directory = _rootSongDirectory; string directory = _rootSongDirectory;
directory += $"{_song.Artist}//{_song.Album}//"; directory += $"{_song.Artist}//{_song.Album}//";
return directory; return directory;
} }
string ArtistDirectory()
{
var directory = _rootSongDirectory;
directory += $"{_song.Artist}//";
return directory;
}
#endregion #endregion
} }
} }
+20
View File
@@ -86,6 +86,26 @@ namespace Icarus.Controllers.Managers
#region Methods #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() public void SaveSongDetails()
{ {
try try
+3 -1
View File
@@ -45,7 +45,9 @@ namespace Icarus.Controllers
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<IActionResult> Get(int id) public async Task<IActionResult> 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); SongCompression cmp = new SongCompression(_archiveDir);
+15 -5
View File
@@ -43,7 +43,9 @@ namespace Icarus.Controllers
//songs = _songMgr.RetrieveAllSongDetails().Result; //songs = _songMgr.RetrieveAllSongDetails().Result;
Console.WriteLine("Attemtping to retrieve songs"); 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(); songs = context.GetAllSongs();
@@ -54,7 +56,9 @@ namespace Icarus.Controllers
[HttpGet("{id}")] [HttpGet("{id}")]
public ActionResult<Song> Get(int id) public ActionResult<Song> 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); Song song = context.GetSong(id);
return song; return song;
@@ -63,8 +67,11 @@ namespace Icarus.Controllers
[HttpPost] [HttpPost]
public void Post([FromBody] Song song) public void Post([FromBody] Song song)
{ {
// TODO: Replace this call with the one in the MusicContext class MusicStoreContext context = HttpContext.RequestServices
_songMgr.SaveSongDetails(song); .GetService(typeof(MusicStoreContext))
as MusicStoreContext;
context.SaveSong(song);
} }
[HttpPut("{id}")] [HttpPut("{id}")]
@@ -75,7 +82,10 @@ namespace Icarus.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int 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); context.DeleteSong(id);
} }
} }
+19 -11
View File
@@ -39,19 +39,12 @@ namespace Icarus.Controllers
#endregion #endregion
[HttpGet]
public ActionResult<IEnumerable<SongData>> Get()
{
List<SongData> songs = new List<SongData>();
return songs;
}
[HttpGet("{id}")] [HttpGet("{id}")]
public async Task<IActionResult> Get(int id) public async Task<IActionResult> 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); var songMetaData = context.GetSong(id);
SongData song = await _songMgr.RetrieveSong(songMetaData); SongData song = await _songMgr.RetrieveSong(songMetaData);
@@ -64,7 +57,10 @@ namespace Icarus.Controllers
{ {
try try
{ {
MusicStoreContext context = HttpContext.RequestServices.GetService(typeof(MusicStoreContext)) as MusicStoreContext; MusicStoreContext context = HttpContext.RequestServices
.GetService(typeof(MusicStoreContext))
as MusicStoreContext;
Console.WriteLine("Uploading song..."); Console.WriteLine("Uploading song...");
var uploads = _songTempDir; var uploads = _songTempDir;
@@ -93,6 +89,18 @@ namespace Icarus.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
public void Delete(int 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);
}
} }
} }
} }