示例#1
0
 public async Task <ActionResult <Movie> > Post(Movie movie)
 {
     try
     {
         _context.Add(movie);
         await _context.SaveChangesAsync();
     }
     catch (Exception e)
     {
         return(BadRequest("Exception..." + e.Message));
     }
     return(Ok(movie));
 }
        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)));
        }
示例#3
0
 public void Create(Show show)
 {
     db.Add(show);
     db.SaveChanges();
 }