public async Task <CursorPaged <ReviewDto, DateTime?> > Handle(FindReviewsCursorQuery request, CancellationToken cancellationToken) { var filterQuery = _repository.Query(); if (request.ReviewStatus != null) { filterQuery = filterQuery.Where(s => s.ReviewStatus == request.ReviewStatus); } if (request.ProductId != null) { filterQuery = filterQuery.Where(s => s.ProductId == request.ProductId); } var result = await filterQuery .Where(s => s.CreatedDate <= request.PageToken) .OrderByDescending(s => s.CreatedDate) .Take(request.PageSize) .Select(s => new ReviewDto { Id = s.Id, Title = s.Title, Comment = s.Comment, Rating = s.Rating, ReviewStatus = s.ReviewStatus, CreatedById = s.CreatedById, CreatedDate = s.CreatedDate }) .ToListAsync(cancellationToken); var next = await filterQuery .Where(s => s.CreatedDate <= request.PageToken) .OrderByDescending(s => s.CreatedDate) .Skip(request.PageSize) .Select(s => new { s.CreatedDate }) .FirstOrDefaultAsync(cancellationToken); var previous = await filterQuery .Where(s => s.CreatedDate > request.PageToken) .OrderBy(s => s.CreatedDate) .Skip(request.PageSize - 1) .Select(s => new { s.CreatedDate }) .FirstOrDefaultAsync(cancellationToken); var paged = new CursorPaged <ReviewDto, DateTime?> { Data = result.ToList(), PreviousPageToken = previous?.CreatedDate, NextPageToken = next?.CreatedDate }; var response = await _identityServiceClient.GetUsersByIdsAsync(new GetUsersByIdsRequest { Ids = { paged.Data.Select(s => s.CreatedById).Distinct() } }); paged.Data.ForEach(s => s.CreatedByUserName = response.Users.SingleOrDefault(x => x.Id == s.CreatedById)?.UserName); return(paged); }
public async Task <OffsetPaged <ReviewDto> > Handle(FindReviewsOffsetQuery request, CancellationToken cancellationToken) { var filterQuery = _repository.Query(); if (request.ReviewStatus != null) { filterQuery = filterQuery.Where(s => s.ReviewStatus == request.ReviewStatus); } if (request.ProductId != null) { filterQuery = filterQuery.Where(s => s.ProductId == request.ProductId); } if (!string.IsNullOrEmpty(request.QueryString)) { request.QueryString = request.QueryString.ToLowerInvariant(); filterQuery = filterQuery.Where( s => s.Title.ToLower().Contains(request.QueryString) || s.Comment.ToLower().Contains(request.QueryString)); } var paged = await filterQuery.Select(s => new ReviewDto { Id = s.Id, Title = s.Title, Comment = s.Comment, Rating = s.Rating, ReviewStatus = s.ReviewStatus, ProductName = s.Product.Name, CreatedById = s.CreatedById, CreatedDate = s.CreatedDate }).ToPagedAsync(request.Page, request.PageSize, cancellationToken); var response = await _identityServiceClient.GetUsersByIdsAsync(new GetUsersByIdsRequest { Ids = { paged.Data.Select(s => s.CreatedById).Distinct() } }, cancellationToken : cancellationToken); paged.Data.ForEach(s => s.CreatedByUserName = response.Users.SingleOrDefault(x => x.Id == s.CreatedById)?.UserName); return(paged); }