From 2f71db7f0a8b636c49af2f830d3bfa2d92a0f73f Mon Sep 17 00:00:00 2001 From: Zackery Anderson Date: Tue, 5 Sep 2023 16:52:45 -0600 Subject: [PATCH 1/7] Trying to break dependencies --- .../Controllers/SearchController.cs | 48 +++++++++++++------ .../Models/Search/SearchResultModel.cs | 10 ++-- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/Smartstore.Web/Controllers/SearchController.cs b/src/Smartstore.Web/Controllers/SearchController.cs index 23a2f5c4b5..bafe1c9986 100644 --- a/src/Smartstore.Web/Controllers/SearchController.cs +++ b/src/Smartstore.Web/Controllers/SearchController.cs @@ -20,6 +20,7 @@ public class SearchController : PublicController private readonly CatalogSettings _catalogSettings; private readonly Lazy _productService; private readonly ProductUrlHelper _productUrlHelper; + private SearchResultModel _searchResultModel; public SearchController( CatalogHelper catalogHelper, @@ -29,7 +30,9 @@ public SearchController( SearchSettings searchSettings, CatalogSettings catalogSettings, Lazy productService, - ProductUrlHelper productUrlHelper) + ProductUrlHelper productUrlHelper + //SearchResultModel search + ) { _catalogHelper = catalogHelper; _catalogSearchService = catalogSearchService; @@ -39,6 +42,15 @@ public SearchController( _catalogSettings = catalogSettings; _productService = productService; _productUrlHelper = productUrlHelper; + + //if(search == null) + //{ + //} + //else + //{ + // _searchResultModel = search; + //} + _searchResultModel = new SearchResultModel(); } [HttpPost] @@ -59,8 +71,9 @@ public async Task InstantSearch(CatalogSearchQuery query) var result = await _catalogSearchService.SearchAsync(query); - var model = new SearchResultModel(query) + var model = new SearchResultModel() { + Query = query, SearchResult = result, Term = term, TotalProductsCount = result.TotalHitsCount @@ -99,23 +112,29 @@ await _localizedEntityService.PrefetchLocalizedPropertiesAsync( public async Task Search(CatalogSearchQuery query) { CatalogSearchResult result = null; - var model = new SearchResultModel(query); + if(_searchResultModel == null) + { + _searchResultModel = new(); + } + _searchResultModel.Query = query; var term = query?.DefaultTerm; if (term == null || term.Length < _searchSettings.InstantSearchTermMinLength) { - model.SearchResult = new CatalogSearchResult(query); - model.TopProducts = ProductSummaryModel.Empty; - model.Error = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength); - return View(model); + _searchResultModel.SearchResult = new CatalogSearchResult(query); + _searchResultModel.TopProducts = ProductSummaryModel.Empty; + _searchResultModel.Error = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength); + return View(_searchResultModel); } + //break into it's own method customer check var customer = Services.WorkContext.CurrentCustomer; if (!customer.IsSystemAccount) { customer.GenericAttributes.LastContinueShoppingPage = HttpContext.Request.RawUrl(); } + //break into it's own method search settings try { if (_searchSettings.SearchProductByIdentificationNumber) @@ -139,10 +158,11 @@ await product.GetActiveSlugAsync(), } catch (Exception ex) { - model.Error = ex.ToString(); + _searchResultModel.Error = ex.ToString(); result = new CatalogSearchResult(query); } + //break into it's own method spell check suggestion if (result.TotalHitsCount == 0 && result.SpellCheckerSuggestions.Length > 0) { // No matches, but spell checker made a suggestion. @@ -154,7 +174,7 @@ await product.GetActiveSlugAsync(), if (result.TotalHitsCount > 0) { - model.AttemptedTerm = term; + _searchResultModel.AttemptedTerm = term; // Restore the original suggestions. result.SpellCheckerSuggestions = oldSuggestions.Where(x => x != query.DefaultTerm).ToArray(); } @@ -164,9 +184,9 @@ await product.GetActiveSlugAsync(), } } - model.SearchResult = result; - model.Term = query.DefaultTerm; - model.TotalProductsCount = result.TotalHitsCount; + _searchResultModel.SearchResult = result; + _searchResultModel.Term = query.DefaultTerm; + _searchResultModel.TotalProductsCount = result.TotalHitsCount; var productSummaryViewMode = query.CustomData.Get("ViewMode") is string viewMode && viewMode.EqualsNoCase("list") ? ProductSummaryViewMode.List @@ -179,9 +199,9 @@ await product.GetActiveSlugAsync(), _catalogHelper.MapListActions(summaryModel, null, _catalogSettings.DefaultPageSizeOptions); // Add product hits. - model.TopProducts = summaryModel; + _searchResultModel.TopProducts = summaryModel; - return View(model); + return View(_searchResultModel); } } } diff --git a/src/Smartstore.Web/Models/Search/SearchResultModel.cs b/src/Smartstore.Web/Models/Search/SearchResultModel.cs index b70284e842..82024d4613 100644 --- a/src/Smartstore.Web/Models/Search/SearchResultModel.cs +++ b/src/Smartstore.Web/Models/Search/SearchResultModel.cs @@ -5,12 +5,12 @@ namespace Smartstore.Web.Models.Search { public class SearchResultModel : SearchResultModelBase, ISearchResultModel { - public SearchResultModel(CatalogSearchQuery query) - { - Query = query; - } + //public SearchResultModel(CatalogSearchQuery query) + //{ + // Query = query; + //} - public CatalogSearchQuery Query { get; } + public CatalogSearchQuery Query { get; set; } public CatalogSearchResult SearchResult { get; set; } From d8354a1c66ecf54169644f53e42589d8afe4f620 Mon Sep 17 00:00:00 2001 From: Zackery Anderson Date: Thu, 7 Sep 2023 18:03:24 -0600 Subject: [PATCH 2/7] Got a working test for the search --- .../Controllers/SearchController.cs | 67 +++++++++---------- .../Models/Search/SearchResultModel.cs | 10 +-- .../Controllers/SearchControllerTests.cs | 37 ++++++++++ 3 files changed, 75 insertions(+), 39 deletions(-) create mode 100644 test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs diff --git a/src/Smartstore.Web/Controllers/SearchController.cs b/src/Smartstore.Web/Controllers/SearchController.cs index bafe1c9986..f739aded4f 100644 --- a/src/Smartstore.Web/Controllers/SearchController.cs +++ b/src/Smartstore.Web/Controllers/SearchController.cs @@ -20,7 +20,7 @@ public class SearchController : PublicController private readonly CatalogSettings _catalogSettings; private readonly Lazy _productService; private readonly ProductUrlHelper _productUrlHelper; - private SearchResultModel _searchResultModel; + //private SearchResultModel _searchResultModel; public SearchController( CatalogHelper catalogHelper, @@ -50,7 +50,7 @@ ProductUrlHelper productUrlHelper //{ // _searchResultModel = search; //} - _searchResultModel = new SearchResultModel(); + //_searchResultModel = new SearchResultModel(); } [HttpPost] @@ -71,9 +71,8 @@ public async Task InstantSearch(CatalogSearchQuery query) var result = await _catalogSearchService.SearchAsync(query); - var model = new SearchResultModel() + var model = new SearchResultModel(query) { - Query = query, SearchResult = result, Term = term, TotalProductsCount = result.TotalHitsCount @@ -110,21 +109,22 @@ await _localizedEntityService.PrefetchLocalizedPropertiesAsync( [LocalizedRoute("/search", Name = "Search")] public async Task Search(CatalogSearchQuery query) + { + return View(await GetSearchResultModel(query)); + } + + public async Task GetSearchResultModel(CatalogSearchQuery query) { CatalogSearchResult result = null; - if(_searchResultModel == null) - { - _searchResultModel = new(); - } - _searchResultModel.Query = query; + var model = new SearchResultModel(query); var term = query?.DefaultTerm; if (term == null || term.Length < _searchSettings.InstantSearchTermMinLength) { - _searchResultModel.SearchResult = new CatalogSearchResult(query); - _searchResultModel.TopProducts = ProductSummaryModel.Empty; - _searchResultModel.Error = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength); - return View(_searchResultModel); + model.SearchResult = new CatalogSearchResult(query); + model.TopProducts = ProductSummaryModel.Empty; + model.Error = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength); + return model; } //break into it's own method customer check @@ -139,26 +139,26 @@ public async Task Search(CatalogSearchQuery query) { if (_searchSettings.SearchProductByIdentificationNumber) { - var (product, attributeCombination) = await _productService.Value.GetProductByCodeAsync(term); - if (product != null) - { - if (attributeCombination != null) - { - return Redirect(await _productUrlHelper.GetProductPathAsync( - product.Id, - await product.GetActiveSlugAsync(), - attributeCombination.AttributeSelection)); - } - - return RedirectToRoute("Product", new { SeName = await product.GetActiveSlugAsync() }); - } + //var (product, attributeCombination) = await _productService.Value.GetProductByCodeAsync(term); + //if (product != null) + //{ + // if (attributeCombination != null) + // { + // return Redirect(await _productUrlHelper.GetProductPathAsync( + // product.Id, + // await product.GetActiveSlugAsync(), + // attributeCombination.AttributeSelection)); + // } + + // return RedirectToRoute("Product", new { SeName = await product.GetActiveSlugAsync() }); + //} } result = await _catalogSearchService.SearchAsync(query); } catch (Exception ex) { - _searchResultModel.Error = ex.ToString(); + model.Error = ex.ToString(); result = new CatalogSearchResult(query); } @@ -174,7 +174,7 @@ await product.GetActiveSlugAsync(), if (result.TotalHitsCount > 0) { - _searchResultModel.AttemptedTerm = term; + model.AttemptedTerm = term; // Restore the original suggestions. result.SpellCheckerSuggestions = oldSuggestions.Where(x => x != query.DefaultTerm).ToArray(); } @@ -184,9 +184,9 @@ await product.GetActiveSlugAsync(), } } - _searchResultModel.SearchResult = result; - _searchResultModel.Term = query.DefaultTerm; - _searchResultModel.TotalProductsCount = result.TotalHitsCount; + model.SearchResult = result; + model.Term = query.DefaultTerm; + model.TotalProductsCount = result.TotalHitsCount; var productSummaryViewMode = query.CustomData.Get("ViewMode") is string viewMode && viewMode.EqualsNoCase("list") ? ProductSummaryViewMode.List @@ -199,9 +199,8 @@ await product.GetActiveSlugAsync(), _catalogHelper.MapListActions(summaryModel, null, _catalogSettings.DefaultPageSizeOptions); // Add product hits. - _searchResultModel.TopProducts = summaryModel; - - return View(_searchResultModel); + model.TopProducts = summaryModel; + return model; } } } diff --git a/src/Smartstore.Web/Models/Search/SearchResultModel.cs b/src/Smartstore.Web/Models/Search/SearchResultModel.cs index 82024d4613..b70284e842 100644 --- a/src/Smartstore.Web/Models/Search/SearchResultModel.cs +++ b/src/Smartstore.Web/Models/Search/SearchResultModel.cs @@ -5,12 +5,12 @@ namespace Smartstore.Web.Models.Search { public class SearchResultModel : SearchResultModelBase, ISearchResultModel { - //public SearchResultModel(CatalogSearchQuery query) - //{ - // Query = query; - //} + public SearchResultModel(CatalogSearchQuery query) + { + Query = query; + } - public CatalogSearchQuery Query { get; set; } + public CatalogSearchQuery Query { get; } public CatalogSearchResult SearchResult { get; set; } diff --git a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs new file mode 100644 index 0000000000..862c1ea7c6 --- /dev/null +++ b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs @@ -0,0 +1,37 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using NUnit.Framework; +using Smartstore.Core.Catalog.Search; +using Smartstore.Test.Common; +using Smartstore.Web.Controllers; +using Smartstore.Web.Models.Search; +using Microsoft.AspNetCore.Mvc; +using Smartstore.Web.Models.Catalog; + +namespace Smartstore.Web.Tests.Common.Controllers; + +[TestFixture] +public class SearchControllerTests +{ + [Test] + public async Task Term_Not_Found() + { + SearchSettings settings = new SearchSettings(); + settings.InstantSearchTermMinLength = 1; + + SearchController controller = new(null, null, null, null, settings, null, null, null); + CatalogSearchQuery query = new CatalogSearchQuery(); + query.DefaultTerm = "a"; + var actual = await controller.GetSearchResultModel(query); + + SearchResultModel expected_result = new SearchResultModel(query); + expected_result.SearchResult = new CatalogSearchResult(query); + expected_result.TopProducts = ProductSummaryModel.Empty; + expected_result.Error = "Search.SearchTermMinimumLengthIsNCharacters"; + + //need to mock db for this assert + //Assert.AreEqual(expected_result.SearchResult, actual.SearchResult); + Assert.AreEqual(expected_result.TopProducts, actual.TopProducts); + Assert.AreEqual(expected_result.Error, actual.Error); + } +} From 56cefe90b3f06c7f73f66bbe972381fc72af012e Mon Sep 17 00:00:00 2001 From: Zackery Anderson Date: Fri, 8 Sep 2023 14:23:54 -0600 Subject: [PATCH 3/7] changes from class --- .../Controllers/SearchControllerTests.cs | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs index 862c1ea7c6..732b30757d 100644 --- a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs +++ b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs @@ -7,6 +7,9 @@ using Smartstore.Web.Models.Search; using Microsoft.AspNetCore.Mvc; using Smartstore.Web.Models.Catalog; +using Smartstore.Collections; +using Moq; +using Smartstore.Core.Catalog.Products; namespace Smartstore.Web.Tests.Common.Controllers; @@ -17,7 +20,7 @@ public class SearchControllerTests public async Task Term_Not_Found() { SearchSettings settings = new SearchSettings(); - settings.InstantSearchTermMinLength = 1; + settings.InstantSearchTermMinLength = 2; SearchController controller = new(null, null, null, null, settings, null, null, null); CatalogSearchQuery query = new CatalogSearchQuery(); @@ -30,8 +33,33 @@ public async Task Term_Not_Found() expected_result.Error = "Search.SearchTermMinimumLengthIsNCharacters"; //need to mock db for this assert - //Assert.AreEqual(expected_result.SearchResult, actual.SearchResult); + Assert.AreEqual(expected_result.SearchResult.TotalHitsCount, actual.SearchResult.TotalHitsCount); Assert.AreEqual(expected_result.TopProducts, actual.TopProducts); Assert.AreEqual(expected_result.Error, actual.Error); } + + [Test] + public async Task Search_Item() + { + SearchSettings settings = new SearchSettings(); + settings.InstantSearchTermMinLength = 2; + + SearchController controller = new(null, null, null, null, settings, null, null, null); + CatalogSearchQuery query = new CatalogSearchQuery(); + query.DefaultTerm = "baseball"; + var actual = await controller.GetSearchResultModel(query); + + SearchResultModel expected_result = new SearchResultModel(query); + expected_result.SearchResult = new CatalogSearchResult(query); + + var pagedMock = new Mock>(); + var pagedList = pagedMock.Object; + + pagedMock.Setup(x => x.it); + + expected_result.TopProducts = new ProductSummaryModel(pagedList); + + //need to mock db for this assert + Assert.AreEqual(expected_result.TopProducts.Items.Count, actual.TopProducts.Items.Count); + } } From 34c01093677c1c8c3fb8d7e985ad6ffa603e80b1 Mon Sep 17 00:00:00 2001 From: Zackery Anderson Date: Mon, 11 Sep 2023 14:06:25 -0600 Subject: [PATCH 4/7] Finished Setup for mock --- .../Common/Controllers/SearchControllerTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs index 732b30757d..6e668fa7ab 100644 --- a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs +++ b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs @@ -55,7 +55,8 @@ public async Task Search_Item() var pagedMock = new Mock>(); var pagedList = pagedMock.Object; - pagedMock.Setup(x => x.it); + pagedMock.Setup(x => x.FirstItemIndex == 0); + expected_result.TopProducts = new ProductSummaryModel(pagedList); From db1779df0617d2a3536f94baecc6d988e403c111 Mon Sep 17 00:00:00 2001 From: Breanna Nesbit Date: Fri, 15 Sep 2023 09:24:08 -0600 Subject: [PATCH 5/7] took everything that was the model into a new services class --- .../Controllers/SearchController.cs | 102 ++++-------------- .../Controllers/SearchControllerService.cs | 99 +++++++++++++++++ 2 files changed, 121 insertions(+), 80 deletions(-) create mode 100644 src/Smartstore.Web/Controllers/SearchControllerService.cs diff --git a/src/Smartstore.Web/Controllers/SearchController.cs b/src/Smartstore.Web/Controllers/SearchController.cs index f739aded4f..053970050e 100644 --- a/src/Smartstore.Web/Controllers/SearchController.cs +++ b/src/Smartstore.Web/Controllers/SearchController.cs @@ -109,98 +109,40 @@ await _localizedEntityService.PrefetchLocalizedPropertiesAsync( [LocalizedRoute("/search", Name = "Search")] public async Task Search(CatalogSearchQuery query) - { - return View(await GetSearchResultModel(query)); - } - - public async Task GetSearchResultModel(CatalogSearchQuery query) { - CatalogSearchResult result = null; - var model = new SearchResultModel(query); var term = query?.DefaultTerm; - if (term == null || term.Length < _searchSettings.InstantSearchTermMinLength) - { - model.SearchResult = new CatalogSearchResult(query); - model.TopProducts = ProductSummaryModel.Empty; - model.Error = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength); - return model; - } - - //break into it's own method customer check - var customer = Services.WorkContext.CurrentCustomer; - if (!customer.IsSystemAccount) + if (_searchSettings.SearchProductByIdentificationNumber) { - customer.GenericAttributes.LastContinueShoppingPage = HttpContext.Request.RawUrl(); - } - - //break into it's own method search settings - try - { - if (_searchSettings.SearchProductByIdentificationNumber) + var (product, attributeCombination) = await _productService.Value.GetProductByCodeAsync(term); + if (product != null) { - //var (product, attributeCombination) = await _productService.Value.GetProductByCodeAsync(term); - //if (product != null) - //{ - // if (attributeCombination != null) - // { - // return Redirect(await _productUrlHelper.GetProductPathAsync( - // product.Id, - // await product.GetActiveSlugAsync(), - // attributeCombination.AttributeSelection)); - // } - - // return RedirectToRoute("Product", new { SeName = await product.GetActiveSlugAsync() }); - //} - } + if (attributeCombination != null) + { + return Redirect(await _productUrlHelper.GetProductPathAsync( + product.Id, + await product.GetActiveSlugAsync(), + attributeCombination.AttributeSelection)); + } - result = await _catalogSearchService.SearchAsync(query); - } - catch (Exception ex) - { - model.Error = ex.ToString(); - result = new CatalogSearchResult(query); - } + return RedirectToRoute("Product", new { SeName = await product.GetActiveSlugAsync() }); - //break into it's own method spell check suggestion - if (result.TotalHitsCount == 0 && result.SpellCheckerSuggestions.Length > 0) - { - // No matches, but spell checker made a suggestion. - // We implicitly search again with the first suggested term. - var oldSuggestions = result.SpellCheckerSuggestions; - - query.DefaultTerm = oldSuggestions[0]; - result = await _catalogSearchService.SearchAsync(query); - - if (result.TotalHitsCount > 0) - { - model.AttemptedTerm = term; - // Restore the original suggestions. - result.SpellCheckerSuggestions = oldSuggestions.Where(x => x != query.DefaultTerm).ToArray(); - } - else - { - query.DefaultTerm = term; + } } + return View(await GetSearchResultModel(query)); + } - model.SearchResult = result; - model.Term = query.DefaultTerm; - model.TotalProductsCount = result.TotalHitsCount; - - var productSummaryViewMode = query.CustomData.Get("ViewMode") is string viewMode && viewMode.EqualsNoCase("list") - ? ProductSummaryViewMode.List - : ProductSummaryViewMode.Grid; - - var mappingSettings = _catalogHelper.GetBestFitProductSummaryMappingSettings(productSummaryViewMode); - var summaryModel = await _catalogHelper.MapProductSummaryModelAsync(result, mappingSettings); + public async Task GetSearchResultModel(CatalogSearchQuery query) + { + CatalogSearchResult result = null; + var model = new SearchResultModel(query); - // Prepare paging/sorting/mode stuff. - _catalogHelper.MapListActions(summaryModel, null, _catalogSettings.DefaultPageSizeOptions); - // Add product hits. - model.TopProducts = summaryModel; - return model; + //break into it's own method search settings + + var service = new SearchControllerService(_catalogSearchService, _searchSettings, _catalogSettings, _catalogHelper); + return await service.GetSearchResultService(model, result, query); } } } diff --git a/src/Smartstore.Web/Controllers/SearchControllerService.cs b/src/Smartstore.Web/Controllers/SearchControllerService.cs new file mode 100644 index 0000000000..4de4369219 --- /dev/null +++ b/src/Smartstore.Web/Controllers/SearchControllerService.cs @@ -0,0 +1,99 @@ +using Smartstore.Core.Catalog; +using Smartstore.Core.Catalog.Search; +using Smartstore.Web.Models.Catalog; +using Smartstore.Web.Models.Search; + +namespace Smartstore.Web.Controllers +{ + public class SearchControllerService: PublicController + { + private readonly CatalogHelper _catalogHelper; + private readonly SearchSettings _searchSettings; + private readonly ICatalogSearchService _catalogSearchService; + private readonly CatalogSettings _catalogSettings; + + + public SearchControllerService(ICatalogSearchService catalogSearchService, + SearchSettings searchSettings, + CatalogSettings catalogSettings, + CatalogHelper catalogHelper) + { + _catalogSearchService = catalogSearchService; + _searchSettings = searchSettings; + _catalogSettings = catalogSettings; + _catalogHelper = catalogHelper; + } + public async Task GetSearchResultService(SearchResultModel model, CatalogSearchResult result, CatalogSearchQuery query) + { + var term = query?.DefaultTerm; + + if (term == null || term.Length < _searchSettings.InstantSearchTermMinLength) + { + model.SearchResult = new CatalogSearchResult(query); + model.TopProducts = ProductSummaryModel.Empty; + model.Error = T("Search.SearchTermMinimumLengthIsNCharacters", _searchSettings.InstantSearchTermMinLength); + return model; + } + + CustomerCheck(); + + try + { + result = await _catalogSearchService.SearchAsync(query); + } + catch (Exception ex) + { + model.Error = ex.ToString(); + result = new CatalogSearchResult(query); + } + + if (result.TotalHitsCount == 0 && result.SpellCheckerSuggestions.Length > 0) + { + // No matches, but spell checker made a suggestion. + // We implicitly search again with the first suggested term. + var oldSuggestions = result.SpellCheckerSuggestions; + + query.DefaultTerm = oldSuggestions[0]; + result = await _catalogSearchService.SearchAsync(query); + + if (result.TotalHitsCount > 0) + { + model.AttemptedTerm = term; + // Restore the original suggestions. + result.SpellCheckerSuggestions = oldSuggestions.Where(x => x != query.DefaultTerm).ToArray(); + } + else + { + query.DefaultTerm = term; + } + } + + model.SearchResult = result; + model.Term = query.DefaultTerm; + model.TotalProductsCount = result.TotalHitsCount; + + var productSummaryViewMode = query.CustomData.Get("ViewMode") is string viewMode && viewMode.EqualsNoCase("list") + ? ProductSummaryViewMode.List + : ProductSummaryViewMode.Grid; + + var mappingSettings = _catalogHelper.GetBestFitProductSummaryMappingSettings(productSummaryViewMode); + var summaryModel = await _catalogHelper.MapProductSummaryModelAsync(result, mappingSettings); + + // Prepare paging/sorting/mode stuff. + _catalogHelper.MapListActions(summaryModel, null, _catalogSettings.DefaultPageSizeOptions); + + // Add product hits. + model.TopProducts = summaryModel; + return model; + } + + public void CustomerCheck() + { + var customer = Services.WorkContext.CurrentCustomer; + if (!customer.IsSystemAccount) + { + customer.GenericAttributes.LastContinueShoppingPage = HttpContext.Request.RawUrl(); + } + } + } +} From 515f28236916a1b8bf8f584c23ac64b5103ebf7e Mon Sep 17 00:00:00 2001 From: Zackery Anderson Date: Fri, 15 Sep 2023 13:02:05 -0600 Subject: [PATCH 6/7] Created a mock for test --- .../Controllers/SearchControllerTests.cs | 21 ++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs index 6e668fa7ab..8d5c737249 100644 --- a/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs +++ b/test/Smartstore.Web.Tests/Common/Controllers/SearchControllerTests.cs @@ -10,6 +10,8 @@ using Smartstore.Collections; using Moq; using Smartstore.Core.Catalog.Products; +using Smartstore.Core.Search.Facets; +using Smartstore.Core.Search; namespace Smartstore.Web.Tests.Common.Controllers; @@ -43,24 +45,19 @@ public async Task Search_Item() { SearchSettings settings = new SearchSettings(); settings.InstantSearchTermMinLength = 2; - - SearchController controller = new(null, null, null, null, settings, null, null, null); CatalogSearchQuery query = new CatalogSearchQuery(); query.DefaultTerm = "baseball"; - var actual = await controller.GetSearchResultModel(query); - - SearchResultModel expected_result = new SearchResultModel(query); - expected_result.SearchResult = new CatalogSearchResult(query); - var pagedMock = new Mock>(); - var pagedList = pagedMock.Object; + var catalogSerchResult = new CatalogSearchResult(null, query, null, 1, null, null, null); - pagedMock.Setup(x => x.FirstItemIndex == 0); - + var searchServiceMock = new Mock(); + searchServiceMock.Setup(x => x.SearchAsync(query, false)).ReturnsAsync(catalogSerchResult); + var searchServiceMockObject = searchServiceMock.Object; - expected_result.TopProducts = new ProductSummaryModel(pagedList); + SearchController controller = new(null, null, null, null, settings, null, null, null); + var actual = await controller.GetSearchResultModel(query); //need to mock db for this assert - Assert.AreEqual(expected_result.TopProducts.Items.Count, actual.TopProducts.Items.Count); + Assert.AreEqual(1, actual.TopProducts.Items.Count); } } From 5cbb0a53a737edc928db19eb467bb61fb6bb599c Mon Sep 17 00:00:00 2001 From: Breanna Nesbit Date: Tue, 19 Sep 2023 08:55:45 -0600 Subject: [PATCH 7/7] services --- src/Smartstore.Web/Controllers/SearchController.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Smartstore.Web/Controllers/SearchController.cs b/src/Smartstore.Web/Controllers/SearchController.cs index 053970050e..93c35065dd 100644 --- a/src/Smartstore.Web/Controllers/SearchController.cs +++ b/src/Smartstore.Web/Controllers/SearchController.cs @@ -126,8 +126,6 @@ await product.GetActiveSlugAsync(), } return RedirectToRoute("Product", new { SeName = await product.GetActiveSlugAsync() }); - - } } return View(await GetSearchResultModel(query)); @@ -137,9 +135,6 @@ public async Task GetSearchResultModel(CatalogSearchQuery que { CatalogSearchResult result = null; var model = new SearchResultModel(query); - - - //break into it's own method search settings var service = new SearchControllerService(_catalogSearchService, _searchSettings, _catalogSettings, _catalogHelper); return await service.GetSearchResultService(model, result, query);