From e1627b04b9ab71ff59ed6d68578271b3cb94c964 Mon Sep 17 00:00:00 2001 From: amazing-username Date: Sat, 25 May 2019 03:06:04 +0000 Subject: [PATCH] The SongCount field is not mapped during migrations. Working on changes to the StoreContext classes. These changes will calculate the song count for the album, artist, genre, and year records when querying data. The other change is to check to see if the record exist in the first place before querying using a left join. #47 --- Controllers/Managers/SongManager.cs | 3 ++ Models/Album.cs | 1 + Models/Artist.cs | 2 ++ Models/Context/AlbumStoreContext.cs | 49 ++++++++++++++++++++++------ Models/Context/ArtistStoreContext.cs | 9 ++--- Models/Context/GenreStoreContext.cs | 12 +++---- Models/Context/YearStoreContext.cs | 8 ++--- Models/Genre.cs | 2 ++ Models/Year.cs | 2 ++ 9 files changed, 59 insertions(+), 29 deletions(-) diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 6cb21cf..ddbf65c 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -634,8 +634,11 @@ namespace Icarus.Controllers.Managers { _logger.Info("Starting process to save the song to the database"); + MetadataRetriever.PrintMetadata(song); SaveAlbumToDatabase(ref song, albumStore); + MetadataRetriever.PrintMetadata(song); SaveArtistToDatabase(ref song, artistStore); + MetadataRetriever.PrintMetadata(song); _logger.Info($"Song;\nTitle {song.Title}\nAlbum {song.AlbumTitle}\nAlbum Id {song.AlbumId}\nArtist {song.ArtistId}"); diff --git a/Models/Album.cs b/Models/Album.cs index 7c40e44..0d3c9f2 100644 --- a/Models/Album.cs +++ b/Models/Album.cs @@ -15,6 +15,7 @@ namespace Icarus.Models [JsonProperty("album_artist")] public string AlbumArtist { get; set; } [JsonProperty("song_count")] + [NotMapped] public int SongCount { get; set; } [JsonIgnore] diff --git a/Models/Artist.cs b/Models/Artist.cs index e4997d0..807d3e4 100644 --- a/Models/Artist.cs +++ b/Models/Artist.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using Newtonsoft.Json; @@ -12,6 +13,7 @@ namespace Icarus.Models [JsonProperty("name")] public string Name { get; set; } [JsonProperty("song_count")] + [NotMapped] public int SongCount { get; set; } [JsonIgnore] diff --git a/Models/Context/AlbumStoreContext.cs b/Models/Context/AlbumStoreContext.cs index cd48e08..4ac7f42 100644 --- a/Models/Context/AlbumStoreContext.cs +++ b/Models/Context/AlbumStoreContext.cs @@ -36,7 +36,9 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "SELECT * FROM Album"; + var query = "SELECT alb.*,COUNT(*) AS SongCount FROM Album alb " + + "LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE " + + "alb.AlbumId=sng.AlbumId GROUP BY alb.AlbumId"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { using (var reader = cmd.ExecuteReader()) @@ -63,7 +65,9 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "SELECT * FROM Album WHERE AlbumId=@AlbumId"; + var query = "SELECT alb.*, COUNT(*) AS SongCount FROM Album alb " + + "LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE "+ + "alb.AlbumId=@AlbumId LIMIT 1"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId); @@ -92,11 +96,13 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "SELECT * FROM Album WHERE Title=@Title"; + var query = "SELECT alb.*, COUNT(*) AS SongCount FROM Album alb " + + "LEFT JOIN Song sng ON alb.AlbumId=sng.AlbumId WHERE " + + "sng.Id=@Id LIMIT 1"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { - cmd.Parameters.AddWithValue("@Title", song.AlbumTitle); + cmd.Parameters.AddWithValue("@Id", song.Id); using (var reader = cmd.ExecuteReader()) { @@ -118,6 +124,18 @@ namespace Icarus.Models.Context { try { + album = GetAlbum(album); + + if (!string.IsNullOrEmpty(album.Title)) + { + _logger.Info("Album exists"); + return true; + } + else + { + _logger.Info("Album does not exist"); + } + /** using (MySqlConnection conn = GetConnection()) { conn.Open(); @@ -139,6 +157,7 @@ namespace Icarus.Models.Context } } } + */ } catch (Exception ex) { @@ -151,6 +170,18 @@ namespace Icarus.Models.Context { try { + var album = GetAlbum(song); + + if (!string.IsNullOrEmpty(album.Title)) + { + _logger.Info("Album exists"); + return true; + } + else + { + _logger.Info("Album does not exist"); + } + /** using (MySqlConnection conn = GetConnection()) { conn.Open(); @@ -172,6 +203,7 @@ namespace Icarus.Models.Context } } } + */ } catch (Exception ex) { @@ -188,14 +220,12 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "INSERT INTO Album(Title, AlbumArtist, " + - "SongCount) VALUES (@Title, @AlbumArtist, " + - "@SongCount)"; + var query = "INSERT INTO Album(Title, AlbumArtist)" + + " VALUES (@Title, @AlbumArtist)"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", album.Title); cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist); - cmd.Parameters.AddWithValue("@SongCount", album.SongCount); cmd.ExecuteNonQuery(); } @@ -215,13 +245,12 @@ namespace Icarus.Models.Context { conn.Open(); var query = "UPDATE Album SET Title=@Title, AlbumArtist=" + - "@AlbumArtist, SongCount=@SongCount WHERE AlbumId=@AlbumId"; + "@AlbumArtist WHERE AlbumId=@AlbumId"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", album.Title); cmd.Parameters.AddWithValue("@AlbumArtist", album.AlbumArtist); - cmd.Parameters.AddWithValue("@SongCount", album.SongCount); cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId); cmd.ExecuteNonQuery(); diff --git a/Models/Context/ArtistStoreContext.cs b/Models/Context/ArtistStoreContext.cs index b6d6b3f..d437885 100644 --- a/Models/Context/ArtistStoreContext.cs +++ b/Models/Context/ArtistStoreContext.cs @@ -13,7 +13,6 @@ namespace Icarus.Models.Context public class ArtistStoreContext : BaseStoreContext { #region Fields - //private static Logger _logger = NLog.LogManager.GetCurrentClassLogger(); #endregion @@ -143,13 +142,11 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "INSERT INTO Artist(Name, SongCount) " + - "VALUES(@Name, @SongCount)"; + var query = "INSERT INTO Artist(Name) VALUES(@Name)"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Name", artist.Name); - cmd.Parameters.AddWithValue("@SongCount", artist.SongCount); cmd.ExecuteNonQuery(); } @@ -171,13 +168,11 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "UPDATE Artist SET Name=@Name, SongCount" + - "=@SongCount WHERE ArtistId=@ArtistId"; + var query = "UPDATE Artist SET Name=@Name WHERE ArtistId=@ArtistId"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Name", artist.Name); - cmd.Parameters.AddWithValue("@SongCount", artist.SongCount); cmd.Parameters.AddWithValue("@ArtistId", artist.ArtistId); cmd.ExecuteNonQuery(); diff --git a/Models/Context/GenreStoreContext.cs b/Models/Context/GenreStoreContext.cs index f33e626..c606f9c 100644 --- a/Models/Context/GenreStoreContext.cs +++ b/Models/Context/GenreStoreContext.cs @@ -172,6 +172,8 @@ namespace Icarus.Models.Context try { + var genre = ParseSingleData(song); + using (var conn = GetConnection()) { conn.Open(); @@ -196,6 +198,7 @@ namespace Icarus.Models.Context } } } + */ } catch (Exception ex) { @@ -218,13 +221,11 @@ namespace Icarus.Models.Context { conn.Open(); - var query = "INSERT INTO Genre(GenreName, SongCount) VALUES(" + - "@GenreName, @SongCount)"; + var query = "INSERT INTO Genre(GenreName) VALUES(@GenreName)"; using (var cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@GenreName", genre.GenreName); - cmd.Parameters.AddWithValue("@SongCount", genre.SongCount); cmd.ExecuteNonQuery(); } @@ -246,13 +247,12 @@ namespace Icarus.Models.Context { conn.Open(); - var query = "UPDATE Genre SET GenreName=@GenreName, " + - "SongCount=@SongCount WHERE GenreId=@GenreId"; + var query = "UPDATE Genre SET GenreName=@GenreName " + + "WHERE GenreId=@GenreId"; using (var cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@GenreName", genre.GenreName); - cmd.Parameters.AddWithValue("@SongCount", genre.SongCount); cmd.Parameters.AddWithValue("@GenreId", genre.GenreId); cmd.ExecuteNonQuery(); diff --git a/Models/Context/YearStoreContext.cs b/Models/Context/YearStoreContext.cs index c99c433..5601a4f 100644 --- a/Models/Context/YearStoreContext.cs +++ b/Models/Context/YearStoreContext.cs @@ -216,13 +216,11 @@ namespace Icarus.Models.Context { conn.Open(); - var query = "INSERT INTO Year(YearValue, SongCount) VALUES(" + - "@YearValue, @SongCount)"; + var query = "INSERT INTO Year(YearValue) VALUES(@YearValue)"; using (var cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@YearValue", year.YearValue); - cmd.Parameters.AddWithValue("@SongCount", year.SongCount); cmd.ExecuteNonQuery(); } @@ -244,14 +242,12 @@ namespace Icarus.Models.Context { conn.Open(); - var query = "UPDATE Year SET YearValue=@YearValue, SongCount=@SongCount " + - "WHERE YearId=@YearId"; + var query = "UPDATE Year SET YearValue=@YearValue WHERE YearId=@YearId"; using (var cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@YearId", year.YearId); cmd.Parameters.AddWithValue("@YearValue", year.YearValue); - cmd.Parameters.AddWithValue("@SongCount", year.SongCount); cmd.ExecuteNonQuery(); } diff --git a/Models/Genre.cs b/Models/Genre.cs index f915e9c..c6913c6 100644 --- a/Models/Genre.cs +++ b/Models/Genre.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using Newtonsoft.Json; @@ -12,6 +13,7 @@ namespace Icarus.Models [JsonProperty("genre")] public string GenreName { get; set; } [JsonProperty("song_count")] + [NotMapped] public int SongCount { get; set; } [JsonIgnore] diff --git a/Models/Year.cs b/Models/Year.cs index 9223eaa..0ca1db2 100644 --- a/Models/Year.cs +++ b/Models/Year.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; using Newtonsoft.Json; @@ -12,6 +13,7 @@ namespace Icarus.Models [JsonProperty("year")] public int YearValue { get; set; } [JsonProperty("song_count")] + [NotMapped] public int SongCount { get; set; } [JsonIgnore]