public async Task <FullTVShow> AddToDBFullTVShowDeails(FullTVShow fullTVShow) { using (var trans = _context.Database.BeginTransaction()) { try{ //AddTVShow await _context.TVShows.AddAsync(fullTVShow.tvshow); //AddTVShowGenres var tvShowGenresList = new List <TVShowGenres>(); foreach (var item in fullTVShow.genres_collection) { tvShowGenresList.Add(new TVShowGenres { tvshow_id = fullTVShow.tvshow.id, tvshow_genre_id = _context.Genres.FirstOrDefault(i => i.genre_id == item.id).id }); } await _context.TVShowGenres.AddRangeAsync(tvShowGenresList); //AddKeywords var keywordsList = new List <KeyWords>(); foreach (var item in fullTVShow.keyWords_collection) { keywordsList.Add(new KeyWords { keyword_id = item.id, name = item.name, material_type_id = 2 }); } await _context.KeyWords.AddRangeAsync(keywordsList); //AddTVShowKeywords var tvShowKeywordList = new List <TVShowKeyWords>(); foreach (var item in fullTVShow.keyWords_collection) { var tvShowKeyword = await _context.KeyWords.FirstOrDefaultAsync(k => k.keyword_id == item.id && k.material_type_id == 2); tvShowKeywordList.Add(new TVShowKeyWords { tvshow_id = fullTVShow.tvshow.id, tvshow_ = fullTVShow.tvshow, tvshow_keyword_id = tvShowKeyword.id, tvshow_keyword_ = tvShowKeyword }); } await _context.TVShowKeyWords.AddRangeAsync(tvShowKeywordList); //AddTVShowCredits var TVShowCreditList = new List <TVShowCredits>(); foreach (var item in fullTVShow.casts_collection) { TVShowCreditList.Add(new TVShowCredits { credit_id = item.credit_id, tvshow_id = fullTVShow.tvshow.id, tvshow_ = fullTVShow.tvshow, is_cast = true }); } foreach (var item in fullTVShow.crews_collection) { TVShowCreditList.Add(new TVShowCredits { credit_id = item.credit_id, tvshow_id = fullTVShow.tvshow.id, tvshow_ = fullTVShow.tvshow, is_cast = false }); } await _context.TVShowCredits.AddRangeAsync(TVShowCreditList); await _context.TVShowCasts.AddRangeAsync(fullTVShow.casts_collection); await _context.TVShowCrews.AddRangeAsync(fullTVShow.crews_collection); await _context.SaveChangesAsync(); trans.Commit(); return(fullTVShow); } catch (Exception ex) { trans.Rollback(); throw new Exception(ex.Message); } finally { trans.Dispose(); } } }
public async Task <FullMovie> AddToDBFullMovieDeails(FullMovie fullMovie) { using (var trans = _context.Database.BeginTransaction()) { try { //AddMovie await _context.Movies.AddAsync(fullMovie.movie); // AddMovieGenres var movieGenres = new List <MovieGenres>(); foreach (var item in fullMovie.genres_collection) { movieGenres.Add(new MovieGenres { movie_id = fullMovie.movie.id, movie_genre_id = _context.Genres.FirstOrDefault(g => g.genre_id == item.id && g.material_type_id == 1).id, }); } await _context.MovieGenres.AddRangeAsync(movieGenres); //AddKeyWords var KeyWords = new List <KeyWords>(); foreach (var item in fullMovie.keyWords_collection) { KeyWords.Add(new KeyWords { keyword_id = item.id, material_type_id = 1, name = item.name }); } await _context.KeyWords.AddRangeAsync(KeyWords); await _context.SaveChangesAsync(); //AddMovieKeyWords var movieKeyWords = new List <MovieKeyWords>(); foreach (var item in fullMovie.keyWords_collection) { var movieKeyWord = await _context.KeyWords.FirstOrDefaultAsync(k => k.keyword_id == item.id && k.material_type_id == 1); movieKeyWords.Add(new MovieKeyWords { movie_id = fullMovie.movie.id, movie_ = fullMovie.movie, movie_keyword_ = movieKeyWord, movie_keyword_id = movieKeyWord.id }); } await _context.MovieKeyWords.AddRangeAsync(movieKeyWords); //AddMovieCredits var movieCredits = new List <MovieCredits>(); foreach (var item in fullMovie.casts_collection) { movieCredits.Add(new MovieCredits { movie_id = fullMovie.movie.id, credit_id = item.credit_id, is_cast = true, movie_ = fullMovie.movie }); } foreach (var item in fullMovie.crews_collection) { movieCredits.Add(new MovieCredits { movie_id = fullMovie.movie.id, credit_id = item.credit_id, is_cast = false, movie_ = fullMovie.movie }); } await _context.MovieCredits.AddRangeAsync(movieCredits); await _context.MovieCasts.AddRangeAsync(fullMovie.casts_collection); await _context.MovieCrews.AddRangeAsync(fullMovie.crews_collection); await _context.SaveChangesAsync(); trans.Commit(); return(fullMovie); } catch (Exception ex) { trans.Rollback(); throw new Exception(ex.Message); } finally { trans.Dispose(); } } }