Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="lstHotel"></param>
        /// <param name="hotelName"></param>
        /// <param name="destination"></param>
        /// <param name="rangeFrom"></param>
        /// <param name="rangeTo"></param>
        /// <param name="dateFrom"></param>
        /// <param name="dateTo"></param>
        /// <param name="sortByName"></param>
        /// <param name="sortByPrice"></param>
        /// <returns></returns>
        private List <Hotel> Search(IQueryable <Hotel> queryableHotels
                                    , String hotelName
                                    , String destination
                                    , Double?rangeFrom
                                    , Double?rangeTo
                                    , DateTime?dateFrom
                                    , DateTime?dateTo
                                    , String sortByName
                                    , String sortByPrice)
        {
            try
            {
                // if nullable values are not provided then it will pass in first OR clause
                // else it will match data in second clause
                queryableHotels = queryableHotels.Where(h => (String.IsNullOrEmpty(hotelName) || h.Name.Contains(hotelName, StringComparison.OrdinalIgnoreCase)) &&
                                                        (String.IsNullOrEmpty(destination) || h.City.Contains(destination, StringComparison.OrdinalIgnoreCase)) &&
                                                        (rangeFrom == null || h.Price >= rangeFrom) &&
                                                        (rangeTo == null || h.Price <= rangeTo) &&
                                                        (h.Availability.Any(a => (dateFrom == null || a.fromDate <= dateFrom) && (dateTo == null || a.toDate >= dateTo))));



                // multiple combinations of ordering are possible
                CommonEnums.HotelSearchOrders hotelSearchOrders = DecideSearchOrder(sortByName, sortByPrice);
                queryableHotels = ApplySort(queryableHotels, hotelSearchOrders);
            }
            catch (CustomException ex)
            {
                // only add message if it is crashed as of query disorder
                if (ex.Messages.IsNull())
                {
                    ex.Messages = new List <CustomMessage>();
                    ex.Messages.Add(new CustomMessage {
                        Code = Constants.CONST_EXCEPTION_QUERY_ERROR, Message = Constants.CONST_EXCEPTION_QUERY_ERROR_DESCRIPTION
                    });
                }

                throw ex;
            }
            catch (Exception ex)
            {
                CustomException cEx = new CustomException(new List <CustomMessage> {
                    new CustomMessage {
                        Code = Constants.CONST_EXCEPTION_QUERY_ERROR, Message = Constants.CONST_EXCEPTION_QUERY_ERROR_DESCRIPTION
                    }
                }
                                                          , ex.Message
                                                          , ex.InnerException);

                throw cEx;
            }

            return(queryableHotels.ToList());
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="queryableHotels"></param>
        /// <param name="hotelSearchOrders"></param>
        /// <returns></returns>
        private IQueryable <Hotel> ApplySort(IQueryable <Hotel> queryableHotels, CommonEnums.HotelSearchOrders hotelSearchOrders)
        {
            try
            {
                // apply sorting cases
                switch (hotelSearchOrders)
                {
                // ascending by name
                case CommonEnums.HotelSearchOrders.AscendingByName:
                {
                    queryableHotels = queryableHotels.OrderBy(h2 => h2.Name);
                    break;
                }

                // ascending by name and ascending by price
                case CommonEnums.HotelSearchOrders.AscendingByNameAndAscendingByPrice:
                {
                    queryableHotels = queryableHotels.OrderBy(h2 => h2.Name).ThenBy(h3 => h3.Price);
                    break;
                }

                // ascending by name and descending by price
                case CommonEnums.HotelSearchOrders.AscendingByNameAndDescendingByPrice:
                {
                    queryableHotels = queryableHotels.OrderBy(h2 => h2.Name).ThenByDescending(h3 => h3.Price);
                    break;
                }

                // ascending by price
                case CommonEnums.HotelSearchOrders.AscendingByPrice:
                {
                    queryableHotels = queryableHotels.OrderBy(h2 => h2.Price);
                    break;
                }

                // descending by name and ascneding by price
                case CommonEnums.HotelSearchOrders.DescendingByNameAndAscendingByPrice:
                {
                    queryableHotels = queryableHotels.OrderByDescending(h2 => h2.Name).ThenBy(h3 => h3.Price);
                    break;
                }

                // descending by price
                case CommonEnums.HotelSearchOrders.DescendingByPrice:
                {
                    queryableHotels = queryableHotels.OrderByDescending(h2 => h2.Price);
                    break;
                }

                // descending by name
                case CommonEnums.HotelSearchOrders.DescendingByName:
                {
                    queryableHotels = queryableHotels.OrderByDescending(h2 => h2.Name);
                    break;
                }

                // descending by name and descending by price
                case CommonEnums.HotelSearchOrders.DescendingByNameAndDescendingByPrice:
                {
                    queryableHotels = queryableHotels.OrderByDescending(h2 => h2.Name).ThenByDescending(h3 => h3.Price);
                    break;
                }
                }
            }
            catch (CustomException ex)
            {
                // detail code for whole messages trail
                if (ex.Messages.IsNull())
                {
                    ex.Messages = new List <CustomMessage>();
                }
                ex.Messages.Add(new CustomMessage {
                    Code = Constants.CONST_EXCEPTION_SORT_NOT_DEFINED, Message = Constants.CONST_EXCEPTION_SORT_NOT_DEFINED_DESCRIPTION
                });

                throw ex;
            }
            catch (Exception ex)
            {
                CustomException cEx = new CustomException(new List <CustomMessage> {
                    new CustomMessage {
                        Code = Constants.CONST_EXCEPTION_SORT_NOT_DEFINED, Message = Constants.CONST_EXCEPTION_SORT_NOT_DEFINED_DESCRIPTION
                    }
                }
                                                          , ex.Message
                                                          , ex.InnerException);

                throw cEx;
            }

            return(queryableHotels);
        }