public IActionResult LikeSong(string trackId) { User currentUser = database.User.ToList().Find(u => u.Email == LoginController.UserEmail); LikedSongs song = ItunesDAL.SaveLike(trackId); currentUser.LikedSongs.Add(song); var saved = false; while (!saved) { try { // Attempt to save changes to the database database.User.Update(currentUser).State = EntityState.Modified; database.SaveChanges(); saved = true; } catch (DbUpdateConcurrencyException ex) { foreach (var entry in ex.Entries) { if (entry.Entity is User) { var proposedValues = entry.CurrentValues; var databaseValues = entry.GetDatabaseValues(); foreach (var property in proposedValues.Properties) { var proposedValue = proposedValues[property]; var databaseValue = databaseValues[property]; // TODO: decide which value should be written to database // proposedValues[property] = <value to be saved>; } // Refresh original values to bypass next concurrency check entry.OriginalValues.SetValues(databaseValues); } else { throw new NotSupportedException( "Don't know how to handle concurrency conflicts for " + entry.Metadata.Name); } } } } return(RedirectToAction("HomePage")); }
public IActionResult HomePage() { List <int> songIds = new List <int>(SongsArray.Songs); List <int> removeSongs = new List <int>(); var currentUser = database.User.ToList().Find(u => u.Email == LoginController.UserEmail); var currentUserLikes = from likedSong in database.LikedSongs where likedSong.UserId == currentUser.Id select likedSong; var currentUserDislikes = from dislikedSong in database.DislikedSongs where dislikedSong.UserId == currentUser.Id select dislikedSong; foreach (var song in songIds) { foreach (var usersLikedSongs in currentUserLikes) { if (usersLikedSongs.TrackId == song) { removeSongs.Add(song); } } foreach (var usersDislikedSongs in currentUserDislikes) { if (usersDislikedSongs.TrackId == song) { removeSongs.Add(song); } } } foreach (var song in removeSongs) { songIds.Remove(song); } if (songIds.Count() > 0) { SongInfo currentSong = ItunesDAL.FindSong(songIds[0]); return(View(currentSong)); } return(View()); }