public async Task <ActionResult> Index(HaikusGetQueryParams queryParams) { if (queryParams.Take == 0) { queryParams.Take = 20; } HaikusListViewModel model = new HaikusListViewModel(); var haikus = (await this.haikusService.GetHaikusAsync(queryParams).ConfigureAwait(false)).Select(i => new HaikuListItem() { Id = i.Id, Text = i.Text, Author = i.Author, Rating = i.Rating }).ToList(); var pageData = this.haikusService.GetHaikusPagingMetadata(); model.Haikus = new StaticPagedList <HaikuListItem>(haikus, queryParams.Skip / queryParams.Take + 1, queryParams.Take, pageData.TotalCount); model.QueryParams = new HaikusGetQueryParams() { SortBy = queryParams.SortBy, Order = queryParams.Order, Skip = queryParams.Skip, Take = queryParams.Take }; return(View(model)); }
public async Task <IEnumerable <HaikuGetDto> > GetHaikusAsync(HaikusGetQueryParams queryParams) { var preQuery = this.UnitOfWork.HaikusRepository.QueryInclude(h => h.User); IOrderedQueryable <HaikuEntity> sortQuery; if (queryParams.SortBy == HaikusSortBy.Date) { if (queryParams.Order == OrderType.Ascending) { sortQuery = preQuery.OrderBy(h => h.DatePublished); } else { sortQuery = preQuery.OrderByDescending(h => h.DatePublished); } } else { if (queryParams.Order == OrderType.Ascending) { sortQuery = preQuery.OrderBy(h => h.Rating); } else { sortQuery = preQuery.OrderByDescending(h => h.Rating); } } var pagingQuery = sortQuery.Skip(queryParams.Skip).Take(queryParams.Take); var data = await this.UnitOfWork.HaikusRepository.GetAllAsync(pagingQuery).ConfigureAwait(false); return(data.Select(h => Mapper.MapHaikuEntityToHaikuGetDto(h))); }