public ActionResult Statistics(SearchEngineInputViewModel viewModel) { if (!ModelState.IsValid) { return(View(viewModel)); } //the cacheKey is not tied to search engine type only //if we want to search different keywords, we need diff implementation of cache string cacheKey = $"_SeoPositions_{viewModel.SearchEngineType.ToString()}"; string currentWebUIBasePath = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}"; var cacheData = GetFromCache(cacheKey); string fetchedData = string.Empty; if (cacheData != null) { fetchedData = cacheData; } else { fetchedData = _searchEngineViewModelService.GetSeoStatistics(viewModel, currentWebUIBasePath); SetInCache(cacheKey, fetchedData); } viewModel.StatisticsResult = fetchedData; viewModel.ShowResult = true; return(View(viewModel)); }
/// <summary> /// This method should contain common logic for ViewModel handling /// </summary> /// <param name="viewModel"></param> /// <param name="currentWebUIBasePath"></param> /// <returns></returns> public string GetSeoStatistics(SearchEngineInputViewModel viewModel, string currentWebUIBasePath) { // all these ISearchEngineService searchEngineService = _serviceAccessor(viewModel.SearchEngineType); var allResults = searchEngineService.PerformSearchAndGetResults(viewModel.SearchKeyWord, currentWebUIBasePath); if (!allResults.Any()) { return(CommonConstants.ZeroResult); } //we are interested in top 100 results only var searchResult = allResults.Take(CommonConstants.TopNumberOfResults); var positionList = new List <int>(); int count = 1; foreach (var item in searchResult) { if (item.Link.Contains(CommonConstants.SympliLinkToSearch, System.StringComparison.InvariantCultureIgnoreCase)) { positionList.Add(count); } count++; } if (!positionList.Any()) { return(CommonConstants.ZeroResult); } return(string.Join(',', positionList)); }