Ejemplo n.º 1
0
        public async Task <IQueryable <DiscountStatisticsDto> > GetStatisticsAsync(
            ODataQueryOptions <DiscountStatisticsDto> options,
            string searchText, DateTime startDate, DateTime endDate)
        {
            var discounts = await _searchService.SearchAsync(searchText);

            var discountsDto = _mapper.Map <IEnumerable <DiscountStatisticsDto> >(discounts);

            var discountViewsAmounts = await _statisticsRepository.GetInWhereAsync(
                s => s.DiscountId, discounts.Select(d => d.Id), startDate, endDate);

            var discountsQueryable = discountsDto.GroupJoin(
                discountViewsAmounts,
                d => d.Id,
                s => s.DiscountId,
                (d, statistics) =>
            {
                d.ViewsAmount = statistics.Sum(s => s.ViewsAmount);
                return(d);
            }).AsQueryable();

            if (options.Filter != null)
            {
                return(options.ApplyTo(discountsQueryable) as IQueryable <DiscountStatisticsDto>);
            }

            return(discountsQueryable);
        }