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;
|
||||
|
||||
@@ -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<string>("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<string>("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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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