Song and the corresponding tables will be deleted if there are not song related to the tables. #44
This commit is contained in:
@@ -154,19 +154,28 @@ namespace Icarus.Controllers.Managers
|
||||
|
||||
return successful;
|
||||
}
|
||||
// TODO: Implement method
|
||||
// This method should do the following, with the help of existing methods
|
||||
// or create helper methods to compelete to intended purpose:
|
||||
//
|
||||
// 1. Delete song from the filesystem
|
||||
// 2. Delete the song record from the database
|
||||
// 3. Decrement the SongCount value or delete the album record from the Database
|
||||
// 4. Decrement the SongCount value or delete the artist record from the Database
|
||||
public bool DeleteSongFromFileSystem(Song song, MusicStoreContext songStore,
|
||||
AlbumStoreContext albumStore, ArtistStoreContext artistStore)
|
||||
{
|
||||
|
||||
return false;
|
||||
public void DeleteSong(Song song, MusicStoreContext songStore,
|
||||
AlbumStoreContext albumStore, ArtistStoreContext artistStore,
|
||||
GenreStoreContext genreStore, YearStoreContext yearStore)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (DeleteSongFromFilesystem(song))
|
||||
{
|
||||
_logger.Info("Failed to delete the song");
|
||||
|
||||
throw new Exception("Failed to delete the song");
|
||||
}
|
||||
|
||||
DeleteSongFromDatabase(song, songStore, albumStore, artistStore,
|
||||
genreStore, yearStore);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
_logger.Error(msg, "An error occurred");
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveSongDetails()
|
||||
@@ -756,6 +765,43 @@ namespace Icarus.Controllers.Managers
|
||||
song.YearId = year.YearId;
|
||||
}
|
||||
|
||||
private bool DeleteSongFromFilesystem(Song song)
|
||||
{
|
||||
var songPath = song.SongPath;
|
||||
|
||||
_logger.Info("Deleting song from the filesystem");
|
||||
|
||||
try
|
||||
{
|
||||
System.IO.File.Delete(songPath);
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
var msg = ex.Message;
|
||||
_logger.Error(msg, "An error occurred when attempting to delete the song from the filesystem");
|
||||
return false;
|
||||
}
|
||||
|
||||
var result = DoesSongExistOnFilesystem(song);
|
||||
|
||||
return result;
|
||||
}
|
||||
private bool DoesSongExistOnFilesystem(Song song)
|
||||
{
|
||||
var songPath = song.SongPath;
|
||||
|
||||
if (!System.IO.File.Exists(songPath))
|
||||
{
|
||||
_logger.Info("Song does not exist on the filesystem");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
_logger.Info("Song exists on the filesystem");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private Album UpdateAlbumInDatabase(Song oldSongRecord, Song newSongRecord, AlbumStoreContext albumStore)
|
||||
{
|
||||
var albumRecord = albumStore.GetAlbum(oldSongRecord);
|
||||
@@ -1090,6 +1136,79 @@ namespace Icarus.Controllers.Managers
|
||||
result.SongTitle = updatedSongRecord.Title;
|
||||
}
|
||||
|
||||
private void DeleteSongFromDatabase(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore,
|
||||
ArtistStoreContext artistStore, GenreStoreContext genreStore, YearStoreContext yearStore)
|
||||
{
|
||||
_logger.Info("Starting process to delete records related to the song from the database");
|
||||
|
||||
DeleteAlbumFromDatabase(song, albumStore);
|
||||
DeleteArtistFromDatabase(song, artistStore);
|
||||
DeleteGenreFromDatabase(song, genreStore);
|
||||
DeleteYearFromDatabase(song, yearStore);
|
||||
|
||||
songStore.DeleteSong(song);
|
||||
}
|
||||
private void DeleteAlbumFromDatabase(Song song, AlbumStoreContext albumStore)
|
||||
{
|
||||
if (!albumStore.DoesAlbumExist(song))
|
||||
{
|
||||
_logger.Info("Cannot delete the album record because it does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
var album = albumStore.GetAlbum(song);
|
||||
|
||||
if (album.SongCount <= 1)
|
||||
{
|
||||
albumStore.DeleteAlbum(album);
|
||||
}
|
||||
}
|
||||
private void DeleteArtistFromDatabase(Song song, ArtistStoreContext artistStore)
|
||||
{
|
||||
if (!artistStore.DoesArtistExist(song))
|
||||
{
|
||||
_logger.Info("Cannot delete the artist record because it does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
var artist = artistStore.GetArtist(song);
|
||||
|
||||
if (artist.SongCount <= 1)
|
||||
{
|
||||
artistStore.DeleteArtist(artist);
|
||||
}
|
||||
}
|
||||
private void DeleteGenreFromDatabase(Song song, GenreStoreContext genreStore)
|
||||
{
|
||||
if (!genreStore.DoesGenreExist(song))
|
||||
{
|
||||
_logger.Info("Cannot delete the genre record because it does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
var genre = genreStore.GetGenre(song);
|
||||
|
||||
if (genre.SongCount <= 1)
|
||||
{
|
||||
genreStore.DeleteGenre(genre);
|
||||
}
|
||||
}
|
||||
private void DeleteYearFromDatabase(Song song, YearStoreContext yearStore)
|
||||
{
|
||||
if (!yearStore.DoesYearExist(song))
|
||||
{
|
||||
_logger.Info("Cannot delete the year record because it does not exist");
|
||||
return;
|
||||
}
|
||||
|
||||
var year = yearStore.GetSongYear(song);
|
||||
|
||||
if (year.SongCount <= 1)
|
||||
{
|
||||
yearStore.DeleteYear(year);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task PopulateSongDetails()
|
||||
{
|
||||
foreach (DataRow row in _results.Rows)
|
||||
|
||||
@@ -87,7 +87,7 @@ namespace Icarus.Controllers
|
||||
|
||||
|
||||
[HttpPut("{id}")]
|
||||
//[Authorize("update:songs")]
|
||||
[Authorize("update:songs")]
|
||||
public IActionResult Put(int id, [FromBody] Song song)
|
||||
{
|
||||
MusicStoreContext context = HttpContext
|
||||
|
||||
@@ -60,7 +60,7 @@ namespace Icarus.Controllers
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
//[Authorize("upload:songs")]
|
||||
[Authorize("upload:songs")]
|
||||
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
|
||||
{
|
||||
try
|
||||
@@ -108,19 +108,39 @@ namespace Icarus.Controllers
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[Authorize("delete:songs")]
|
||||
public void Delete(int id)
|
||||
public IActionResult Delete(int id)
|
||||
{
|
||||
MusicStoreContext context = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(MusicStoreContext)) as MusicStoreContext;
|
||||
AlbumStoreContext albumStore = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(AlbumStoreContext)) as AlbumStoreContext;
|
||||
ArtistStoreContext artistStore = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(ArtistStoreContext)) as ArtistStoreContext;
|
||||
GenreStoreContext genreStore = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(GenreStoreContext)) as GenreStoreContext;
|
||||
YearStoreContext yearStore = HttpContext
|
||||
.RequestServices
|
||||
.GetService(typeof(YearStoreContext)) as YearStoreContext;
|
||||
|
||||
var songMetaData = context.GetSong(id);
|
||||
|
||||
var result = _songMgr.DeleteSongFromFileSystem(songMetaData);
|
||||
|
||||
if (result)
|
||||
|
||||
if (string.IsNullOrEmpty(songMetaData.Title))
|
||||
{
|
||||
context.DeleteSong(songMetaData.Id);
|
||||
_logger.LogInformation("Song does not exist");
|
||||
return NotFound("Song does not exist");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("Starting process of deleting song from the filesystem and database");
|
||||
|
||||
_songMgr.DeleteSong(songMetaData, context, albumStore,
|
||||
artistStore, genreStore, yearStore);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user