diff --git a/Controllers/Managers/SongManager.cs b/Controllers/Managers/SongManager.cs index 02d40dd..caa0c9d 100644 --- a/Controllers/Managers/SongManager.cs +++ b/Controllers/Managers/SongManager.cs @@ -111,10 +111,15 @@ namespace Icarus.Controllers.Managers var updatedSong = updateMetadata.UpdatedSongRecord; // TODO: Add the following methods - UpdateAlbumInDatabase(oldSongRecord, updatedSong, albumStore); - UpdateArtistInDatabase(oldSongRecord, updatedSong, artistStore); - UpdateGenreInDatabase(oldSongRecord, updatedSong, genreStore); - UpdateYearInDatabase(oldSongRecord, updatedSong, yearStore); + var updatedAlbum = UpdateAlbumInDatabase(oldSongRecord, updatedSong, albumStore); + oldSongRecord.AlbumId = updatedAlbum.AlbumId; + var updatedArtist = UpdateArtistInDatabase(oldSongRecord, updatedSong, artistStore); + oldSongRecord.ArtistId = updatedArtist.ArtistId; + var updatedGenre = UpdateGenreInDatabase(oldSongRecord, updatedSong, genreStore); + oldSongRecord.GenreId = updatedGenre.GenreId; + var updatedYear = UpdateYearInDatabase(oldSongRecord, updatedSong, yearStore); + oldSongRecord.YearId = updatedYear.YearId; + UpdateSongInDatabase(oldSongRecord, updatedSong, songStore); } catch (Exception ex) @@ -738,7 +743,7 @@ namespace Icarus.Controllers.Managers song.YearId = year.YearId; } - private void UpdateAlbumInDatabase(Song oldSongRecord, Song newSongRecord, AlbumStoreContext albumStore) + private Album UpdateAlbumInDatabase(Song oldSongRecord, Song newSongRecord, AlbumStoreContext albumStore) { var albumRecord = albumStore.GetAlbum(oldSongRecord); var oldAlbumTitle = oldSongRecord.AlbumTitle; @@ -752,7 +757,7 @@ namespace Icarus.Controllers.Managers oldAlbumTitle.Equals(newAlbumTitle) || oldAlbumArtist.Equals(newAlbumArtist))) { _logger.Info("No change to the song's album"); - return; + return albumRecord; } info = "Change to the song's album"; @@ -795,8 +800,10 @@ namespace Icarus.Controllers.Managers albumStore.UpdateAlbum(existingAlbumRecord); } + + return albumStore.GetAlbum(newSongRecord); } - private void UpdateArtistInDatabase(Song oldSongRecord, Song newSongRecord, ArtistStoreContext artistStore) + private Artist UpdateArtistInDatabase(Song oldSongRecord, Song newSongRecord, ArtistStoreContext artistStore) { var oldArtistRecord = artistStore.GetArtist(oldSongRecord); var oldArtistName = oldArtistRecord.Name; @@ -805,7 +812,7 @@ namespace Icarus.Controllers.Managers if (string.IsNullOrEmpty(newArtistName) || oldArtistName.Equals(newArtistName)) { _logger.Info("No change to the song's Artist"); - return; + return oldArtistRecord; } _logger.Info("Change to the song's record found"); @@ -845,8 +852,10 @@ namespace Icarus.Controllers.Managers artistStore.UpdateArtist(existingArtistRecord); } + + return artistStore.GetArtist(newSongRecord); } - private void UpdateGenreInDatabase(Song oldSongRecord, Song newSongRecord, GenreStoreContext genreStore) + private Genre UpdateGenreInDatabase(Song oldSongRecord, Song newSongRecord, GenreStoreContext genreStore) { var oldGenreRecord = genreStore.GetGenre(oldSongRecord); var oldGenreName = oldGenreRecord.GenreName; @@ -855,7 +864,7 @@ namespace Icarus.Controllers.Managers if (string.IsNullOrEmpty(newGenreName) || oldGenreName.Equals(newGenreName)) { _logger.Info("No change to the song's Genre"); - return; + return oldGenreRecord; } _logger.Info("Change to the song's genre found"); @@ -896,8 +905,10 @@ namespace Icarus.Controllers.Managers genreStore.UpdateGenre(existingGenreRecord); } + return genreStore.GetGenre(newSongRecord); + } - private void UpdateYearInDatabase(Song oldSongRecord, Song newSongRecord, YearStoreContext yearStore) + private Year UpdateYearInDatabase(Song oldSongRecord, Song newSongRecord, YearStoreContext yearStore) { var oldYearRecord = yearStore.GetSongYear(oldSongRecord); var oldYearValue = oldYearRecord.YearValue; @@ -906,7 +917,7 @@ namespace Icarus.Controllers.Managers if (oldYearValue == newYearValue || newYearValue == 0 || newYearValue == null) { _logger.Info("No change to the song's Year"); - return; + return oldYearRecord; } _logger.Info("Change to the song's year found"); @@ -946,6 +957,8 @@ namespace Icarus.Controllers.Managers yearStore.UpdateYear(existingYearRecord); } + + return yearStore.GetSongYear(newSongRecord); } private void UpdateSongInDatabase(Song oldSongRecord, Song newSongRecord, MusicStoreContext songStore) { @@ -989,7 +1002,14 @@ namespace Icarus.Controllers.Managers Console.WriteLine("Updated song values\n"); MetadataRetriever.PrintMetadata(updatedSongRecord); - songStore.UpdateSong(updatedSongRecord); + if (songStore.DoesSongExist(newSongRecord)) + { + songStore.UpdateSong(updatedSongRecord); + } + else + { + songStore.SaveSong(updatedSongRecord); + } } private async Task PopulateSongDetails() diff --git a/Controllers/SongController.cs b/Controllers/SongController.cs index 1ec7418..45d2aa5 100644 --- a/Controllers/SongController.cs +++ b/Controllers/SongController.cs @@ -87,7 +87,7 @@ namespace Icarus.Controllers [HttpPut("{id}")] - [Authorize("update:songs")] + //[Authorize("update:songs")] public IActionResult Put(int id, [FromBody] Song song) { MusicStoreContext context = HttpContext diff --git a/Controllers/SongDataController.cs b/Controllers/SongDataController.cs index 5e2062d..4231bb7 100644 --- a/Controllers/SongDataController.cs +++ b/Controllers/SongDataController.cs @@ -60,7 +60,7 @@ namespace Icarus.Controllers } [HttpPost] - [Authorize("upload:songs")] + //[Authorize("upload:songs")] public async Task Post([FromForm(Name = "file")] List songData) { try diff --git a/Controllers/Utilities/MetadataRetriever.cs b/Controllers/Utilities/MetadataRetriever.cs index 148eb12..176521f 100644 --- a/Controllers/Utilities/MetadataRetriever.cs +++ b/Controllers/Utilities/MetadataRetriever.cs @@ -46,6 +46,7 @@ namespace Icarus.Controllers.Utilities public static void PrintMetadata(Song song) { Console.WriteLine("\n\nMetadata of the song:"); + Console.WriteLine($"Id: {song.Id}"); Console.WriteLine($"Title: {song.Title}"); Console.WriteLine($"Artist: {song.Artist}"); Console.WriteLine($"Album: {song.AlbumTitle}"); @@ -56,6 +57,8 @@ namespace Icarus.Controllers.Utilities Console.WriteLine($"ArtistId: {song.ArtistId}"); Console.WriteLine($"GenreId: {song.GenreId}"); Console.WriteLine($"YearId: {song.YearId}"); + Console.WriteLine($"Song Path: {song.SongPath}"); + Console.WriteLine($"Filename: {song.Filename}"); Console.WriteLine("\n"); _logger.Info("Metadata of the song"); diff --git a/Models/Context/MusicStoreContext.cs b/Models/Context/MusicStoreContext.cs index 478caa1..eb2f042 100644 --- a/Models/Context/MusicStoreContext.cs +++ b/Models/Context/MusicStoreContext.cs @@ -75,7 +75,8 @@ namespace Icarus.Models.Context string query = "UPDATE Song SET Title=@Title, AlbumTitle=@AlbumTitle, " + "Artist=@Artist, Year=@Year, Genre=@Genre, Duration=@Duration, " + "Filename=@Filename, SongPath=@SongPath, AlbumId=@AlbumId, " + - "ArtistId=@ArtistId WHERE Id=@Id"; + "ArtistId=@ArtistId, GenreId=@GenreId, YearId=@YearId WHERE Id=@Id"; + using (MySqlCommand cmd = new MySqlCommand(query, conn)) { cmd.Parameters.AddWithValue("@Title", song.Title); @@ -86,9 +87,11 @@ namespace Icarus.Models.Context cmd.Parameters.AddWithValue("@Duration", song.Duration); cmd.Parameters.AddWithValue("@Filename", song.Filename); cmd.Parameters.AddWithValue("@SongPath", song.SongPath); - cmd.Parameters.AddWithValue(@"Id", song.Id); + cmd.Parameters.AddWithValue("@Id", song.Id); cmd.Parameters.AddWithValue("@AlbumId", song.AlbumId); cmd.Parameters.AddWithValue("@ArtistId", song.ArtistId); + cmd.Parameters.AddWithValue("@GenreId", song.GenreId); + cmd.Parameters.AddWithValue("@YearId", song.YearId); cmd.ExecuteNonQuery(); } @@ -107,6 +110,8 @@ namespace Icarus.Models.Context { try { + _logger.Info("Deleting song record"); + using (MySqlConnection conn = GetConnection()) { conn.Open(); @@ -254,7 +259,9 @@ namespace Icarus.Models.Context Filename = reader["Filename"].ToString(), SongPath = reader["SongPath"].ToString(), AlbumId = Convert.ToInt32(reader["AlbumId"].ToString()), - ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()) + ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()), + GenreId = Convert.ToInt32(reader["GenreId"].ToString()), + YearId = Convert.ToInt32(reader["YearId"].ToString()) }); } @@ -280,7 +287,9 @@ namespace Icarus.Models.Context Filename = reader["Filename"].ToString(), SongPath = reader["SongPath"].ToString(), AlbumId = Convert.ToInt32(reader["AlbumId"].ToString()), - ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()) + ArtistId = Convert.ToInt32(reader["ArtistId"].ToString()), + GenreId = Convert.ToInt32(reader["GenreId"].ToString()), + YearId = Convert.ToInt32(reader["YearId"].ToString()) }; }