public async Task <Pageable <Story> > GetAllByTypeAsync(StoryClientRequest request) { var ids = await GetIdsByTypeAsync(request.type); var total = ids.Count(); var requestedPage = request.page; /*Do not load all if there is no search*/ if (string.IsNullOrEmpty(request.search)) { ids = ids.Page(request.page, request.pageSize); } /*load the id details in separate threads*/ IEnumerable <Story> stories = (await Task.WhenAll(ids.Select(GetByIdAsync))); stories = stories.Where(x => x?.link != null && x?.title != null); /*no filter ? easy return pageable*/ if (string.IsNullOrEmpty(request.search)) { Pageable <Story> pageable = new Pageable <Story>() { filtered = total, totalCount = total, pageSize = request.pageSize, page = requestedPage, content = stories }; pageable.Calculate(); return(pageable); } /*if we filter then call the pager*/ return(stories.Page(request)); }
public void CacheTest() { StoryClientRequest request = new StoryClientRequest { page = 0, pageSize = 1, type = StoryType.NewStories.ToString(), search = "" }; Pageable <Story> stories = Task.Run(async() => await repo.GetAllByTypeAsync(request)).Result; Assert.IsTrue(cache.TryGetValue(stories.content.FirstOrDefault().id, out Story s)); TestStory(s); }
public void GetByTypeAsync() { StoryClientRequest request = new StoryClientRequest { page = 0, pageSize = 10, type = StoryType.NewStories.ToString() }; Pageable <Story> stories = Task.Run(async() => await repo.GetAllByTypeAsync(request)).Result; Assert.IsTrue(stories.page == 0); Assert.IsTrue(stories.content.Count() == 10); Assert.IsTrue(stories.pageSize == 10); Assert.IsTrue(stories.totalCount > 10); }
public async Task <Pageable <Story> > Index(StoryClientRequest request) { return(await _repo.GetAllByTypeAsync(request)); }