Resolved issue with Song record being deleted after updating the metadata records. #37

This commit is contained in:
amazing-username
2019-05-20 00:56:51 -04:00
parent c9e8e2c6da
commit ada7939866
5 changed files with 51 additions and 19 deletions
+33 -13
View File
@@ -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()
+1 -1
View File
@@ -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
+1 -1
View File
@@ -60,7 +60,7 @@ namespace Icarus.Controllers
}
[HttpPost]
[Authorize("upload:songs")]
//[Authorize("upload:songs")]
public async Task Post([FromForm(Name = "file")] List<IFormFile> songData)
{
try
@@ -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");
+13 -4
View File
@@ -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())
};
}