Ejemplo n.º 1
0
        public async Task <PagedList <TravelDto> > GetTravelsAsync(TravelParams travelParams, IList <string> roles)
        {
            IQueryable <Travel> query;

            if (roles.Contains("Admin"))
            {
                query = _context.Travels.AsQueryable();
            }
            else
            {
                query = _context.Travels.Where(x => x.Active).AsQueryable();
            }

            if (!String.IsNullOrEmpty(travelParams.CityName))
            {
                var travelsCities = _context.TravelCities.Where(x => x.City.Name == travelParams.CityName);

                query = query.Where(x =>
                                    travelsCities.Any(p => p.TravelId == x.Id));
            }

            if (travelParams.TravelFromDate != null || travelParams.TravelToDate != null)
            {
                query = FilterTravelByDate(query, travelParams);
            }

            return(await PagedList <TravelDto> .CreateAsync(query.ProjectTo <TravelDto>(_mapper.ConfigurationProvider).AsNoTracking(),
                                                            travelParams.PageNumber, travelParams.PageSize));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <IEnumerable <TravelDto> > > GetTravels(TravelParams travelParams)
        {
            var user = await _unitOfWork.UserRepository.GetUserByIdAsync(User.GetUserId());

            var roles = _userManager.GetRolesAsync(user).Result;

            var travels = await _unitOfWork.TravelRepository.GetTravelsAsync(travelParams, roles);

            Response.AddPaginationHeader(travels.CurrentPage, travels.PageSize, travels.TotalCount, travels.TotalPages);

            return(travels);
        }
Ejemplo n.º 3
0
        private IQueryable <Travel> FilterTravelByDate(IQueryable <Travel> query, TravelParams travelParams)
        {
            if (travelParams.TravelFromDate != null && travelParams.TravelToDate != null)
            {
                query = query.Where(x => ((x.TravelFromDate >= travelParams.TravelFromDate || x.TravelFromDate == null) &&
                                          (x.TravelToDate <= travelParams.TravelToDate ||
                                           (x.TravelToDate == null ? x.TravelFromDate <= travelParams.TravelToDate : false))) ||
                                    (x.TravelFromDate == null && x.TravelToDate == null));
            }
            else if (travelParams.TravelFromDate != null)
            {
                query = query.Where(x => x.TravelFromDate >= travelParams.TravelFromDate ||
                                    (x.TravelFromDate == null ? x.TravelToDate >= travelParams.TravelFromDate : false) ||
                                    (x.TravelFromDate == null && x.TravelToDate == null));
            }
            else if (travelParams.TravelToDate != null)
            {
                query = query.Where(x => x.TravelToDate <= travelParams.TravelToDate ||
                                    (x.TravelToDate == null ? x.TravelFromDate <= travelParams.TravelToDate : false) ||
                                    (x.TravelFromDate == null && x.TravelToDate == null));
            }

            return(query);
        }