Provided solution for #18
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
@@ -41,7 +42,7 @@ namespace Icarus.Controllers.Managers
|
||||
{
|
||||
try
|
||||
{
|
||||
_songDirectory = CreateDirectoryFromSong();
|
||||
_songDirectory = AlbumDirectory();
|
||||
|
||||
if (!Directory.Exists(_songDirectory))
|
||||
{
|
||||
@@ -58,19 +59,54 @@ namespace Icarus.Controllers.Managers
|
||||
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<string>("RootMusicPath");
|
||||
}
|
||||
|
||||
string CreateDirectoryFromSong()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -45,7 +45,9 @@ namespace Icarus.Controllers
|
||||
[HttpGet("{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);
|
||||
|
||||
|
||||
@@ -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<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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,19 +39,12 @@ namespace Icarus.Controllers
|
||||
#endregion
|
||||
|
||||
|
||||
[HttpGet]
|
||||
public ActionResult<IEnumerable<SongData>> Get()
|
||||
{
|
||||
List<SongData> songs = new List<SongData>();
|
||||
|
||||
|
||||
return songs;
|
||||
}
|
||||
|
||||
[HttpGet("{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);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user