private void UpdateMovieMovies(string[] selectedMovies, Rental rentalToUpdate) { if (selectedMovies == null) { rentalToUpdate.MovieAssigns = new List <MovieAssign>(); return; } var selectedMovieHS = new HashSet <string>(selectedMovies); var rentalMovies = new HashSet <int> (rentalToUpdate.MovieAssigns.Select(c => c.Movie.MovieID)); foreach (var movie in _context.Movies) { if (selectedMovieHS.Contains(movie.MovieID.ToString())) { if (!rentalMovies.Contains(movie.MovieID)) { rentalToUpdate.MovieAssigns.Add(new MovieAssign { RentalID = rentalToUpdate.RentalID, MovieID = movie.MovieID }); } } else { if (rentalMovies.Contains(movie.MovieID)) { MovieAssign movieToRemove = rentalToUpdate.MovieAssigns.FirstOrDefault(i => i.MovieID == movie.MovieID); _context.Remove(movieToRemove); } } } }
private void UpdateMovieGenres(string[] selectedGenres, Movie movieToUpdate) { if (selectedGenres == null) { movieToUpdate.GenreAssigns = new List <GenreAssign>(); return; } var selectedGenreHS = new HashSet <string>(selectedGenres); var movieGenres = new HashSet <int> (movieToUpdate.GenreAssigns.Select(c => c.Genre.GenreID)); foreach (var genre in _context.Genres) { if (selectedGenreHS.Contains(genre.GenreID.ToString())) { if (!movieGenres.Contains(genre.GenreID)) { movieToUpdate.GenreAssigns.Add(new GenreAssign { MovieID = movieToUpdate.MovieID, GenreID = genre.GenreID }); } } else { if (movieGenres.Contains(genre.GenreID)) { GenreAssign genreToRemove = movieToUpdate.GenreAssigns.FirstOrDefault(i => i.GenreID == genre.GenreID); _context.Remove(genreToRemove); } } } }