/// <summary> /// Fetches airports matching the given search string from the local database. /// Three letter searches will search for FAA codes, four letter searches will /// search for ICAO code, and all others will search for the airport name /// </summary> /// <param name="searchStr">The string to search for</param> /// <returns>The matching airports</returns> public Airports FetchMatchingAirports(string searchStr) { if (searchStr == null) { return(new Airports()); } IQuery searchQuery; switch (searchStr.Length) { case (int)AirportCodeLength.FAA: searchQuery = Query .Select(AirportNameResult) .From(DataSource.Database(UserSession.Database)) .Where(TypeProperty .EqualTo("airport") .And(FaaProperty .EqualTo(searchStr.ToUpperInvariant()))) .OrderBy(Ordering.Property("datfield").Ascending()); break; case (int)AirportCodeLength.ICAO: searchQuery = Query .Select(AirportNameResult) .From(DataSource.Database(UserSession.Database)) .Where(TypeProperty .EqualTo("airport") .And(IcaoProperty .EqualTo(searchStr.ToUpperInvariant()))); break; default: searchQuery = Query .Select(AirportNameResult) .From(DataSource.Database(UserSession.Database)) .Where(TypeProperty .EqualTo("airport") .And(AirportNameProperty .Like($"{searchStr}%"))); break; } try { using (var results = searchQuery.Run()) { return(results.Select(x => x.GetString("airportname")).Where(x => x != null).ToList()); } } finally { searchQuery.Dispose(); } }
/// <summary> /// Gets the guest bookmark document (only applicable to the guest user) /// </summary> /// <returns>The guest bookmark document</returns> public Document FetchGuestBookmarkDocument() { if (!IsGuest) { throw new InvalidOperationException("This method is only for the guest user"); } using (var searchQuery = QueryBuilder .Select(DocIdResult) .From(DataSource.Database(Database)) .Where(TypeProperty.EqualTo(Expression.String("bookmarkedhotels")))) { var results = searchQuery.Execute().ToList(); var docID = results.FirstOrDefault()?.GetString("id"); return(docID != null?Database.GetDocument(docID) : null); } }
private Task <Hotels> FetchHotelsFromLocalAsync(string description, string location) { // Description is looked up in the "description" and "name" content // Location is looking up in country, city, state, and address // Reference: https://developer.couchbase.com/documentation/server/4.6/sdk/sample-application.html // MATCH can only appear at top-level, or in a top-level AND IExpression descExp = null; if (!String.IsNullOrWhiteSpace(description)) { descExp = FullTextExpression.Index("description").Match(description); } var locationExp = CountryProperty.Like(Expression.String($"%{location}%")) .Or(CityProperty.Like(Expression.String($"%{location}%"))) .Or(StateProperty.Like(Expression.String($"%{location}%"))) .Or(AddressProperty.Like(Expression.String($"%{location}%"))); var searchExp = locationExp; if (descExp != null) { searchExp = descExp.And(locationExp); } using (var hotelSearchQuery = QueryBuilder .Select(SelectResult.All()) .From(DataSource.Database(UserSession.Database)) .Where(TypeProperty.EqualTo(Expression.String("hotel")).And(searchExp))) { var results = hotelSearchQuery.Execute().ToList(); var hotels = results.Select(x => x.GetDictionary(0).ToDictionary(y => y.Key, y => y.Value) as Hotel).ToList(); return(Task.FromResult(hotels)); } }