public async Task <IActionResult> UpdateMovieAsync( [FromRoute, SwaggerParameter(Description = "Id of movie to update", Required = true)] Guid movieId, [FromBody, SwaggerParameter(Description = "Movie to update", Required = true)] MovieToUpdateDto movieToUpdate, [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType) { var movieFromDb = await _movieRepository.GetMovieAsync(movieId); // upserting if movie does not already exist if (movieFromDb == null) { var movieEntity = Mapper.Map <Movie>(movieToUpdate); movieEntity.Id = movieId; _movieRepository.AddMovie(movieEntity); if (!await _movieRepository.SaveChangesAsync()) { _logger.LogError($"Upserting movie: {movieId} failed on save"); } // if any genre is added to movie, create many-to-many relationship for each genre if (movieToUpdate.GenreIds.Count > 0) { for (int i = 0; i < movieToUpdate.GenreIds.Count; i++) { // create moviegenre object with genre id and movie id from movieToCreate object MovieGenre movieGenreToAdd = new MovieGenre { MovieId = movieId, GenreId = movieToUpdate.GenreIds[i] }; movieGenreToAdd.Id = Guid.NewGuid(); _movieGenreRepository.AddMovieGenre(movieGenreToAdd); } // save changes to database after all many-to-many relationships has been created if any exist if (!await _movieGenreRepository.SaveChangesAsync()) { _logger.LogError("Saving changes to database while deleting a moviegenre failed"); } } var movieToReturn = Mapper.Map <MovieDto>(movieEntity); if (mediaType == "application/vnd.biob.json+hateoas") { var links = CreateLinksForMovies(movieToReturn.Id, null); var linkedMovie = movieToReturn.ShapeData(null) as IDictionary <string, object>; linkedMovie.Add("links", links); return(CreatedAtRoute("GetMovie", new { movieId = movieToReturn.Id }, linkedMovie)); } else { return(CreatedAtRoute("GetMovie", new { movieId = movieToReturn.Id }, movieToReturn)); } } Mapper.Map(movieToUpdate, movieFromDb); _movieRepository.UpdateMovie(movieFromDb); if (!await _movieRepository.SaveChangesAsync()) { _logger.LogError($"Updating movie: {movieId} failed on save"); } // easy solution delete all moviegenres with id, add new ids var moviegenresExist = await _movieGenreRepository.GetAllMovieGenresByMovieIdAsync(movieId); // delete all existing many-to-many genre relationships for movie foreach (var moviegenre in moviegenresExist) { _movieGenreRepository.DeleteMovieGenre(moviegenre); } // if any genre is added to movie, create many-to-many relationship for each genre if (movieToUpdate.GenreIds.Count > 0) { for (int i = 0; i < movieToUpdate.GenreIds.Count; i++) { // create moviegenre object with genre id and movie id from movieToCreate object MovieGenre movieGenreToAdd = new MovieGenre { MovieId = movieId, GenreId = movieToUpdate.GenreIds[i] }; movieGenreToAdd.Id = Guid.NewGuid(); _movieGenreRepository.AddMovieGenre(movieGenreToAdd); } } // save changes to database after all many-to-many relationships has been deleted and recreated if any exist if (!await _movieGenreRepository.SaveChangesAsync()) { _logger.LogError("Saving changes to database while deleting a moviegenre failed"); } return(NoContent()); }