public void WithNullInputStr_ShouldThrowException()
        {
            var minKeywordLength = 1;

            Assert.Throws <NullReferenceException>(() =>
                                                   ParameterParser.ParseSearchKeyWordsParameter(null, minKeywordLength));
        }
        public void WithEmptyInputStr_ShouldReturnEmptyCollection()
        {
            var keyInputStr      = "";
            var minKeywordLength = 1;

            var result = ParameterParser.ParseSearchKeyWordsParameter(keyInputStr, minKeywordLength);

            Assert.Empty(result);
        }
        public void WithKeyWordAndSeriesKeyWord_ShouldReturnCombineSeriesWithKeyWordBehind()
        {
            var keyInputStr      = "3 series";
            var minKeywordLength = 2;

            var result = ParameterParser.ParseSearchKeyWordsParameter(keyInputStr, minKeywordLength);

            Assert.Single(result);
            Assert.Equal(keyInputStr, result.First());
        }
        public void WithKeyWords_ShouldFilterKeyWordsByMinKeywordLength()
        {
            var keyWordWithAboveMinLength = "333";
            var keyInputStr      = $"1 22 22 {keyWordWithAboveMinLength}";
            var minKeywordLength = 3;

            var result = ParameterParser.ParseSearchKeyWordsParameter(keyInputStr, minKeywordLength);

            Assert.Single(result);
            Assert.Equal(keyWordWithAboveMinLength, result.First());
        }
예제 #5
0
        public async Task <IActionResult> Index(SearchBindingModel model)
        {
            var cookie = this.HttpContext.Request.Cookies;

            var sortDirectionKey = WebConstants.CookieUserSearchCarsSortDirectionKey;
            var sortDirection    = this.cookiesService.GetValueOrDefault <SortStrategyDirection>(cookie, sortDirectionKey);

            var sortTypeKey = WebConstants.CookieUserSearchCarsSortTypeKey;
            var sortType    = this.cookiesService.GetValueOrDefault <BaseCarSortStrategyType>(cookie, sortTypeKey);

            var sortStrategy = BaseCarSortStrategyFactory
                               .GetStrategy <BaseCar>(sortType, sortDirection);

            var splitedKeyWords = ParameterParser
                                  .ParseSearchKeyWordsParameter(model.KeyWords, WebConstants.MinSearchKeyWordLength)
                                  .Distinct()
                                  .ToArray();
            var filterStrategies = CarSearchFilterStrategyFactory.GetStrategies(splitedKeyWords);

            IEnumerable <CarInventoryConciseViewModel> carViewModels = new List <CarInventoryConciseViewModel>();
            int totalPagesCount = 0;

            if (filterStrategies.Count > 0)
            {
                var filteredCars          = this.carsService.GetFiltered <BaseCar>(filterStrategies.ToArray());
                var filteredAndSortedCars = sortStrategy.Sort(filteredCars);

                carViewModels = await(await this.carTestDriveService
                                      .GetCarTestDriveModelAsync <CarConciseTestDriveServiceModel>(filteredAndSortedCars, this.User, model.PageNumber))
                                .To <CarInventoryConciseViewModel>()
                                .ToArrayAsync();

                totalPagesCount = await PaginationHelper.CountTotalPagesCountAsync(filteredCars);
            }

            var searchModel = new CarSearchViewModel()
            {
                Cars = carViewModels,
                SortStrategyDirection = sortDirection,
                SortStrategyType      = sortType,
                CurrentPage           = model.PageNumber,
                TotalPagesCount       = totalPagesCount,
                KeyWords = splitedKeyWords
            };

            return(View(searchModel));
        }