Continuing work on updating a song's record whenever attributes or metadata are updated. Running into an issue where the Song record is deleted during an update after the other records have been updated (artist record, album record, genre record, etc.). I believe I have narrowed it down to the Genre id and Year Id of the song not being updated. #37

This commit is contained in:
amazing-username
2019-05-19 21:48:03 -04:00
parent 991d2e0d6a
commit c9e8e2c6da
7 changed files with 289 additions and 33 deletions
+259 -9
View File
@@ -112,10 +112,10 @@ namespace Icarus.Controllers.Managers
// TODO: Add the following methods
UpdateAlbumInDatabase(oldSongRecord, updatedSong, albumStore);
// UpdateArtistInDatabase(oldSongRecord, newSongRecord, artistStore
// UpdateeGenreInDatabase(oldSongRecord, newSongRecord, genreStore)
// UpdateYearInDatabase(oldSongRecord, newSongRecord, yearStore)
// UpdateSongInDatabase(oldSongRecord, newSongRecord, songStore)
UpdateArtistInDatabase(oldSongRecord, updatedSong, artistStore);
UpdateGenreInDatabase(oldSongRecord, updatedSong, genreStore);
UpdateYearInDatabase(oldSongRecord, updatedSong, yearStore);
UpdateSongInDatabase(oldSongRecord, updatedSong, songStore);
}
catch (Exception ex)
{
@@ -563,6 +563,24 @@ namespace Icarus.Controllers.Managers
}
}
private bool SongRecordChanged(Song currentSong, Song songUpdates)
{
var currentTitle = currentSong.Title;
var currentArtist = currentSong.Artist;
var currentAlbum = currentSong.AlbumTitle;
var currentGenre = currentSong.Genre;
var currentYear = currentSong.Year;
if (!currentTitle.Equals(songUpdates.Title) || !currentArtist.Equals(songUpdates.Artist) ||
!currentAlbum.Equals(songUpdates.AlbumTitle) ||
!currentGenre.Equals(songUpdates.Genre) || currentYear != songUpdates.Year)
{
return true;
}
return false;
}
private void Initialize()
{
try
@@ -724,22 +742,254 @@ namespace Icarus.Controllers.Managers
{
var albumRecord = albumStore.GetAlbum(oldSongRecord);
var oldAlbumTitle = oldSongRecord.AlbumTitle;
var oldAlbumArtist = oldSongRecord.Artist;
var newAlbumTitle = newSongRecord.AlbumTitle;
var newAlbumArtist = newSongRecord.Artist;
var info = string.Empty;
Console.WriteLine($"{oldAlbumTitle}\n{newAlbumTitle}");
if (!oldAlbumTitle.Equals(newAlbumTitle))
if ((string.IsNullOrEmpty(newAlbumTitle) || string.IsNullOrEmpty(newAlbumArtist) ||
oldAlbumTitle.Equals(newAlbumTitle) || oldAlbumArtist.Equals(newAlbumArtist)))
{
_logger.Info("No change to the song's album");
return;
}
info = "Change to the song's album";
_logger.Info(info);
if (albumRecord.SongCount > 1)
{
_logger.Info("Decrementing existing album's song count");
albumRecord.SongCount -= 1;
albumStore.UpdateAlbum(albumRecord);
}
else
{
info = "No change to the song's album";
_logger.Info(info);
_logger.Info("Deleting existing album record that no longer has any songs");
albumStore.DeleteAlbum(albumRecord);
}
if (!albumStore.DoesAlbumExist(newSongRecord))
{
_logger.Info("Creating new album record");
var newAlbumRecord = new Album
{
Title = newAlbumTitle,
AlbumArtist = newAlbumArtist,
SongCount = 1
};
albumStore.SaveAlbum(newAlbumRecord);
}
else
{
_logger.Info("Updating existing album record");
var existingAlbumRecord = albumStore.GetAlbum(newSongRecord);
existingAlbumRecord.AlbumArtist = newAlbumArtist;
existingAlbumRecord.SongCount += 1;
albumStore.UpdateAlbum(existingAlbumRecord);
}
}
private void UpdateArtistInDatabase(Song oldSongRecord, Song newSongRecord, ArtistStoreContext artistStore)
{
var oldArtistRecord = artistStore.GetArtist(oldSongRecord);
var oldArtistName = oldArtistRecord.Name;
var newArtistName = newSongRecord.Artist;
if (string.IsNullOrEmpty(newArtistName) || oldArtistName.Equals(newArtistName))
{
_logger.Info("No change to the song's Artist");
return;
}
_logger.Info("Change to the song's record found");
if (oldArtistRecord.SongCount > 1)
{
_logger.Info("Decrementing existing artist's song count");
oldArtistRecord.SongCount -= 1;
artistStore.UpdateArtist(oldArtistRecord);
}
else
{
_logger.Info("Deleting artist record that no longer has any songs");
artistStore.DeleteArtist(oldArtistRecord);
}
if (!artistStore.DoesArtistExist(newSongRecord))
{
_logger.Info("Creating new artist record");
var newArtistRecord = new Artist
{
Name = newArtistName,
SongCount = 1
};
artistStore.SaveArtist(newArtistRecord);
}
else
{
_logger.Info("Updating existing artist record");
var existingArtistRecord = artistStore.GetArtist(newSongRecord);
existingArtistRecord.SongCount += 1;
artistStore.UpdateArtist(existingArtistRecord);
}
}
private void UpdateGenreInDatabase(Song oldSongRecord, Song newSongRecord, GenreStoreContext genreStore)
{
var oldGenreRecord = genreStore.GetGenre(oldSongRecord);
var oldGenreName = oldGenreRecord.GenreName;
var newGenreName = newSongRecord.Genre;
if (string.IsNullOrEmpty(newGenreName) || oldGenreName.Equals(newGenreName))
{
_logger.Info("No change to the song's Genre");
return;
}
_logger.Info("Change to the song's genre found");
if (oldGenreRecord.SongCount > 1)
{
_logger.Info("Decrementing exisiting genre song count");
oldGenreRecord.SongCount -= 1;
genreStore.UpdateGenre(oldGenreRecord);
}
else
{
_logger.Info("Deleting genre record");
genreStore.DeleteGenre(oldGenreRecord);
}
if (!genreStore.DoesGenreExist(newSongRecord))
{
_logger.Info("Creating new genre record");
var newGenreRecord = new Genre
{
GenreName = newGenreName,
SongCount = 1
};
genreStore.SaveGenre(newGenreRecord);
}
else
{
_logger.Info("Updating existing genre record");
var existingGenreRecord = genreStore.GetGenre(newSongRecord);
existingGenreRecord.SongCount += 1;
genreStore.UpdateGenre(existingGenreRecord);
}
}
private void UpdateYearInDatabase(Song oldSongRecord, Song newSongRecord, YearStoreContext yearStore)
{
var oldYearRecord = yearStore.GetSongYear(oldSongRecord);
var oldYearValue = oldYearRecord.YearValue;
var newYearValue = newSongRecord.Year;
if (oldYearValue == newYearValue || newYearValue == 0 || newYearValue == null)
{
_logger.Info("No change to the song's Year");
return;
}
_logger.Info("Change to the song's year found");
if (oldYearRecord.SongCount > 1)
{
_logger.Info("Decrementing song count of year record");
oldYearRecord.SongCount -= 1;
yearStore.UpdateYear(oldYearRecord);
}
else
{
_logger.Info("Deleting year record");
yearStore.DeleteYear(oldYearRecord);
}
if(!yearStore.DoesYearExist(newSongRecord))
{
_logger.Info("Creating new year record");
var newYearRecord = new Year
{
YearValue = newYearValue.Value,
SongCount = 1
};
yearStore.SaveYear(newYearRecord);
}
else
{
_logger.Info("Updating existing year record");
var existingYearRecord = yearStore.GetSongYear(newSongRecord);
existingYearRecord.SongCount += 1;
yearStore.UpdateYear(existingYearRecord);
}
}
private void UpdateSongInDatabase(Song oldSongRecord, Song newSongRecord, MusicStoreContext songStore)
{
var updatedSongRecord = oldSongRecord;
if (!SongRecordChanged(updatedSongRecord, newSongRecord))
{
_logger.Info("No change to the song record");
return;
}
_logger.Info("Changes to song record found");
Console.WriteLine($"Song id {updatedSongRecord.Id}");
Console.WriteLine("Before updated song values");
MetadataRetriever.PrintMetadata(updatedSongRecord);
if (!string.IsNullOrEmpty(newSongRecord.Title))
{
updatedSongRecord.Title = newSongRecord.Title;
}
if (!string.IsNullOrEmpty(newSongRecord.AlbumTitle))
{
updatedSongRecord.AlbumTitle = newSongRecord.AlbumTitle;
}
if (!string.IsNullOrEmpty(newSongRecord.Artist))
{
updatedSongRecord.Artist = newSongRecord.Artist;
}
if (!string.IsNullOrEmpty(newSongRecord.Genre))
{
updatedSongRecord.Genre = newSongRecord.Genre;
}
if (newSongRecord.Year != null || newSongRecord.Year > 0)
{
updatedSongRecord.Year = newSongRecord.Year;
}
_logger.Info("Applied changes to song record");
Console.WriteLine("Updated song values\n");
MetadataRetriever.PrintMetadata(updatedSongRecord);
songStore.UpdateSong(updatedSongRecord);
}
private async Task PopulateSongDetails()
-17
View File
@@ -127,23 +127,6 @@ namespace Icarus.Controllers
yearStore);
return Ok("song exists");
var oldSongRecord = context.GetSong(id);
song.SongPath = oldSongRecord.SongPath;
MetadataRetriever updateMetadata = new MetadataRetriever();
updateMetadata.UpdateMetadata(song, oldSongRecord);
var updatedSong = updateMetadata.UpdatedSongRecord;
context.UpdateSong(updatedSong);
SongResult songResult = new SongResult
{
Message = updateMetadata.Message,
SongTitle = updatedSong.Title
};
return Ok(songResult);
}
}
}
@@ -43,6 +43,29 @@ namespace Icarus.Controllers.Utilities
#region Methods
public static void PrintMetadata(Song song)
{
Console.WriteLine("\n\nMetadata of the song:");
Console.WriteLine($"Title: {song.Title}");
Console.WriteLine($"Artist: {song.Artist}");
Console.WriteLine($"Album: {song.AlbumTitle}");
Console.WriteLine($"Genre: {song.Genre}");
Console.WriteLine($"Year: {song.Year}");
Console.WriteLine($"Duration: {song.Duration}");
Console.WriteLine($"AlbumId: {song.AlbumId}");
Console.WriteLine($"ArtistId: {song.ArtistId}");
Console.WriteLine($"GenreId: {song.GenreId}");
Console.WriteLine($"YearId: {song.YearId}");
Console.WriteLine("\n");
_logger.Info("Metadata of the song");
_logger.Info($"Title: {song.Title}");
_logger.Info($"Artist: {song.Artist}");
_logger.Info($"Album: {song.AlbumTitle}");
_logger.Info($"Genre: {song.Genre}");
_logger.Info($"Year: {song.Year}");
_logger.Info($"Duration: {song.Duration}");
}
public Song RetrieveMetaData(string filePath)
{
Song song = new Song();
+2 -2
View File
@@ -119,7 +119,7 @@ namespace Icarus.Models.Context
{
conn.Open();
var query = "SELECT * FROM Album WHERE Title=@Title";
_logger.Info($"Song title to rerieve album:\n{song.AlbumTitle}");
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Title", song.AlbumTitle);
@@ -280,7 +280,7 @@ namespace Icarus.Models.Context
using (var conn = GetConnection())
{
conn.Open();
var query = "DELETE Album WHERE AlbumId=@AlbumId";
var query = "DELETE FROM Album WHERE AlbumId=@AlbumId";
using (var cmd = new MySqlCommand(query, conn))
{
+1 -1
View File
@@ -202,7 +202,7 @@ namespace Icarus.Models.Context
{
conn.Open();
var query = "DELETE Artist WHERE ArtistId=@ArtistId";
var query = "DELETE FROM Artist WHERE ArtistId=@ArtistId";
using (var cmd = new MySqlCommand(query, conn))
{
+1 -1
View File
@@ -275,7 +275,7 @@ namespace Icarus.Models.Context
{
conn.Open();
var query = "DELETE Genre WHERE GenreId=@GenreId";
var query = "DELETE FROM Genre WHERE GenreId=@GenreId";
using (var cmd = new MySqlCommand(query, conn))
{
+1 -1
View File
@@ -273,7 +273,7 @@ namespace Icarus.Models.Context
{
conn.Open();
var query = "DELETE Year WHERE YearId=@YearId";
var query = "DELETE FROM Year WHERE YearId=@YearId";
using (var cmd = new MySqlCommand(query, conn))
{