private async Task SaveShowData(RTLDbContext db, TVShowResponse showItem, CancellationToken cancellationToken) { var showCast = new List <ShowCast>(); var duplicateSet = new HashSet <int>(); foreach (var cast in showItem.Casts) { if (duplicateSet.Contains(cast.Id)) { continue; } if (await db.Actors.AnyAsync(s => s.Id == cast.Id, cancellationToken)) { if (!await db.TVShowCast .Where(s => s.CastId == cast.Id && s.ShowId == showItem.Id) .AnyAsync(cancellationToken)) { db.TVShowCast.Add(new ShowCast() { CastId = cast.Id, ShowId = showItem.Id }); } } else { showCast.Add(new ShowCast() { Cast = new Cast() { Name = cast.Name, Id = cast.Id, DoB = cast.DoB } }); } duplicateSet.Add(cast.Id); } var show = new TVShow() { Id = showItem.Id, Name = showItem.Name, ShowCasts = showCast }; db.TVShows.Add(show); await db.SaveChangesAsync(cancellationToken); }
public ActionResult GetSingleTVShow(int id) { TVShowEntity item = _repository.GetSingle(id); if (item == null) { return(NotFound()); } TVShowResponse response = _mapper.Map <TVShowResponse>(item); foreach (var temp in item.TVShowGenre) { response.Genres.Add(temp.Genre.Genre); } return(Ok(response)); }