예제 #1
0
        private List <HackerNewsModel> InsertInCached(HackerNewsModel model)
        {
            var list = GetAllCached();

            if (list.Any(x => x.Id == model.Id))
            {
                return(list);
            }

            list.Add(model);

            _cache.Set("HackerNews", list);

            return(list);
        }
예제 #2
0
        public async Task <HackerNewsModel> GetNewsByIdAsync(int newsId)
        {
            using (HttpClient client = new HttpClient())
            {
                HackerNewsModel newsModel = new HackerNewsModel();

                var response = await client.GetAsync(string.Format(Constants.ITEM_URL, newsId));

                if (response.IsSuccessStatusCode)
                {
                    var newsResponse = response.Content.ReadAsStringAsync().Result;
                    newsModel = JsonConvert.DeserializeObject <HackerNewsModel>(newsResponse);
                }

                return(newsModel);
            }
        }
예제 #3
0
        public List <HackerNewsModel> GetPage(List <int> ids, int take, string search, CancellationToken cancellationToken)
        {
            List <HackerNewsModel> resultList = new List <HackerNewsModel>();
            List <HackerNewsModel> cachedList = GetAllCached();

            foreach (var id in ids)
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    return(null);
                }

                HackerNewsModel item = cachedList.FirstOrDefault(x => x.Id == id);

                if (item == null)
                {
                    item = this.GetById(id).Result;

                    if (item == null)
                    {
                        continue;
                    }

                    cachedList = InsertInCached(item);
                }

                if (String.IsNullOrEmpty(search))
                {
                    resultList.Add(item);
                }

                else if (item.Title.ToLower().Contains(search.ToLower()))
                {
                    resultList.Add(item);
                }

                if (resultList.Count == take)
                {
                    break;
                }
            }

            return(resultList);
        }