Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/CS/HotelBooking/App_Code/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/ASP.NET/CS/HotelBooking/App_Code/SearchQuery.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DevExpress.Web.ASPxEditors;

public class SearchQuery {
    public SearchQuery() {
        Stars = new int[0];
        IsExclusive = false;
        HotelServices = new string[0];
        RoomServices = new string[0];
        IsSearch = false;
    }
    public decimal MaxPrice { get; set; }
    public decimal MinPrice { get; set; }
    public string Country { get; set; }
    public string City { get; set; }

    public DateTime? FromDate { get; set; }
    public DateTime? ToDate { get;set; }

    public int[] Stars { get; set; }
    public string[] HotelServices { get; set; }
    public string[] RoomServices { get; set; }

    public string RoomType { get; set; }
    public int Adults { get; set; }
    public int Children { get; set; }

    public bool IsExclusive { get; set; }

    public static SearchQuery MostExclusive {
        get {
            return new SearchQuery() {
                IsExclusive = true,
                Stars = new int[] { 5 }
            };
        }
    }
    public bool IsSearch { get; set; }

    static readonly Func<SearchQuery, Hotel, bool> Match_Country = (q, h) => string.IsNullOrEmpty(q.Country) || q.Country == h.Country.Name;
    static readonly Func<SearchQuery, Hotel, bool> Match_City = (q, h) => string.IsNullOrEmpty(q.City) || q.City == h.City;
    static readonly Func<SearchQuery, Hotel, bool> Match_HotelServices = (q, h) => MatchServices(q.HotelServices, h.HotelServices);
    static readonly Func<SearchQuery, Hotel, bool> Match_RoomServices = (q, h) => MatchServices(q.RoomServices, h.RoomServices);
    static readonly Func<SearchQuery, Hotel, bool> Match_Stars = (q, h) => q.Stars.Length == 0 || q.Stars.Contains(h.Stars);
    static readonly Func<SearchQuery, Hotel, bool> Match_Room = (q, h) => MatchRooms(h, q.RoomType, q.Adults, q.Children, q.MinPrice, q.MaxPrice);
    static readonly Func<SearchQuery, Hotel, bool>[] MatchExpressions = new Func<SearchQuery, Hotel, bool>[] {
        Match_Country,
        Match_City,
        Match_HotelServices,
        Match_RoomServices,
        Match_Stars,
        Match_Room
    };

    static bool MatchServices(string[] queryServices, List<string> hotelServices) {
        foreach(var qs in queryServices) {
            if(!hotelServices.Contains(qs))
                return false;
        }
        return true;
    }
    static bool MatchRooms(Hotel hotel, string type, int adults, int children, decimal minPrice, decimal maxPrice) {
        if(string.IsNullOrEmpty(type))
            return true;
        return hotel.Rooms.Any(
            r => r.Type == type &&
            r.Adults >= adults &&
            (maxPrice == 0 || r.Price <= maxPrice) &&
            (minPrice == 0 || r.Price >= minPrice)
        );
    }
    static bool MatchSearchString(string searchString, string title) {
        string[] words = searchString.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        foreach(var word in words) {
            if(title.IndexOf(word, StringComparison.InvariantCultureIgnoreCase) < 0)
                return false;
        }
        return true;
    }

    public bool IsMatch(Hotel hotel) {
        foreach(var expression in MatchExpressions)
            if(!expression(this, hotel))
                return false;
        return true;
    }

}