public async Task <IActionResult> Index()
        {
            var url = new Uri($"https://api.github.com/repositories?since=364");
            List <Repositorios> repositorio = new List <Repositorios>();

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("User-Agent", "Other");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));
                var response = await client.GetAsync(url);

                if (response.IsSuccessStatusCode)
                {
                    string lista = await response.Content.ReadAsStringAsync();

                    repositorio = JsonConvert.DeserializeObject <List <Repositorios> >(lista) as  List <Repositorios>;
                }
                if (ModelState.IsValid)
                {
                    foreach (var item in repositorio)
                    {
                        _context.Add(item);
                        await _context.SaveChangesAsync();
                    }
                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(repositorio));
        }
Example #2
0
        public async Task SaveSearchResult(IEnumerable <Repository> repos)
        {
            var unSavedRepos = repos.Where(repo => !_gitHubContext.Repositories.Any(rep => rep.Id == repo.Id));

            _gitHubContext.Repositories.AddRange(unSavedRepos);
            await _gitHubContext.SaveChangesAsync();

            return;
        }