示例#1
0
        public async Task <ActionResult <IEnumerable <Hotel> > > SearchHotels([FromBody] HotelSearchCriteria criteria)
        {
            var data = await _hotelsRepository.getHotels();

            if (HotelSearchCriteria.HasCriteria(criteria))
            {
                Func <HotelDto, bool> nameCriteria = e => string.IsNullOrWhiteSpace(criteria.Name) ||
                                                     (!string.IsNullOrWhiteSpace(e.name) && e.name.ToLower().Contains(criteria.Name.ToLower()));
                Func <HotelDto, bool> ratingCriteria = e => !criteria.RatingFilter.HasValue || (criteria.RatingFilter.Value == e.rating);
                data = data.Where(r => nameCriteria(r) && ratingCriteria(r));
            }

            var hotels = data.Select(r => this._mapper.Map <Hotel>(r)).ToList();

            return(Ok(hotels));
        }
示例#2
0
        public async Task <IEnumerable <HotelDto> > getHotelsByCriteria(HotelSearchCriteria criteria)
        {
            if (HotelSearchCriteria.HasCriteria(criteria))
            {
                IEnumerable <HotelDto> data = await LoadHotels();

                if (!string.IsNullOrWhiteSpace(criteria.Name))
                {
                    data = data.Where(r => !string.IsNullOrWhiteSpace(r.name) && r.name.ToLower().Contains(criteria.Name.ToLower()));
                }

                if (criteria.RatingFilter.HasValue)
                {
                    data = data.Where(r => r.rating == criteria.RatingFilter);
                }

                return(await Task.FromResult(data));
            }
            else
            {
                return(await LoadHotels());
            }
        }