// [Authorize] public async Task <object> AddFullTVShowDetails([FromBody] FullTVShow fullTVShow) { try { return(await _tvShowRepository.AddToDBFullTVShowDeails(fullTVShow)); } catch (Exception ex) { _logger.LogError("create new movie failed"); throw ex; } }
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(); } } }