Ejemplo n.º 1
0
        public CollectionResult <CarServiceShortInfoDto> GetInfos(CarServiceFilter filter)
        {
            if (filter == null)
            {
                filter = new CarServiceFilter
                {
                    CurrentPage  = Common.Constants.FilterConstants.DefaultCurrentPage,
                    ItemsPerPage = Common.Constants.FilterConstants.DefaultItemsPerPage
                };
            }
            var infos = Paginate(filter.CurrentPage, filter.ItemsPerPage, BuildQueryForCarServices(filter), out var itemsCount)
                        .ToList()
                        .Select(item =>
            {
                var dto         = Mapper.Map <CarServiceShortInfoDto>(item);
                dto.AverageMark = item.Reviews.Count(r => !r.IsDeleted) == 0
                        ? 0
                        : item.Points / item.Reviews.Count(r => !r.IsDeleted);
                return(dto);
            })
                        .ToList();

            return(new CollectionResult <CarServiceShortInfoDto>
            {
                CurrentPage = filter.CurrentPage,
                Items = infos,
                ItemsCount = itemsCount,
                ItemsPerPage = filter.ItemsPerPage
            });
        }
Ejemplo n.º 2
0
        private IQueryable <CarService> BuildQueryForCarServices(CarServiceFilter filter)
        {
            var query = UnitOfWork.Repository <ICarServiceRepository>()
                        .GetAll(true)
                        .Include(q => q.Reviews)
                        .Include(q => q.City)
                        .Include(q => q.Files)
                        .Include(q => q.Reviews);

            if (filter.ShowBlocked.HasValue && filter.ShowBlocked.Value)
            {
                query = query.Where(q => q.State == CarServiceState.Active || q.State == CarServiceState.Blocked);
            }
            else
            {
                query = query.Where(q => q.State == CarServiceState.Active);
            }

            if (!string.IsNullOrEmpty(filter.Name))
            {
                query = query.Where(q => q.Name.Contains(filter.Name));
            }

            if (filter.CityId.HasValue)
            {
                query = query.Where(q => q.City.Id == filter.CityId.Value);
            }

            if (filter.Stars.HasValue)
            {
                query = query.Where(q => q.Reviews.Count(r => !r.IsDeleted) != 0 && q.Points / q.Reviews.Count(r => !r.IsDeleted) == filter.Stars.Value);
            }

            return(query.OrderByDescending(q => q.Reviews.Count(r => !r.IsDeleted) == 0
                ? 0
                : q.Points / q.Reviews.Count(r => !r.IsDeleted)));
        }
 public IHttpActionResult GetCarServices(CarServiceFilter filter)
 {
     return(CallBusinessLogicActionWithResult(() => _manager.GetInfos(filter)));
 }