public async Task <List <HackerNewsStory> > SearchStory(string searchTerm)
        {
            List <HackerNewsStory> stories = new List <HackerNewsStory>();
            var response = await _hackerNewsRepo.BestStoriesAsync();

            if (response.IsSuccessStatusCode)
            {
                var storyIds = response.Content.ReadFromJsonAsync <List <int> >().Result;


                var tasks = storyIds.Select(GetStoryAync);

                stories = (await Task.WhenAll(tasks)).ToList();
                if (!String.IsNullOrEmpty(searchTerm))
                {
                    var search = searchTerm.ToLower();
                    stories = stories.Where(
                        s => s.title.ToLower().IndexOf(search) > -1 || s.by.ToLower().IndexOf(search) > -1
                        ).ToList();
                }
            }

            return(stories);
        }
Example #2
0
        public async Task <List <HackerNewsStory> > Index(string searchTerm)
        {
            List <HackerNewsStory> stories = new List <HackerNewsStory>();

            var response = await _repo.BestStoriesAsync();

            if (response.IsSuccessStatusCode)
            {
                var storiesResponse = response.Content.ReadAsStringAsync().Result;
                var bestIds         = JsonConvert.DeserializeObject <List <int> >(storiesResponse);

                var tasks = bestIds.Select(GetStoryAsync);
                stories = (await Task.WhenAll(tasks)).ToList();

                if (!String.IsNullOrEmpty(searchTerm))
                {
                    var search = searchTerm.ToLower();
                    stories = stories.Where(s =>
                                            s.Title.ToLower().IndexOf(search) > -1 || s.By.ToLower().IndexOf(search) > -1)
                              .ToList();
                }
            }
            return(stories);
        }