public virtual async Task <IHttpActionResult> AutoComplete(AutoCompleteSearchViewModel request, int limit = MAXIMUM_AUTOCOMPLETE_RESULT) { var originalSearchTerms = request.Query.Trim(); var searchTerms = SearchTermsTransformationProvider.TransformSearchTerm(originalSearchTerms, ComposerContext.CultureInfo.Name); var searchCriteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(searchTerms, RequestUtils.GetBaseUrl(Request).ToString(), false).ConfigureAwait(false); searchCriteria.NumberOfItemsPerPage = limit; var searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false); if (searchResultsViewModel.ProductSearchResults?.TotalCount == 0 && originalSearchTerms != searchTerms) { searchCriteria.Keywords = originalSearchTerms; searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false); } var vm = new AutoCompleteViewModel() { Suggestions = new List <ProductSearchViewModel>() }; if (searchResultsViewModel.ProductSearchResults?.SearchResults?.Count > 0) { vm.Suggestions = searchResultsViewModel.ProductSearchResults.SearchResults.Take(limit) .Select(p => { p.SearchTerm = searchTerms; return(p); }) .ToList(); } return(Ok(vm)); }
public async Task WHEN_one_selected_facet_SHOULD_facets_count_should_be_1() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); var facetName = GetRandom.String(5); SetupFacets(new FacetSetting(facetName)); // Act SearchViewModel model = await service.GetSearchViewModelAsync(new SearchCriteria { Keywords = "any", CultureInfo = new CultureInfo(CultureName), Scope = Scope, SelectedFacets = { new Composer.Parameters.SearchFilter { Name = facetName } } }); // Assert model.SelectedFacets.Facets.Count.Should().Be(1); }
public void WHEN_param_is_null_SHOULD_throw_argument_null_exception() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act & Assert Assert.ThrowsAsync <ArgumentNullException>(() => service.GetSearchViewModelAsync(null)); }
public void WHEN_scope_is_empty_SHOULD_throw_argument_exception() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act & Assert Assert.ThrowsAsync <ArgumentException>(() => service.GetSearchViewModelAsync( new SearchCriteria { Keywords = "any", CultureInfo = new CultureInfo(CultureName), Scope = string.Empty } )); }
public void WHEN_culture_info_is_null_SHOULD_throw_argument_exception() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act & Assert Assert.ThrowsAsync <ArgumentException>(() => service.GetSearchViewModelAsync( new SearchCriteria { Keywords = "any", CultureInfo = null, Scope = "global" } )); }
public virtual async Task <IHttpActionResult> GetFacets(GetFacetsRequest request) { var queryString = HttpUtility.ParseQueryString(request.QueryString); var searchCriteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(queryString["keywords"], RequestUtils.GetBaseUrl(Request).ToString(), true).ConfigureAwait(false); searchCriteria.NumberOfItemsPerPage = 0; searchCriteria.SelectedFacets.AddRange(SearchUrlProvider.BuildSelectedFacets(queryString)); var searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false); searchResultsViewModel.ProductSearchResults.Facets = searchResultsViewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix)).ToList(); return(Ok(searchResultsViewModel)); }
public async Task WHEN_keywords_is_not_null_or_whitespace_SHOULD_returned_model_contain_keywords() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act SearchViewModel model = await service.GetSearchViewModelAsync(new SearchCriteria { Keywords = "any", CultureInfo = new CultureInfo(CultureName), Scope = Scope }); // Assert model.Keywords.Should().Be("any"); }
public async Task WHEN_keywords_is_empty_SHOULD_return_no_product() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act var model = await service.GetSearchViewModelAsync(new SearchCriteria { Keywords = string.Empty, CultureInfo = new CultureInfo(CultureName), Scope = Scope }); //Assert model.ProductSearchResults.TotalCount.Should().Be(0); }
public async Task WHEN_no_selected_facet_SHOULD_returned_model_contain_no_facet() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act SearchViewModel model = await service.GetSearchViewModelAsync(new SearchCriteria { Keywords = "any", CultureInfo = new CultureInfo(CultureName), Scope = Scope, SelectedFacets = { } }); // Assert model.SelectedFacets.Facets.Count.Should().Be(0); }
public SearchRequestContext( ISearchViewService searchViewService, ISearchUrlProvider searchUrlProvider, HttpRequestBase request, IBaseSearchCriteriaProvider baseSearchCriteriaProvider) { SearchViewService = searchViewService ?? throw new ArgumentNullException(nameof(searchViewService)); SearchUrlProvider = searchUrlProvider ?? throw new ArgumentNullException(nameof(searchUrlProvider)); Request = request; BaseSearchCriteriaProvider = baseSearchCriteriaProvider ?? throw new ArgumentNullException(nameof(baseSearchCriteriaProvider)); _viewModel = new Lazy <SearchViewModel>(() => { var criteria = BuildProductsSearchCriteria(); return(SearchViewService.GetSearchViewModelAsync(criteria).Result); }); }
public SearchRequestContext(IComposerContext composerContext, ISearchViewService searchViewService, IInventoryLocationProvider inventoryLocationProvider, ISearchUrlProvider searchUrlProvider, HttpRequestBase request) { ComposerContext = composerContext ?? throw new ArgumentNullException(nameof(composerContext)); SearchViewService = searchViewService ?? throw new ArgumentNullException(nameof(searchViewService)); InventoryLocationProvider = inventoryLocationProvider ?? throw new ArgumentNullException(nameof(inventoryLocationProvider)); SearchUrlProvider = searchUrlProvider ?? throw new ArgumentNullException(nameof(searchUrlProvider)); Request = request; _viewModel = new Lazy <SearchViewModel>(() => { var criteria = BuildProductsSearchCriteria(); return(SearchViewService.GetSearchViewModelAsync(criteria).Result); }); }
public virtual async Task <IHttpActionResult> GetSearchResults(GetSearchResultsRequest request) { var queryString = HttpUtility.ParseQueryString(request.QueryString ?? ""); var SelectedFacets = SearchUrlProvider.BuildSelectedFacets(queryString).ToList(); var CurrentPage = int.TryParse(queryString[SearchRequestParams.Page], out int page) && page > 0 ? page : 1; var SortDirection = queryString[SearchRequestParams.SortDirection] ?? SearchRequestParams.DefaultSortDirection; var SortBy = queryString[SearchRequestParams.SortBy] ?? SearchRequestParams.DefaultSortBy; var BaseUrl = RequestUtils.GetBaseUrl(Request).ToString(); var Keywords = queryString[SearchRequestParams.Keywords]; BaseSearchViewModel viewModel; if (!string.IsNullOrEmpty(request.CategoryId)) { var param = new GetCategoryBrowsingViewModelParam { CategoryId = request.CategoryId, CategoryName = string.Empty, BaseUrl = BaseUrl, IsAllProducts = false, NumberOfItemsPerPage = SearchConfiguration.MaxItemsPerPage, Page = CurrentPage, SortBy = SortBy, SortDirection = SortDirection, InventoryLocationIds = await InventoryLocationProvider.GetInventoryLocationIdsForSearchAsync().ConfigureAwait(false), SelectedFacets = SelectedFacets, CultureInfo = ComposerContext.CultureInfo, }; viewModel = await CategoryBrowsingViewService.GetCategoryBrowsingViewModelAsync(param).ConfigureAwait(false); } else { var searchCriteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(Keywords, BaseUrl, true, CurrentPage).ConfigureAwait(false); searchCriteria.SortBy = SortBy; searchCriteria.SortDirection = SortDirection; searchCriteria.SelectedFacets.AddRange(SelectedFacets); viewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false); } viewModel.ProductSearchResults.Facets = viewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix)).ToList(); return(Ok(viewModel)); }
public virtual async Task <IHttpActionResult> AutoComplete(AutoCompleteSearchViewModel request, int limit = MAXIMUM_AUTOCOMPLETE_RESULT) { var originalSearchTerms = request.Query.Trim(); var searchTerms = SearchTermsTransformationProvider.TransformSearchTerm(originalSearchTerms, ComposerContext.CultureInfo.Name);; var searchCriteria = new SearchCriteria { Keywords = searchTerms, NumberOfItemsPerPage = limit, IncludeFacets = false, StartingIndex = 0, SortBy = "score", SortDirection = "desc", Page = 1, BaseUrl = RequestUtils.GetBaseUrl(Request).ToString(), Scope = ComposerContext.Scope, CultureInfo = ComposerContext.CultureInfo, InventoryLocationIds = await InventoryLocationProvider.GetInventoryLocationIdsForSearchAsync().ConfigureAwait(false), }; var searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false); if (searchResultsViewModel.ProductSearchResults?.TotalCount == 0 && originalSearchTerms != searchTerms) { searchCriteria.Keywords = originalSearchTerms; searchResultsViewModel = await SearchViewService.GetSearchViewModelAsync(searchCriteria).ConfigureAwait(false); } var vm = new AutoCompleteViewModel() { Suggestions = new List <ProductSearchViewModel>() }; if (searchResultsViewModel.ProductSearchResults?.SearchResults?.Count > 0) { vm.Suggestions = searchResultsViewModel.ProductSearchResults.SearchResults.Take(limit) .Select(p => { p.SearchTerm = searchTerms; return(p); }) .ToList(); } return(Ok(vm)); }
public async Task WHEN_one_selected_facet_SHOULD_facets_are_not_all_removable() { // Arrange SearchViewService service = _container.CreateInstance <SearchViewService>(); // Act SearchViewModel model = await service.GetSearchViewModelAsync(new SearchCriteria { Keywords = "any", CultureInfo = new CultureInfo(CultureName), Scope = Scope, SelectedFacets = { new Composer.Parameters.SearchFilter() } }); // Assert model.SelectedFacets.IsAllRemovable.Should().BeFalse(); }
public virtual async Task <IHttpActionResult> GetSearchResultsBySkus(GetSearchResultsBySkusRequest request) { if (request.Skus == null) { return(BadRequest($"{nameof(request.Skus)} cannot be empty")); } var queryString = HttpUtility.ParseQueryString(request.QueryString ?? ""); var SelectedFacets = SearchUrlProvider.BuildSelectedFacets(queryString).ToList(); var Keywords = queryString[SearchRequestParams.Keywords]; var BaseUrl = RequestUtils.GetBaseUrl(Request).ToString(); var IncludeFactes = request.IncludeFacets; var searchCriteria = await BaseSearchCriteriaProvider.GetSearchCriteriaAsync(Keywords, BaseUrl, IncludeFactes).ConfigureAwait(false); var searchBySkusCriteria = new SearchBySkusCriteria { Skus = request.Skus, Keywords = searchCriteria.Keywords, NumberOfItemsPerPage = request.Skus.Length, StartingIndex = searchCriteria.StartingIndex, Page = searchCriteria.Page, BaseUrl = searchCriteria.BaseUrl, Scope = searchCriteria.Scope, CultureInfo = searchCriteria.CultureInfo, InventoryLocationIds = searchCriteria.InventoryLocationIds, AvailabilityDate = searchCriteria.AvailabilityDate, IncludeFacets = searchCriteria.IncludeFacets }; searchBySkusCriteria.SelectedFacets.AddRange(SelectedFacets); var viewModel = await SearchViewService.GetSearchViewModelAsync(searchBySkusCriteria).ConfigureAwait(false); if (IncludeFactes) { viewModel.ProductSearchResults.Facets = viewModel.ProductSearchResults.Facets.Where(f => !f.FieldName.StartsWith(SearchConfiguration.CategoryFacetFiledNamePrefix)).ToList(); } return(Ok(viewModel)); }