From ea25480ad194015e9db1fc81ee9b2633d62e5b3f Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Sat, 10 Aug 2019 12:40:26 -0400 Subject: [PATCH] #56 --- Controllers/Managers/SongManager.cs | 36 +++--- Controllers/Utilities/MetadataRetriever.cs | 128 --------------------- Database/Repositories/AlbumRepository.cs | 1 + 3 files changed, 22 insertions(+), 143 deletions(-) diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 7b4a4fc..b102f44 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -80,11 +80,12 @@ namespace Icarus.Controllers.Managers { var oldSongRecord = songStore.GetSong(song); song.SongPath = oldSongRecord.SongPath; + var oldSng = ConvertSongToSng(oldSongRecord); + var updatedSng = ConvertSongToSng(song); - MetadataRetriever updateMetadata = new MetadataRetriever(); - updateMetadata.UpdateMetadata(song, oldSongRecord); + MetadataRetriever.update_metadata(ref updatedSng, ref oldSng); - var updatedSong = updateMetadata.UpdatedSongRecord; + var updatedSong = ConvertSngToSong(updatedSng); var updatedAlbum = UpdateAlbumInDatabase(oldSongRecord, updatedSong, albumStore); oldSongRecord.AlbumId = updatedAlbum.AlbumId; @@ -221,12 +222,16 @@ namespace Icarus.Controllers.Managers { return new Sng { - Id = song.Id, - Title = song.Title, - Artist = song.Artist, - Album = song.AlbumTitle, - Genre = song.Genre, - Year = song.Year.Value, + Id = (song.Id == 0) ? 0 : song.Id, + Title = (string.IsNullOrEmpty(song.Title)) ? string.Empty : + song.Title, + Artist = (string.IsNullOrEmpty(song.Artist)) ? string.Empty : + song.Artist, + Album = (string.IsNullOrEmpty(song.AlbumTitle)) ? string.Empty : + song.AlbumTitle, + Genre = (string.IsNullOrEmpty(song.Genre)) ? string.Empty : + song.Genre, + Year = (song.Year == null) ? 0 : song.Year.Value, Duration = song.Duration, SongPath = song.SongPath }; @@ -240,7 +245,7 @@ namespace Icarus.Controllers.Managers Artist = song.Artist, AlbumTitle = song.Album, Genre = song.Genre, - Year = song.Year, + Year = (song.Year == 0) ? 0 : song.Year, Duration = song.Duration, SongPath = song.SongPath }; @@ -373,7 +378,6 @@ namespace Icarus.Controllers.Managers if (!genreStore.DoesGenreExist(song)) { genreStore.SaveGenre(genre); - Console.WriteLine("Going to find genre"); genre = genreStore.GetGenre(song); } else @@ -465,13 +469,13 @@ namespace Icarus.Controllers.Managers { _logger.Info("Creating new album record"); - var newAlbumRecord = new Album + albumStore.SaveAlbum(new Album { Title = newAlbumTitle, AlbumArtist = newAlbumArtist - }; + }); - albumStore.SaveAlbum(newAlbumRecord); + newSongRecord.AlbumTitle = newAlbumTitle; return albumStore.GetAlbum(newSongRecord, true); } @@ -651,8 +655,10 @@ namespace Icarus.Controllers.Managers { updatedSongRecord.Genre = newSongRecord.Genre; } - if (newSongRecord.Year != null || newSongRecord.Year > 0) + if (newSongRecord.Year > 0) updatedSongRecord.Year = newSongRecord.Year; + else + updatedSongRecord.Year = oldSongRecord.Year; _logger.Info("Applied changes to song record"); diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index f4fb81f..b25b1a0 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -15,17 +15,11 @@ namespace Icarus.Controllers.Utilities { #region Fields private static NLog.Logger _logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); - private Song _updatedSong; private string _message; #endregion #region Properties - public Song UpdatedSongRecord - { - get => _updatedSong; - set => _updatedSong = value; - } public string Message { get => _message; @@ -88,23 +82,6 @@ namespace Icarus.Controllers.Utilities return null; } - public void UpdateMetadata(Song updatedSong, Song oldSong) - { - try - { - InitializeUpdatedSong(oldSong); - var songValues = CheckSongValues(updatedSong); - PerformUpdate(updatedSong, songValues); - Message = "Successfully updated metadata"; - } - catch (Exception ex) - { - var msg = ex.Message; - Console.WriteLine($"An error occurred: {msg}"); - _logger.Error(msg, "An error occurred"); - Message = "Failed to update metadata"; - } - } public void UpdateCoverArt(Song song, CoverArt coverArt) { Console.WriteLine("Updating song's cover art"); @@ -121,111 +98,6 @@ namespace Icarus.Controllers.Utilities tag.Tag.Pictures = pics; tag.Save(); } - - private void PerformUpdate(Song updatedSong, SortedDictionary checkedValues) - { - var filePath = updatedSong.SongPath; - var title = updatedSong.Title; - var artist = updatedSong.Artist; - var album = updatedSong.AlbumTitle; - var genre = updatedSong.Genre; - var year = updatedSong.Year; - TagLib.File fileTag = TagLib.File.Create(filePath); - try - { - Console.WriteLine($"Updating metadata of {title}"); - _logger.Info($"Updating metadata of {title}"); - - foreach (var key in checkedValues.Keys) - { - bool result = checkedValues[key]; - - - if (!result) - switch (key.ToLower()) - { - case "title": - _updatedSong.Title = title; - fileTag.Tag.Title = title; - break; - case "artists": - _updatedSong.Artist = artist; - fileTag.Tag.Performers = new []{artist}; - break; - case "album": - _updatedSong.AlbumTitle = album; - fileTag.Tag.Album = album; - break; - case "genre": - _updatedSong.Genre = genre; - fileTag.Tag.Genres = new []{genre}; - break; - case "year": - _updatedSong.Year = year; - fileTag.Tag.Year = (uint)year; - break; - } - } - - fileTag.Save(); - - Console.WriteLine("Successfully updated metadata"); - _logger.Info("Successfully updated metadata"); - } - catch (Exception ex) - { - var msg = ex.Message; - Console.WriteLine($"An error occurred:\n{msg}"); - _logger.Error(msg, "An error occurred"); - } - } - private void InitializeUpdatedSong(Song song) - { - _updatedSong = new Song - { - Id = song.Id, - Title = song.Title, - AlbumTitle = song.AlbumTitle, - Artist = song.Artist, - Genre = song.Genre, - Year = song.Year, - Duration = song.Duration, - Filename = song.Filename, - SongPath = song.SongPath - }; - } - - private SortedDictionary CheckSongValues(Song song) - { - var songValues = new SortedDictionary(); - Console.WriteLine("Checking for null data"); - _logger.Info("Checking for null data"); - try - { - songValues["Title"] = String.IsNullOrEmpty(song.Title); - songValues["Artists"] = String.IsNullOrEmpty(song.Artist); - songValues["Album"] = String.IsNullOrEmpty(song.AlbumTitle); - songValues["Genre"] = String.IsNullOrEmpty(song.Genre); - - if (song.Year == null) - songValues["Year"] = true; - else if (song.Year==0) - songValues["Year"] = true; - else - songValues["Year"] = false; - - Console.WriteLine("Checking for null data completed"); - _logger.Info("Checking for null data completed"); - } - catch (Exception ex) - { - var msg = ex.Message; - Console.WriteLine($"An error occurred: \n{msg}"); - _logger.Error(msg, "An error occurred"); - } - - return songValues; - } #endregion } } diff --git a/Database/Repositories/AlbumRepository.cs b/Database/Repositories/AlbumRepository.cs index 5d67305..860c1bc 100644 --- a/Database/Repositories/AlbumRepository.cs +++ b/Database/Repositories/AlbumRepository.cs @@ -156,6 +156,7 @@ namespace Icarus.Database.Repositories using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", song.AlbumTitle); + Console.WriteLine($"repo alb {song.AlbumTitle}"); using (var reader = cmd.ExecuteReader()) album = ParseSingleData(reader);