public async Task<IEnumerable<VehicleDomainModel>> Search(VehicleSearchDomainModel searchCriteria) { var tenant = "Belgium-Belgium-BelgiumClient1"; var entities = await _cosmosDbConnector.Search<VehicleEntity>(tenant, searchCriteria); var vehicleDomainModel = _autoMapper.Map<IEnumerable<VehicleDomainModel>>(entities); return vehicleDomainModel; }
/// <summary> /// Prepare Where Clause based on Search Criteria /// </summary> /// <param name="partitionId">Partion Id</param> /// <param name="searchCriteria">Search Criteria</param> /// <returns>Where Clause based on Search Criteria</returns> private string PrepareWhereClause(string partitionId, VehicleSearchDomainModel searchCriteria) { var and = "and"; var or = "or "; var whereClause = $"Tenant == \"{partitionId}\""; var tempQuery = string.Empty; var makeQuery = AppendPredicate(searchCriteria.Make, "Make"); makeQuery = ReplaceLastOccurrence(makeQuery, or, string.Empty); whereClause = (!string.IsNullOrWhiteSpace(makeQuery)) ? $"{whereClause} {and} {makeQuery}" : whereClause; var modelQuery = AppendPredicate(searchCriteria.Model, "Model"); modelQuery = ReplaceLastOccurrence(modelQuery, or, string.Empty); whereClause = (!string.IsNullOrWhiteSpace(modelQuery)) ? $"{whereClause} {and} {modelQuery}" : whereClause; var bodyTypesQuery = AppendPredicate(searchCriteria.BodyTypes, "BodyType"); bodyTypesQuery = ReplaceLastOccurrence(bodyTypesQuery, or, string.Empty); whereClause = (!string.IsNullOrWhiteSpace(bodyTypesQuery)) ? $"{whereClause} {and} {bodyTypesQuery}" : whereClause; var fuelTypesQuery = AppendPredicate(searchCriteria.FuelTypes, "FuelType"); fuelTypesQuery = ReplaceLastOccurrence(fuelTypesQuery, or, string.Empty); whereClause = (!string.IsNullOrWhiteSpace(fuelTypesQuery)) ? $"{whereClause} {and} {fuelTypesQuery}" : whereClause; var gearBoxTypesQuery = AppendPredicate(searchCriteria.GearboxTypes, "GearBoxType"); gearBoxTypesQuery = ReplaceLastOccurrence(gearBoxTypesQuery, or, string.Empty); whereClause = (!string.IsNullOrWhiteSpace(gearBoxTypesQuery)) ? $"{whereClause} {and} {gearBoxTypesQuery}" : whereClause; return(whereClause); }
public async Task <ActionResult <IEnumerable <VehicleDomainModel> > > Search(VehicleSearchDomainModel searchCriteria) { var vehicles = await _vehicleSearchService.Search(searchCriteria); if (vehicles == null || !vehicles.Any()) { return(NotFound()); } return(Ok(vehicles)); }
/// <summary> /// Search For Vehicles /// </summary> /// <returns>Vehicle Details</returns> public async Task <IEnumerable <VehicleDomainModel> > Search(VehicleSearchDomainModel searchCriteria) { var response = await _vehiclesDataProvider.Search(searchCriteria); return(response); }
/// <summary> /// Search for a Vehicle /// </summary> /// <typeparam name="TResponse">Type of Response</typeparam> /// <param name="partionId">Partition Id</param> /// <param name="searchCriteria">Search Criteria</param> /// <returns>Vehicle Records Matching Search Criteria</returns> public async Task <IEnumerable <TResponse> > Search <TResponse>(string partitionId, VehicleSearchDomainModel searchCriteria) where TResponse : class, ICosmosEntity { _logger.LogInformation($"Searching For Vehicles in Partion {partitionId}"); var response = new List <TResponse>(); var collectionUri = UriFactory.CreateDocumentCollectionUri(_databaseName, _collectionName); var whereClause = PrepareWhereClause(partitionId, searchCriteria); var documentQuery = _documentClient.CreateDocumentQuery <TResponse>(collectionUri, new FeedOptions() { EnableCrossPartitionQuery = false, MaxItemCount = 100, }) //.Where("Tenant == @0 and (Make.ToLower() == @1 or Make.ToLower() == @2) ", partionId, "nissan", "peugeot") .Where(whereClause) .Select(c => c) .AsDocumentQuery(); var query = documentQuery.ToString(); _logger.LogInformation($"Document Query: {query}"); while (documentQuery.HasMoreResults) { var feedResponse = await documentQuery.ExecuteNextAsync <TResponse>(); _logger.LogInformation($"Request Charge :{feedResponse.RequestCharge}"); foreach (TResponse vehicle in feedResponse) { response.Add(vehicle); } } return(response); }