public CollectionResult <ApplicationShortInfoForServiceDto> GetApplicationsFromPool(ApplicationsPoolFilter filter, string currentUserId)
        {
            UserManager.IsUserInCarServiceRole(currentUserId);
            var service = UnitOfWork.Repository <ICarServiceRepository>().GetByUserId(currentUserId);

            if (filter == null)
            {
                filter = new ApplicationsPoolFilter
                {
                    CurrentPage  = Common.Constants.FilterConstants.DefaultCurrentPage,
                    ItemsPerPage = Common.Constants.FilterConstants.DefaultItemsPerPage
                };
            }

            var result = Paginate(filter.CurrentPage, filter.ItemsPerPage, BuildQueryForPool(filter, service), out var itemsCount)
                         .ToList()
                         .Select(item =>
            {
                var dto         = Mapper.Map <ApplicationShortInfoForServiceDto>(item);
                dto.OfferId     = item.Offers.FirstOrDefault(of => !of.IsDeleted && of.Service.ApplicationUser.Id == currentUserId)?.Id;
                dto.WorkClasses = item.WorkTypes
                                  .GroupBy(y => y.Class.Name)
                                  .Select(y => new WorkClassDto
                {
                    Name  = y.Key,
                    Types = Mapper.Map <IEnumerable <WorkTypeDto> >(y.ToList())
                })
                                  .ToList();
                return(dto);
            });

            return(new CollectionResult <ApplicationShortInfoForServiceDto>
            {
                ItemsCount = itemsCount,
                CurrentPage = filter.CurrentPage,
                ItemsPerPage = filter.ItemsPerPage,
                Items = result
            });
        }
        private IQueryable <Application> BuildQueryForPool(ApplicationsPoolFilter filter, CarService service)
        {
            if (service.State == CarServiceState.Blocked || service.State == CarServiceState.Rejected)
            {
                return(new List <Application>().AsQueryable());
            }

            var query = UnitOfWork.Repository <IApplicationRepository>()
                        .GetAll(true)
                        .Include(app => app.WorkTypes)
                        .Include(app => app.City)
                        .Include(app => app.Car)
                        .Include(app => app.Car.Model)
                        .Include(app => app.Car.Model.Mark)
                        .Where(app => !app.IsDeleted && app.State == ApplicationState.InSearch);

            query = query.Where(app => app.City.Id == service.City.Id);

            var workTypesId = service.WorkTypes.Select(s => s.Id).ToList();

            query = query.Where(app => app.WorkTypes.Any() && app.WorkTypes.All(p => workTypesId.Contains(p.Id)));

            if (filter.CreatedFrom.HasValue)
            {
                query = query.Where(app => app.Created >= filter.CreatedFrom.Value);
            }

            if (filter.CreatedTo.HasValue)
            {
                query = query.Where(app => app.Created <= filter.CreatedTo.Value);
            }

            if (filter.MarkId.HasValue)
            {
                query = query.Where(app => app.Car.Model.Mark.Id == filter.MarkId.Value);
            }

            return(query.OrderByDescending(app => app.Created));
        }
 public IHttpActionResult GetPool(ApplicationsPoolFilter filter)
 {
     return(CallBusinessLogicActionWithResult(() => _manager.GetApplicationsFromPool(filter, User.Identity.GetUserId())));
 }