Song and the corresponding tables will be deleted if there are not song related to the tables. #44

This commit is contained in:
amazing-username
2019-05-25 00:50:30 +00:00
parent f9e66f416d
commit dd89b97997
6 changed files with 211 additions and 62 deletions
+131 -12
View File
@@ -154,19 +154,28 @@ namespace Icarus.Controllers.Managers
return successful; 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() public void SaveSongDetails()
@@ -756,6 +765,43 @@ namespace Icarus.Controllers.Managers
song.YearId = year.YearId; 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) private Album UpdateAlbumInDatabase(Song oldSongRecord, Song newSongRecord, AlbumStoreContext albumStore)
{ {
var albumRecord = albumStore.GetAlbum(oldSongRecord); var albumRecord = albumStore.GetAlbum(oldSongRecord);
@@ -1090,6 +1136,79 @@ namespace Icarus.Controllers.Managers
result.SongTitle = updatedSongRecord.Title; 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() private async Task PopulateSongDetails()
{ {
foreach (DataRow row in _results.Rows) foreach (DataRow row in _results.Rows)
+1 -1
View File
@@ -87,7 +87,7 @@ namespace Icarus.Controllers
[HttpPut("{id}")] [HttpPut("{id}")]
//[Authorize("update:songs")] [Authorize("update:songs")]
public IActionResult Put(int id, [FromBody] Song song) public IActionResult Put(int id, [FromBody] Song song)
{ {
MusicStoreContext context = HttpContext MusicStoreContext context = HttpContext
+26 -6
View File
@@ -60,7 +60,7 @@ namespace Icarus.Controllers
} }
[HttpPost] [HttpPost]
//[Authorize("upload:songs")] [Authorize("upload:songs")]
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData) public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
{ {
try try
@@ -108,19 +108,39 @@ namespace Icarus.Controllers
[HttpDelete("{id}")] [HttpDelete("{id}")]
[Authorize("delete:songs")] [Authorize("delete:songs")]
public void Delete(int id) public IActionResult Delete(int id)
{ {
MusicStoreContext context = HttpContext MusicStoreContext context = HttpContext
.RequestServices .RequestServices
.GetService(typeof(MusicStoreContext)) as MusicStoreContext; .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 songMetaData = context.GetSong(id);
var result = _songMgr.DeleteSongFromFileSystem(songMetaData); if (string.IsNullOrEmpty(songMetaData.Title))
if (result)
{ {
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();
} }
} }
} }
+3 -42
View File
@@ -41,20 +41,7 @@ namespace Icarus.Models.Context
{ {
using (var reader = cmd.ExecuteReader()) using (var reader = cmd.ExecuteReader())
{ {
while (reader.Read()) albums = ParseData(reader);
{
var id = Convert.ToInt32(reader["AlbumId"]);
var title = reader["Title"].ToString();
var albumArtist = reader["AlbumArtist"].ToString();
var songCount = Convert.ToInt32(reader["SongCount"]);
albums.Add(new Album
{
AlbumId = id,
Title = title,
AlbumArtist = albumArtist,
SongCount = songCount
});
}
} }
} }
} }
@@ -83,20 +70,7 @@ namespace Icarus.Models.Context
using (var reader = cmd.ExecuteReader()) using (var reader = cmd.ExecuteReader())
{ {
while (reader.Read()) album = ParseSingleData(reader);
{
var id = Convert.ToInt32(reader["AlbumId"]);
var title = reader["Title"].ToString();
var albumArtist = reader["AlbumArtist"].ToString();
var songCount = Convert.ToInt32(reader["SongCount"]);
album =new Album
{
AlbumId = id,
Title = title,
AlbumArtist = albumArtist,
SongCount = songCount
};
}
} }
} }
} }
@@ -126,20 +100,7 @@ namespace Icarus.Models.Context
using (var reader = cmd.ExecuteReader()) using (var reader = cmd.ExecuteReader())
{ {
while (reader.Read()) album = ParseSingleData(reader);
{
var id = Convert.ToInt32(reader["AlbumId"]);
var title = reader["Title"].ToString();
var albumArtist = reader["AlbumArtist"].ToString();
var songCount = Convert.ToInt32(reader["SongCount"]);
album = new Album
{
AlbumId = id,
Title = title,
AlbumArtist = albumArtist,
SongCount = songCount
};
}
} }
} }
} }
+26
View File
@@ -106,6 +106,32 @@ namespace Icarus.Models.Context
_logger.Error(msg, "An error occurred"); _logger.Error(msg, "An error occurred");
} }
} }
public void DeleteSong(Song song)
{
try
{
_logger.Info("Deleting song record");
using (var conn = GetConnection())
{
conn.Open();
var query = "DELETE FROM Song WHERE Id=@Id";
using (var cmd = new MySqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Id", song.Id);
cmd.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
var msg = ex.Message;
_logger.Error(msg, "An error occurred");
}
}
public void DeleteSong(int id) public void DeleteSong(int id)
{ {
try try
+23
View File
@@ -0,0 +1,23 @@
echo "Adding migrations..."
echo "Adding User migration"
dotnet ef migrations add User --context UserContext
echo "Adding Song migration"
dotnet ef migrations add Song --context SongContext
echo "Adding Album migration"
dotnet ef migrations add Album --context AlbumContext
echo "Adding Artist migration"
dotnet ef migrations add Artist --context ArtistContext
echo "Adding Genre migration"
dotnet ef migrations add Genre --context GenreContext
echo "Adding Year migration"
dotnet ef migrations add Year --context YearContext
echo "Updating migrations.."
echo "Updating User migration"
dotnet ef database update --context UserContext
echo "Updating Song migration"
echo "Updating Album migration"
echo "Updating Artist migration"
echo "Updating Genre migration"
echo "Updating Year migration"
dotnet ef database update --context SongContext