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); }
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); } }
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); }