public async Task <Page <Mobile> > GetMobilesList(MobilesFilter filter) { var result = _context.Mobiles .Include(a => a.Videos) .Include(a => a.Photos) .AsQueryable(); if (!string.IsNullOrEmpty(filter.Name)) { result = result.Where(i => EF.Functions.Like(i.Name, $"%{filter.Name}%")); } if (!string.IsNullOrEmpty(filter.Manufacturer)) { result = result.Where(i => EF.Functions.Like(i.Manufacturer, $"%{filter.Manufacturer}%")); } if (filter.PriceFrom != null) { result = result.Where(i => i.Price >= filter.PriceFrom); } if (filter.PriceTo != null) { result = result.Where(i => i.Price <= filter.PriceTo); } var pageCount = result.Count(); result = result.Skip((filter.Page - 1) * ItemsPerPage); result = result.Take(ItemsPerPage); return(new Page <Mobile> { Data = _mapper.Map <IEnumerable <Mobile> >(await result.ToListAsync()), PageCount = pageCount }); }
public async Task <Page <Mobile> > Handle(GetMobilesListRequest request, CancellationToken cancellationToken) { var filter = new MobilesFilter() { Manufacturer = request.Manufacturer, Name = request.Name, PriceFrom = request.PriceFrom, PriceTo = request.PriceTo, Page = request.Page }; return(await _mobilesRepository.GetMobilesList(filter)); }