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)); }
public async Task <IActionResult> Index(CarsInventoryBindingModel model) { var cookie = this.HttpContext.Request.Cookies; var sortDirectionKey = WebConstants.CookieUserNewCarsSortDirectionKey; var sortDirection = this.cookiesService.GetValueOrDefault <SortStrategyDirection>(cookie, sortDirectionKey); var sortTypeKey = WebConstants.CookieUserNewCarsSortTypeKey; var sortType = this.cookiesService.GetValueOrDefault <BaseCarSortStrategyType>(cookie, sortTypeKey); var sortStrategy = BaseCarSortStrategyFactory.GetStrategy <NewCar>(sortType, sortDirection); var priceRanges = ParameterParser.ParsePriceRange(model.PriceRange); var filterStrategies = CarFilterStrategyFactory .GetStrategies(model.Year, priceRanges[0], priceRanges[1], model.Series); var filteredCars = this.carsService.GetFiltered <NewCar>(filterStrategies.ToArray()); var sortedAndFilteredCars = sortStrategy.Sort(filteredCars); var filterMultipleStrategy = CarMultipleFilterStrategyFactory.GetStrategy(model.ModelTypes); var filteredByMultipleCars = filterMultipleStrategy.Filter(sortedAndFilteredCars); var currentPageCarViewModels = await(await this.carTestDriveService .GetCarTestDriveModelAsync <CarConciseTestDriveServiceModel>(filteredByMultipleCars, this.User, model.PageNumber)) .To <CarInventoryConciseViewModel>() .ToArrayAsync(); var key = KeyGenerator.Generate( WebConstants.CacheNewInventoryPrepend, model.ModelTypes, model.PageNumber.ToString(), model.PriceRange, model.Series, model.Year); CarsFilterViewModel filterViewModel; var cachedModel = await this.cacheService.GetOrDefaultAsync <CarsFilterViewModel>(key); if (cachedModel != null) { filterViewModel = cachedModel; } else { var filterServiceModel = await this.carsFilterTypesService .GetCarFilterModelAsync(sortedAndFilteredCars, filteredByMultipleCars); filterViewModel = Mapper.Map <CarsFilterViewModel>(filterServiceModel); } var viewModel = new CarsInventoryViewModel() { SortStrategyType = sortType, SortStrategyDirection = sortDirection, Cars = currentPageCarViewModels, CurrentPage = model.PageNumber, FilterModel = filterViewModel, TotalPagesCount = await PaginationHelper.CountTotalPagesCountAsync(filteredByMultipleCars), TotalCarsCount = await filteredByMultipleCars.CountAsync() }; FilterTypeHelper.SelectFilterTypes(filterViewModel.Years, model.Year); FilterTypeHelper.SelectFilterTypes(filterViewModel.Series, model.Series); FilterTypeHelper.SelectFilterTypes(filterViewModel.ModelTypes, model.ModelTypes.ToArray()); FilterTypeHelper.SelectFilterTypes(filterViewModel.Prices, model.PriceRange); _ = this.cacheService.AddInfinityCacheAsync(filterViewModel, key, WebConstants.CacheCarsType); return(View(viewModel)); }