private List <ShowWithCast> ShowsWithCasts(PagingParameterModel pagingparametermodel)
        {
            List <ShowWithCast> results = new List <ShowWithCast>();

            try
            {
                _ShowCastcontext = new ShowCastContext();
                _Showcontext     = new ShowContext();
                _Castcontext     = new CastContext();
                var shows = from sh in _Showcontext.Show
                            select sh;
                int ii        = shows.Count();
                var showsList = shows.Skip((pagingparametermodel.pageNumber - 1) * pagingparametermodel.pageSize).Take(pagingparametermodel.pageSize).ToList();

                foreach (Show s in showsList)
                {
                    ShowWithCast sc = new ShowWithCast();
                    sc.ShowID = s.ShowID;
                    sc.Name   = s.ShowName;

                    var showPairs = from sp in _ShowCastcontext.ShowCast
                                    where sp.ShowID == s.ShowID
                                    select sp;

                    sc.Cast = new List <Cast>();

                    foreach (ShowCast item in showPairs)
                    {
                        var casts = from cst in _Castcontext.Cast
                                    where cst.CastID == item.CastID
                                    select cst;

                        foreach (Cast aCast in casts)
                        {
                            sc.Cast.Add(aCast);
                        }
                    }
                    sc.Cast = sc.Cast.OrderByDescending(x => x.BirthDay).ToList();
                    results.Add(sc);
                }
            }
            catch (Exception ex)
            {
                string er = ex.ToString();
            }
            return(results);
        }
        public async Task <JsonResult> ScrapeShowsAndPersistData()
        {
            _ShowCastcontext = new ShowCastContext();
            _Showcontext     = new ShowContext();
            _Castcontext     = new CastContext();
            //Clears existing data in the database first:
            _Showcontext.Database.ExecuteSqlCommand("TRUNCATE TABLE [Show]");

            string url = "http://api.tvmaze.com/shows";
            List <ShowWithCast> jSonResult = new List <ShowWithCast>();

            try
            {
                _Showcontext = new ShowContext();
                var showName = from sn in _Showcontext.Show
                               select sn;


                using (var client = new HttpClient())
                {
                    using (var r = await client.GetAsync(new Uri(url)))
                    {
                        string JsonStr = await r.Content.ReadAsStringAsync();

                        var result = JsonConvert.DeserializeObject <List <ShowFull> >(JsonStr);

                        foreach (ShowFull fliek in result)
                        {
                            Show aShow = new Show();
                            aShow.ShowID   = fliek.id;
                            aShow.ShowName = fliek.name;
                            _Showcontext.Add(aShow);
                            await _Showcontext.SaveChangesAsync();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
            }
            var shows = from sctx in _Showcontext.Show
                        select sctx;

            return(await Task.FromResult(Json(shows)));
        }
        public async Task <JsonResult> PairShowsCastsAndPersistData()
        {
            _ShowCastcontext = new ShowCastContext();
            _Castcontext     = new CastContext();
            //Deletes all existing data in the local database first
            _ShowCastcontext.Database.ExecuteSqlCommand("TRUNCATE TABLE [ShowCast]");
            _Castcontext.Database.ExecuteSqlCommand("TRUNCATE TABLE [Cast]");
            string url = "";
            List <ShowWithCast> jSonResult = new List <ShowWithCast>();

            try
            {
                _Castcontext     = new CastContext();
                _ShowCastcontext = new ShowCastContext();

                var shows = from sh in _Showcontext.Show
                            select sh;

                int ShowCastCounter = 0;
                //loop through all the shows
                foreach (Show s in shows)
                {
                    //retrieves every show's cast
                    //Example - http://api.tvmaze.com/shows/1/cast
                    url = "http://api.tvmaze.com/shows/" + s.ShowID + "/cast";

                    using (var client = new HttpClient())
                    {
                        using (var r = await client.GetAsync(new Uri(url)))
                        {
                            string JsonStr2 = await r.Content.ReadAsStringAsync();

                            var result2 = JsonConvert.DeserializeObject <List <CastFull> >(JsonStr2);

                            foreach (CastFull cast in result2)
                            {
                                ShowCastCounter++;
                                Cast aCast = new Cast();
                                aCast.CastID   = cast.person.id;
                                aCast.Name     = cast.person.name;
                                aCast.BirthDay = cast.person.birthday;
                                _Castcontext   = new CastContext();

                                _Castcontext.Add(aCast);
                                await _Castcontext.SaveChangesAsync();

                                ShowCast cs = new ShowCast();
                                cs.ShowCastID    = ShowCastCounter;
                                cs.ShowID        = s.ShowID;
                                cs.CastID        = aCast.CastID;
                                _ShowCastcontext = new ShowCastContext();
                                _ShowCastcontext.Add(cs);
                                await _ShowCastcontext.SaveChangesAsync();
                            }
                        }
                    }
                }
                await _Castcontext.SaveChangesAsync();

                await _ShowCastcontext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                string error = ex.ToString();
            }

            _Castcontext = new CastContext();
            var casts = from sh in _Castcontext.Cast
                        select sh;

            return(await Task.FromResult(Json(casts)));
        }