diff --git a/Controllers/AlbumController.cs b/Controllers/AlbumController.cs index 9413ae5..9481c9b 100644 --- a/Controllers/AlbumController.cs +++ b/Controllers/AlbumController.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using Icarus.Models; +using Icarus.Models.Context; namespace Icarus.Controllers { @@ -37,6 +38,13 @@ namespace Icarus.Controllers { List albums = new List(); + AlbumStoreContext albumStoreContext = HttpContext + .RequestServices + .GetService(typeof(AlbumStoreContext)) as AlbumStoreContext; + + albums = albumStoreContext.GetAlbums(); + + return Ok(albums); } @@ -44,6 +52,13 @@ namespace Icarus.Controllers public IActionResult Get(int id) { Album album = new Album(); + album.AlbumId = id; + + AlbumStoreContext albumStoreContext = HttpContext + .RequestServices + .GetService(typeof(AlbumStoreContext)) as AlbumStoreContext; + + album = albumStoreContext.GetAlbum(album); return Ok(album); } diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 6cf3b13..8b6deaf 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -116,13 +116,13 @@ namespace Icarus.Controllers.Managers using (MySqlConnection conn = new MySqlConnection(_connectionString)) { conn.Open(); - string query = "INSERT INTO Songs(Title, Album, Artist, Year, Genre, Duration, " + - "Filename, SongPath) VALUES(@Title, @Album, @Artist, @Year, @Genre, " + + string query = "INSERT INTO Songs(Title, AlbumTitle, Artist, Year, Genre, Duration, " + + "Filename, SongPath) VALUES(@Title, @AlbumTitle, @Artist, @Year, @Genre, " + "@Duration, @Filename, @SongPath)"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", _song.Title); - cmd.Parameters.AddWithValue("@Album", _song.Album); + cmd.Parameters.AddWithValue("@AlbumTitle", _song.AlbumTitle); cmd.Parameters.AddWithValue("@Artist", _song.Artist); cmd.Parameters.AddWithValue("@Year", _song.Year); cmd.Parameters.AddWithValue("@Genre", _song.Genre); @@ -147,13 +147,13 @@ namespace Icarus.Controllers.Managers using (MySqlConnection conn = new MySqlConnection(_connectionString)) { conn.Open(); - string query = "INSERT INTO Songs(Title, Album, Artist, Year, Genre, Duration, " + - ", Filename, SongPath) VALUES(@Title, @Album, @Artist, @Year, @Genre, " + + string query = "INSERT INTO Songs(Title, AlbumTitle, Artist, Year, Genre, Duration, " + + ", Filename, SongPath) VALUES(@Title, @AlbumTitle, @Artist, @Year, @Genre, " + "@Duration, @Filename, @SongPath)"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", song.Title); - cmd.Parameters.AddWithValue("@Album", song.Album); + cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle); cmd.Parameters.AddWithValue("@Artist", song.Artist); cmd.Parameters.AddWithValue("@Year", song.Year); cmd.Parameters.AddWithValue("@Genre", song.Genre); @@ -235,12 +235,8 @@ namespace Icarus.Controllers.Managers { try { - // TODO: Define and implement method that will take care of the - // song and album records, eventually will address the artist - // records. Make use of helper functions _logger.Info("Starting process to save song to the filesystem"); var fileTempPath = Path.Combine(_tempDirectoryRoot, songFile.FileName); - //await SaveSongToFileSystemTemp(song, fileTempPath); var song = await SaveSongTemp(songFile, fileTempPath); System.IO.File.Delete(fileTempPath); @@ -372,13 +368,13 @@ namespace Icarus.Controllers.Managers } - Song RetrieveMetaData(string filePath) + private Song RetrieveMetaData(string filePath) { Song newSong = new Song { Title = "Untitled", Artist = "Untitled", - Album = "Untitled", + AlbumTitle = "Untitled", Year = 0, Genre = "Untitled", Duration = 0, @@ -404,7 +400,7 @@ namespace Icarus.Controllers.Managers newSong.Artist = artist; album = tag.Album; Console.WriteLine("Album: {0}", album); - newSong.Album = album; + newSong.AlbumTitle = album; genre = "Not Implemented"; Console.WriteLine("Genre: {0}", genre); newSong.Genre = genre; @@ -428,7 +424,7 @@ namespace Icarus.Controllers.Managers return newSong; } - async Task RetrieveSongFromFileSystem(Song details) + private async Task RetrieveSongFromFileSystem(Song details) { byte[] uncompressedSong = System.IO.File.ReadAllBytes(details.SongPath); @@ -497,31 +493,36 @@ namespace Icarus.Controllers.Managers private void SaveSongToDatabase(Song song, MusicStoreContext songStore, AlbumStoreContext albumStore, ArtistStoreContext artistStore) { - // TODO: Not finished and have not been tested + song.AlbumId = 1; var album = new Album(); - album.Title = song.Album; + album.Title = song.AlbumTitle; album.AlbumArtist = song.Artist; if (!albumStore.DoesAlbumExist(song)) { album.SongCount = 1; _logger.Info("Saving album to database"); + _logger.Info($"Ablum song count before retrieving from store: {album.SongCount}"); + _logger.Info($"Ablum Id before retrieving from store: {album.AlbumId}"); albumStore.SaveAlbum(album); _logger.Info("Album suuccessfully saved to database"); - album = albumStore.GetAlbum(album); + album = albumStore.GetAlbum(song); + _logger.Info($"Ablum song count after retrieving from store: {album.SongCount}"); + _logger.Info($"Ablum Id after retrieving from store: {album.AlbumId}"); } else { var albumRetrieved = albumStore.GetAlbum(song); - album.Id = albumRetrieved.Id; + album.AlbumId = albumRetrieved.AlbumId; album.SongCount = albumRetrieved.SongCount + 1; - // TODO: Add functionality to the method below albumStore.UpdateAlbum(album); } - song.AlbumId = album.Id; - // TODO: Update the method below to save the newly added AlbumId value + song.AlbumId = album.AlbumId; + _logger.Info($"Song;\nTitle {song.Title}\nAlbum {song.AlbumTitle}\nAlbum Id {song.AlbumId}"); + songStore.SaveSong(song); + _logger.Info("Successfully saved song to database"); } private async Task PopulateSongDetails() @@ -541,7 +542,7 @@ namespace Icarus.Controllers.Managers song.Title = row[col].ToString(); break; case "ALBUM": - song.Album = row[col].ToString(); + song.AlbumTitle = row[col].ToString(); break; case "ARTIST": song.Artist = row[col].ToString(); diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index b41a593..b979f1d 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -86,11 +86,9 @@ namespace Icarus.Controllers if (sng.Length > 0) { Console.WriteLine($"Song filename {sng.FileName}"); _logger.LogInformation($"Song filename {sng.FileName}"); - // TODO: Add functionality for overloaded method + await _songMgr.SaveSongToFileSystem(sng, songStoreContext, albumStoreContext, artistStoreContext); - var song = _songMgr.SongDetails; - songStoreContext.SaveSong(song); } } } diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index ab11951..7f0681a 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -59,7 +59,7 @@ namespace Icarus.Controllers.Utilities song.Title = _title; song.Artist = _artist; - song.Album = _album; + song.AlbumTitle = _album; song.Genre = _genre; song.Year = _year; song.Duration = _duration; @@ -120,7 +120,7 @@ namespace Icarus.Controllers.Utilities var filePath = updatedSong.SongPath; var title = updatedSong.Title; var artist = updatedSong.Artist; - var album = updatedSong.Album; + var album = updatedSong.AlbumTitle; var genre = updatedSong.Genre; var year = updatedSong.Year; TagLib.File fileTag = TagLib.File.Create(filePath); @@ -144,7 +144,7 @@ namespace Icarus.Controllers.Utilities fileTag.Tag.Artists = new []{artist}; break; case "album": - _updatedSong.Album = album; + _updatedSong.AlbumTitle = album; fileTag.Tag.Album = album; break; case "genre": @@ -175,7 +175,7 @@ namespace Icarus.Controllers.Utilities { Id = song.Id, Title = song.Title, - Album = song.Album, + AlbumTitle = song.AlbumTitle, Artist = song.Artist, Genre = song.Genre, Year = song.Year, @@ -230,7 +230,7 @@ namespace Icarus.Controllers.Utilities { songValues["Title"] = String.IsNullOrEmpty(song.Title); songValues["Artists"] = String.IsNullOrEmpty(song.Artist); - songValues["Album"] = String.IsNullOrEmpty(song.Album); + songValues["Album"] = String.IsNullOrEmpty(song.AlbumTitle); songValues["Genre"] = String.IsNullOrEmpty(song.Genre); if (song.Year==0) { diff --git a/Models/Album.cs b/Models/Album.cs index 61abb57..7c40e44 100644 --- a/Models/Album.cs +++ b/Models/Album.cs @@ -9,13 +9,14 @@ namespace Icarus.Models public class Album { [JsonProperty("id")] - public int Id { get; set; } + public int AlbumId { get; set; } [JsonProperty("title")] public string Title { get; set; } [JsonProperty("album_artist")] public string AlbumArtist { get; set; } [JsonProperty("song_count")] public int SongCount { get; set; } + [JsonIgnore] public List Songs { get; set; } } diff --git a/Models/Context/AlbumContext.cs b/Models/Context/AlbumContext.cs index 51a449b..587a114 100644 --- a/Models/Context/AlbumContext.cs +++ b/Models/Context/AlbumContext.cs @@ -20,7 +20,8 @@ namespace Icarus.Models.Context protected override void OnModelCreating(ModelBuilder modelBuilder) { - modelBuilder.Entity(); + modelBuilder.Entity() + .ToTable("Album"); } } } diff --git a/Models/Context/AlbumStoreContext.cs b/Models/Context/AlbumStoreContext.cs index 64eb08c..c34a1b8 100644 --- a/Models/Context/AlbumStoreContext.cs +++ b/Models/Context/AlbumStoreContext.cs @@ -45,13 +45,13 @@ namespace Icarus.Models.Context { while (reader.Read()) { - var id = Convert.ToInt32(reader["Id"]); + 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 { - Id = id, + AlbumId = id, Title = title, AlbumArtist = albumArtist, SongCount = songCount @@ -78,22 +78,22 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "SELECT * FROM Album WHERE Id=@Id"; + var query = "SELECT * FROM Album WHERE AlbumId=@AlbumId"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { - cmd.Parameters.AddWithValue("@Id", album.Id); + cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { - var id = Convert.ToInt32(reader["Id"]); + 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 { - Id = id, + AlbumId = id, Title = title, AlbumArtist = albumArtist, SongCount = songCount @@ -121,21 +121,22 @@ 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.Album); + cmd.Parameters.AddWithValue("@Title", song.AlbumTitle); using (var reader = cmd.ExecuteReader()) { while (reader.Read()) { - var id = Convert.ToInt32(reader["Id"]); + 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 { - Id = id, + AlbumId = id, Title = title, AlbumArtist = albumArtist, SongCount = songCount @@ -162,10 +163,10 @@ namespace Icarus.Models.Context { conn.Open(); - var query = "SELECT * FROM Album WHERE Id=@Id"; + var query = "SELECT * FROM Album WHERE AlbumId=@AlbumId"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { - cmd.Parameters.AddWithValue("@Id", album.Id); + cmd.Parameters.AddWithValue("@AlbumId", album.AlbumId); using (var reader = cmd.ExecuteReader()) { @@ -198,7 +199,7 @@ namespace Icarus.Models.Context var query = "SELECT * FROM Album WHERE Title=@Title"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { - cmd.Parameters.AddWithValue("@Title", song.Album); + cmd.Parameters.AddWithValue("@Title", song.AlbumTitle); using (var reader = cmd.ExecuteReader()) { @@ -230,7 +231,7 @@ namespace Icarus.Models.Context conn.Open(); var query = "INSERT INTO Album(Title, AlbumArtist, " + "SongCount) VALUES (@Title, @AlbumArtist, " + - "SongCount)"; + "@SongCount)"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", album.Title); @@ -251,6 +252,22 @@ namespace Icarus.Models.Context { try { + using (MySqlConnection conn = GetConnection()) + { + conn.Open(); + var query = "UPDATE Album SET Title=@Title, AlbumArtist=" + + "@AlbumArtist, SongCount=@SongCount 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(); + } + } } catch (Exception ex) { @@ -264,13 +281,13 @@ namespace Icarus.Models.Context List albums = new List(); while (reader.Read()) { - var id = Convert.ToInt32(reader["Id"]); + 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 { - Id = id, + AlbumId = id, Title = title, AlbumArtist = albumArtist, SongCount = songCount @@ -282,20 +299,22 @@ namespace Icarus.Models.Context private Album ParseSingleData(MySqlDataReader reader) { Album album = new Album(); + _logger.Info("Retrieving single album record"); while (reader.Read()) { - var id = Convert.ToInt32(reader["Id"]); + 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 { - Id = id, + AlbumId = id, Title = title, AlbumArtist = albumArtist, SongCount = songCount }; } + _logger.Info("Single ablum retreived"); return album; } diff --git a/Models/Context/MusicStoreContext.cs b/Models/Context/MusicStoreContext.cs index e8aac0c..0d37075 100644 --- a/Models/Context/MusicStoreContext.cs +++ b/Models/Context/MusicStoreContext.cs @@ -33,20 +33,21 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - string query = "INSERT INTO Songs(Title, Album, Artist," + - " Year, Genre, Duration, Filename, SongPath) " + - "VALUES(@Title, @Album, @Artist, @Year, @Genre, " + - "@Duration, @Filename, @SongPath)"; + string query = "INSERT INTO Song(Title, AlbumTitle, Artist," + + " Year, Genre, Duration, Filename, SongPath, AlbumId) " + + "VALUES(@Title, @AlbumTitle, @Artist, @Year, @Genre, " + + "@Duration, @Filename, @SongPath, @AlbumId)"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", song.Title); - cmd.Parameters.AddWithValue("@Album", song.Album); + cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle); cmd.Parameters.AddWithValue("@Artist", song.Artist); cmd.Parameters.AddWithValue("@Year", song.Year); cmd.Parameters.AddWithValue("@Genre", song.Genre); cmd.Parameters.AddWithValue("@Duration", song.Duration); cmd.Parameters.AddWithValue("@Filename", song.Filename); cmd.Parameters.AddWithValue("@SongPath", song.SongPath); + cmd.Parameters.AddWithValue("@AlbumId", song.AlbumId); cmd.ExecuteNonQuery(); } @@ -56,8 +57,10 @@ namespace Icarus.Models.Context { var exMsg = ex.Message; Console.WriteLine($"An error occurred:\n{exMsg}"); + _logger.Error(exMsg, "An error occurred"); } } + // TODO: Update this method to be compatible with the new AlbumId field public void UpdateSong(Song song) { try @@ -66,14 +69,14 @@ namespace Icarus.Models.Context { conn.Open(); - string query = "UPDATE Songs SET Title=@Title, Album=@Album, " + + string query = "UPDATE Song SET Title=@Title, AlbumTitle=@AlbumTitle, " + "Artist=@Artist, Year=@Year, Genre=@Genre, " + "Duration=@Duration, Filename=@Filename, " + "SongPath=@SongPath WHERE Id=@Id"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", song.Title); - cmd.Parameters.AddWithValue("@Album", song.Album); + cmd.Parameters.AddWithValue("@AlbumTitle", song.AlbumTitle); cmd.Parameters.AddWithValue("@Artist", song.Artist); cmd.Parameters.AddWithValue("@Year", song.Year); cmd.Parameters.AddWithValue("@Genre", song.Genre); @@ -91,6 +94,7 @@ namespace Icarus.Models.Context var msg = ex.Message; Console.WriteLine("An error occurred in MusicStoreContext:"); Console.WriteLine(msg); + _logger.Error(msg, "An error occurred"); } } public void DeleteSong(int id) @@ -100,7 +104,7 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - string query = "DELETE FROM Songs WHERE Id=@Id"; + string query = "DELETE FROM Song WHERE Id=@Id"; using (MySqlCommand cmd = new MySqlCommand(query, conn)) { @@ -114,6 +118,7 @@ namespace Icarus.Models.Context { var exMsg = ex.Message; Console.WriteLine($"An error occurred:\n{exMsg}"); + _logger.Error(exMsg, "An error occurred"); } } @@ -125,7 +130,7 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "SELECT * FROM Songs"; + var query = "SELECT * FROM Song"; MySqlCommand cmd = new MySqlCommand(query, conn); using (var reader = cmd.ExecuteReader()) { @@ -135,7 +140,7 @@ namespace Icarus.Models.Context { Id = Convert.ToInt32(reader["Id"]), Title = reader["Title"].ToString(), - Album = reader["Album"].ToString(), + AlbumTitle = reader["AlbumTitle"].ToString(), Artist = reader["Artist"].ToString(), Year = Convert.ToInt32(reader["Year"]), Genre = reader["Genre"].ToString(), @@ -152,6 +157,7 @@ namespace Icarus.Models.Context var exMsg = ex.Message; Console.WriteLine($"An error ocurred:\n{exMsg}"); songs.Clear(); + _logger.Error(exMsg, "An error occurred"); } return songs; @@ -166,7 +172,7 @@ namespace Icarus.Models.Context using (MySqlConnection conn = GetConnection()) { conn.Open(); - var query = "SELECT * FROM Songs WHERE Id=@Id"; + var query = "SELECT * FROM Song WHERE Id=@Id"; MySqlCommand cmd = new MySqlCommand(query, conn); cmd.Parameters.AddWithValue("@Id", id); @@ -179,7 +185,7 @@ namespace Icarus.Models.Context { Id = Convert.ToInt32(reader["Id"]), Title = reader["Title"].ToString(), - Album = reader["Album"].ToString(), + AlbumTitle = reader["AlbumTitle"].ToString(), Artist = reader["Artist"].ToString(), Year = Convert.ToInt32(reader["Year"]), Genre = reader["Genre"].ToString(), @@ -196,6 +202,7 @@ namespace Icarus.Models.Context { var exMsg = ex.Message; Console.WriteLine($"An error ocurred: {exMsg}"); + _logger.Error(exMsg, "An error occurred"); } return song; @@ -211,7 +218,7 @@ namespace Icarus.Models.Context { Id = Convert.ToInt32(reader["Id"]), Title = reader["Title"].ToString(), - Album = reader["Album"].ToString(), + AlbumTitle = reader["AlbumTitle"].ToString(), Artist = reader["Artist"].ToString(), Year = Convert.ToInt32(reader["Year"]), Genre = reader["Genre"].ToString(), @@ -235,7 +242,7 @@ namespace Icarus.Models.Context { Id = Convert.ToInt32(reader["Id"]), Title = reader["Title"].ToString(), - Album = reader["Album"].ToString(), + AlbumTitle = reader["AlbumTitle"].ToString(), Artist = reader["Artist"].ToString(), Year = Convert.ToInt32(reader["Year"]), Genre = reader["Genre"].ToString(), diff --git a/Models/Context/SongContext.cs b/Models/Context/SongContext.cs index 5f93385..3fd05ce 100644 --- a/Models/Context/SongContext.cs +++ b/Models/Context/SongContext.cs @@ -22,7 +22,8 @@ namespace Icarus.Models.Context protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity() - .HasOne(s => s.AlbumObject) + .ToTable("Song") + .HasOne(s => s.Album) .WithMany(a => a.Songs) .HasForeignKey(s => s.AlbumId) .HasConstraintName("ForeignKey_Song_Album"); diff --git a/Models/Song.cs b/Models/Song.cs index 67b050f..82750ef 100644 --- a/Models/Song.cs +++ b/Models/Song.cs @@ -17,7 +17,7 @@ namespace Icarus.Models [JsonProperty("title")] public string Title { get; set; } [JsonProperty("album")] - public string Album { get; set; } + public string AlbumTitle { get; set; } [JsonProperty("artist")] public string Artist { get; set; } [JsonProperty("year")] @@ -30,8 +30,9 @@ namespace Icarus.Models public string Filename { get; set; } [JsonProperty("song_path")] public string SongPath { get; set; } + [JsonIgnore] - public Album AlbumObject { get; set; } + public Album Album { get; set; } [JsonIgnore] public int AlbumId { get; set; } }