private static IMongoQueryable <ApartmentModel> ApplyBasicFilters(GetAllApartmentsQuery filters, IMongoQueryable <ApartmentModel> query) { if (!string.IsNullOrWhiteSpace(filters.ActivityState)) { query = query.Where(apartment => apartment.ActivityState.ToLower() == filters.ActivityState.ToLower()); } if (!string.IsNullOrEmpty(filters.ApartmentType)) { query = query.Where(apartment => apartment.ApartmentType.ToLower() == filters.ApartmentType.ToLower()); } if (!string.IsNullOrEmpty(filters.CityName)) { query = query.Where(a => a.Location.Address.CityName.ToLower() == filters.CityName.ToLower()); } if (!string.IsNullOrEmpty(filters.CountryName)) { query = query.Where(a => a.Location.Address.CountryName.ToLower() == filters.CountryName.ToLower()); } if (filters.FromPrice != null) { query = query.Where(a => a.PricePerNight >= filters.FromPrice); } if (filters.ToPrice != null) { query = query.Where(a => a.PricePerNight <= filters.ToPrice); } if (filters.FromNumberOfRooms != null) { query = query.Where(a => a.NumberOfRooms >= filters.FromNumberOfRooms); } if (filters.ToNumberOfRooms != null) { query = query.Where(a => a.NumberOfRooms <= filters.ToNumberOfRooms); } if (filters.NumberOfGuests != null) { query = query.Where(a => a.NumberOfGuests >= filters.NumberOfGuests); } if (filters.HostId != null) { query = query.Where(apartment => apartment.HostId == filters.HostId); } return(query); }
private static IQueryable <Apartment> ApplyBasicFilters(GetAllApartmentsQuery filters, IQueryable <Apartment> query) { if (!string.IsNullOrWhiteSpace(filters.ActivityState)) { query = query.Where(apartment => string.Equals(apartment.ActivityState, filters.ActivityState, StringComparison.OrdinalIgnoreCase)); } if (!string.IsNullOrEmpty(filters.ApartmentType)) { query = query.Where(apartment => string.Equals(apartment.ApartmentType, filters.ApartmentType, StringComparison.OrdinalIgnoreCase)); } if (!string.IsNullOrEmpty(filters.CityName)) { query = query.Where(a => string.Equals(a.Location.Address.CityName, filters.CityName, StringComparison.OrdinalIgnoreCase)); } if (!string.IsNullOrEmpty(filters.CountryName)) { query = query.Where(a => string.Equals(a.Location.Address.CountryName, filters.CountryName, StringComparison.OrdinalIgnoreCase)); } if (filters.FromPrice != null) { query = query.Where(a => a.PricePerNight >= filters.FromPrice); } if (filters.ToPrice != null) { query = query.Where(a => a.PricePerNight <= filters.ToPrice); } if (filters.FromNumberOfRooms != null) { query = query.Where(a => a.NumberOfRooms >= filters.FromNumberOfRooms); } if (filters.ToNumberOfRooms != null) { query = query.Where(a => a.NumberOfRooms <= filters.ToNumberOfRooms); } if (filters.NumberOfGuests != null) { query = query.Where(a => a.NumberOfGuests >= filters.NumberOfGuests); } if (filters.HostId != null) { query = query.Where(apartment => apartment.HostId == filters.HostId); } return(query); }
public async Task <IEnumerable <ApartmentDto> > Handle(GetAllApartmentsQuery request, CancellationToken cancellationToken) { var query = this.context.Apartments.AsQueryable(); query = ApplyBasicFilters(request, query); var apartments = await query.ToListAsync(cancellationToken).ConfigureAwait(false); var filtered = this.ApplyComplexFilters(request, apartments); return(filtered.Select(a => CustomMapper.Map(a)).ToList()); }
public async Task <IEnumerable <ApartmentDto> > Handle(GetAllApartmentsQuery request, CancellationToken cancellationToken) { var query = this.GetApartmentsWithIncludedRelations(); query = ApplyBasicFilters(request, query); var apartments = await query.ToListAsync(cancellationToken).ConfigureAwait(false); var tasks = apartments.Select(async item => new ApartmentDto(item) { Rating = await this.context.Comments.Where(c => !c.IsDeleted && c.ApartmentId == item.Id) .DefaultIfEmpty() .AverageAsync(c => (double)c.Rating, cancellationToken).ConfigureAwait(false), AvailableDates = await this.mediator.Send(new GetAvailableDatesQuery() { ApartmentId = item.Id }, cancellationToken).ConfigureAwait(false) }); var apartmentDtos = await Task.WhenAll(tasks).ConfigureAwait(false); return(this.ApplyComplexFilters(request, apartmentDtos)); }
private IEnumerable <ApartmentModel> ApplyComplexFilters(GetAllApartmentsQuery filter, IEnumerable <ApartmentModel> apartmentDtos) { var query = apartmentDtos; if (!string.IsNullOrWhiteSpace(filter.AmenityName)) { query = query.Where(apartment => apartment.Amenities.Any(amenity => string.Equals(amenity.Name, filter.AmenityName, StringComparison.OrdinalIgnoreCase))); } if (filter.FromDate != null && filter.ToDate != null) { var fromDate = filter.FromDate.Value; var toDate = filter.ToDate.Value; string[] daysRangeStr = DateTimeHelpers.GetDateDayRange(fromDate, toDate) .Select(d => DateTimeHelpers.FormatToYearMonthDayString(d)) .ToArray(); query = query.Where(a => !daysRangeStr .Except(a.AvailableDates.Select(d => DateTimeHelpers.FormatToYearMonthDayString(d)).ToArray()) .Any()).ToArray(); } else if (filter.FromDate != null) { var fromDate = filter.FromDate.Value; query = query.Where(a => a.AvailableDates.Any(d => !DateTimeHelpers.IsDayBefore(d, fromDate))); } else if (filter.ToDate != null) { var toDate = filter.ToDate.Value; query = query.Where(a => a.AvailableDates.Any(d => !DateTimeHelpers.IsDayAfter(d, toDate))); } return(query.ToArray()); }