public IActionResult Search(ProductSearchInputModel inputModel, [FromServices] IProductService productService) { var result = new ProductSearchResultViewModel { Results = productService.Search(inputModel) }; return(View("ProductSearchResults", result)); }
/// <summary> /// Initializes the specified rendering. /// </summary> /// <param name="rendering">The rendering.</param> public override void Initialize(Rendering rendering) { base.Initialize(rendering); this.ProductSearchResults = new List <ProductSearchResultViewModel>(); if (this.SearchResults == null || !this.SearchResults.Any()) { return; } foreach (var searchResult in this.SearchResults) { var productSearchResultModel = new ProductSearchResultViewModel(); productSearchResultModel.Initialize(this.Rendering, searchResult); this.ProductSearchResults.Add(productSearchResultModel); } }
public IActionResult SearchResult(string q) { var viewModel = new ProductSearchResultViewModel(); viewModel.Produkter = _dbContext.Produkter.Include(r => r.ProductCategory) .Where(r => q == null || r.Name.Contains(q) || r.ProductCategory.Namn.Contains(q)) .Select(dbProd => new ProductViewModel { Id = dbProd.Id, Namn = dbProd.Name, Pris = dbProd.Price, ProductCategory = dbProd.ProductCategory.Namn }).ToList(); return(View(viewModel)); }
public PartialViewResult GetSearchResultsAsPartialView(string country, int page = 1, int productsPerPage = 25, string categoryID = "#00000000-0000-0000-0000-000000000000", string searchText = "") { Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); var allCategories = CategoryHelper.GetAllCategoriesFromCache().ToArray(); var allCompanies = CompanyHelper.GetAllCompaniesFromCache().ToArray(); var allSubscriptions = CompanySubscriptionHelper.GetAllCompanySubscriptionsFromCache().ToArray(); var allCountries = new CountryRepository().GetAll().ToArray(); var allSubscriptionProducts = new SubscriptionProductRepository().GetAll().ToArray(); CategoryModel parentCategory = null; var cleanCategoryID = categoryID.Replace("#", string.Empty); //get parent category of sub categories if ((categoryID != null)) { parentCategory = (from category in allCategories where category.RowKey.Equals(cleanCategoryID) select category).SingleOrDefault();//will return null for all cateogries } //check if root category was selected if (parentCategory == null) { parentCategory = CategoryHelper.AllCategories; } //var childCategoryArray = new CategoryModel[0]; var childCategoryArray = new List <CategoryModel>().ToArray(); if (parentCategory != CategoryHelper.AllCategories) { childCategoryArray = (from child in allCategories where child.ParentID == cleanCategoryID select child).ToArray(); } else { childCategoryArray = (from child in allCategories where child.ParentID == "0" select child).ToArray(); } var categoryAndDescendantsList = CategoryHelper.GetAllDescendantsFlattened(cleanCategoryID, allCategories, childCategoryArray).ToList(); categoryAndDescendantsList.Add(parentCategory); var categoryAndDescendantsArray = categoryAndDescendantsList.ToArray(); int resultsTotal; int productsDatabaseTotal; var results = ProductHelper.ProductSearch(categoryAndDescendantsArray, searchText, country, page, productsPerPage, allCompanies.ToArray(), null, out resultsTotal, out productsDatabaseTotal); List <ProductSearchResultModel> productSearchResults = new List <ProductSearchResultModel>(); results.ToList().ForEach(productModel =>//TODO: Search on country criteria TODO: Cache company details for this loop and avoid getting all at start { var company = allCompanies.Where(f => f.RowKey == (productModel.CompanyID)).SingleOrDefault(); if (company != null)//if company details have not been supplied - exclude products from the search results { //get countryname and flagurl string countryName = "Unknown"; string countryCode = "00"; if (company.Country != null) { var countryModel = allCountries.Where(c => c.Name.ToLower().Trim() == company.Country.ToLower().Trim()).SingleOrDefault(); if (countryModel != null) { countryCode = countryModel.Iso2DigitCode; countryName = countryModel.Name; } //country found so set name and code } //get subscription status int companyLevel = 0; var companySubscription = allSubscriptions.Where(s => s.CompanyRowKey == company.RowKey).SingleOrDefault(); if (companySubscription != null) { if (!(companySubscription.StartDateTime.AddYears(1) >= DateTime.UtcNow)) { companySubscription = null;//subscription is expired so make null } else { var companySubscriptionProduct = allSubscriptionProducts.Where(sp => sp.ID == companySubscription.ProductID).SingleOrDefault(); if (companySubscriptionProduct != null) { companyLevel = companySubscriptionProduct.Level; } } } var isVerified = company.IsVerified; var isChamberCertified = company.IsChamberCertified; var isGreenCertified = company.IsGreenCertified; productSearchResults.Add(new ProductSearchResultModel { CompanyName = company.Name, Product = productModel, BusinessType = company.BusinessType, IsChamberCertified = isChamberCertified, IsGreenCertified = isGreenCertified, IsVerified = isVerified, Country = countryName, FlagUrl = Settings.Default.FlagUrlPath.ToLower().Trim().Replace("%countrycode%", countryCode).ToLower(), CompanySubscriptionLevel = companyLevel, CompanyID = company.RowKey }); } }); if (productSearchResults.Count() == 0)//set current page to zero if there are no results { page = 0; } var totalPages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(resultsTotal) / Convert.ToDouble(productsPerPage))); var searchResultViewModel = new ProductSearchResultViewModel(productSearchResults.ToArray(), productsPerPage, page, parentCategory, totalPages, resultsTotal); stopWatch.Stop(); if (Settings.Default.IsDiagnosticsModeEnabled.ToLower() == "true") { searchResultViewModel.SearchDiagnosticInformation = "[" + DateTime.UtcNow.ToLongTimeString() + " UTC Searched " + productsDatabaseTotal.ToString() + " Products in " + stopWatch.ElapsedMilliseconds + "ms]"; } return(PartialView(searchResultViewModel)); }