public async Task <PagedResultDto <GetTourForViewDto> > GetAll(GetAllToursInput input) { try { var filteredTours = _tourRepository.GetAll().AppendTourMainFilter(input); var tours = from tour in filteredTours select new GetTourForViewDto() { Tour = new TourDto { Id = tour.Id, Name = tour.Name, Price = tour.Price, Description = tour.Description, Latitude = tour.Latitude, Longitude = tour.Longitude, LocationName = tour.LocationName, UserId = tour.UserId, Rating = (_reviewRepository.GetAll().Where(review => review.TourId == tour.Id).Count() > 0 ? (decimal?)(_reviewRepository.GetAll().Where(review => review.TourId == tour.Id).Sum(review => review.Rating) / _reviewRepository.GetAll().Where(review => review.TourId == tour.Id).Count()) : null) }, TourPictures = _tourPictureRepository.GetAll().Where(tourPicture => tourPicture.TourId == tour.Id).Select(tourPicture => new TourPictureDto { Id = tourPicture.Id, Link = tourPicture.Link, TourId = tourPicture.Id }), TourDates = _tourDateRepository.GetAll().Where(tourDate => tourDate.TourId == tour.Id).OrderByDescending(tourDate => tourDate.StartDate).Select(tourDate => new TourDateDto { Id = tourDate.Id, StartDate = tourDate.StartDate, EndDate = tourDate.EndDate, TourId = tourDate.Id }) }; var pagedAndFilteredTours = tours .OrderBy(input.Sorting ?? "Tour.Id desc") .PageBy(input); var totalCount = await tours.CountAsync(); return(new PagedResultDto <GetTourForViewDto>( totalCount, await pagedAndFilteredTours.ToListAsync() )); } catch (Exception e) { Console.WriteLine(e); } return(null); }
public async Task <FileDto> GetToursToExcel(GetAllToursInput input) { var filteredTours = _tourRepository.GetAll().AppendTourMainFilter(input); var tours = from tour in filteredTours select new GetTourForViewDto() { Tour = new TourDto { Id = tour.Id, Name = tour.Name, Price = tour.Price, Description = tour.Description, Latitude = tour.Latitude, Longitude = tour.Longitude, LocationName = tour.LocationName, UserId = tour.UserId, Rating = (_reviewRepository.GetAll().Where(review => review.TourId == tour.Id).Count() > 0 ? (decimal?)(_reviewRepository.GetAll().Where(review => review.TourId == tour.Id).Sum(review => review.Rating) / _reviewRepository.GetAll().Where(review => review.TourId == tour.Id).Count()) : null) }, TourPictures = _tourPictureRepository.GetAll().Where(tourPicture => tourPicture.TourId == tour.Id).Select(tourPicture => new TourPictureDto { Id = tourPicture.Id, Link = tourPicture.Link, TourId = tourPicture.Id }), TourDates = _tourDateRepository.GetAll().Where(tourDate => tourDate.TourId == tour.Id).OrderByDescending(tourDate => tourDate.StartDate).Select(tourDate => new TourDateDto { Id = tourDate.Id, StartDate = tourDate.StartDate, EndDate = tourDate.EndDate, TourId = tourDate.Id }) }; var result = await tours.OrderBy(input.Sorting ?? "Tour.Id asc").ToListAsync(); return(_toursExcelExporter.ExportToFile(result)); }
public static IQueryable <Tour> AppendTourMainFilter(this IQueryable <Tour> existingQuery, GetAllToursInput input) { return(existingQuery.WhereIf(!string.IsNullOrWhiteSpace(input.Name), tour => tour.Name.Contains(input.Name)) .WhereIf(!string.IsNullOrWhiteSpace(input.Description), tour => tour.Name.Contains(input.Description)) .WhereIf(!string.IsNullOrWhiteSpace(input.LocationName), tour => tour.LocationName.Contains(input.LocationName)) .WhereIf(input.MinPrice != null, tour => tour.Price >= input.MinPrice) .WhereIf(input.MaxPrice != null, tour => tour.Price <= input.MaxPrice) .WhereIf(input.UserId != null, tour => tour.UserId == input.UserId)); }