public DiseaseSearchResultViewModel GetSearchResult(IEnumerable<string> selectedSymptomsIdsAsString, DiseaseTargetType targetGroup) { var selectedSymptomsSet = new HashSet<Symptom>(); var relatedDiseasesSet = new HashSet<Disease>(); this.BuildDiseasesList(selectedSymptomsIdsAsString, selectedSymptomsSet, relatedDiseasesSet, targetGroup); var result = this.BuildSearchResultList(selectedSymptomsSet, relatedDiseasesSet); return result; }
public ActionResult SearchResult(IEnumerable<string> selectedSymptomsNames, DiseaseTargetType TargetGroup) { var selectedSymptomsIds = this.Symptoms.GetSymptomsIdsByNames(selectedSymptomsNames); this.Symptoms.AddToLastSearches(selectedSymptomsIds, TargetGroup); var cacheKey = this.GetCacheKeyFromSymptomsList(selectedSymptomsIds, TargetGroup); var model = this.Cache.Get( cacheKey, () => this.Diseases.GetSearchResult(selectedSymptomsIds, TargetGroup), 20 * 60); return this.View(model); }
public void BuildDiseasesList( IEnumerable<string> selectedSymptomsIdsAsString, HashSet<Symptom> selectedSymptomsSet, HashSet<Disease> relatedDiseasesSet, DiseaseTargetType targetGroup) { foreach (var selectedSymptomIdAsString in selectedSymptomsIdsAsString) { // TODO: Validation if string can be parsed var selectedSymptomId = int.Parse(selectedSymptomIdAsString); var symptom = this.Symptoms.All() .Include(x => x.Diseases) .FirstOrDefault(x => x.SymptomId == selectedSymptomId); if (symptom != null && symptom.Diseases.Count > 0) { if (!selectedSymptomsSet.Contains(symptom)) { selectedSymptomsSet.Add(symptom); } foreach (var disease in symptom.Diseases) { if (targetGroup.Equals(DiseaseTargetType.Both) || disease.Target.Equals(targetGroup)) { if (!relatedDiseasesSet.Contains(disease)) { relatedDiseasesSet.Add(disease); } } } } } }
public void AddToLastSearches(IEnumerable<string> selectedSymptomsIds, DiseaseTargetType targetGroup) { var lastSearches = this.Cache.Get( ServicesConstants.SymptomsLastSearchedCacheKey, () => { return new List<SearchViewModel>(); }, 24 * 60 * 60); var search = new SearchViewModel() { SearchedOn = DateTime.UtcNow, TargetGroup = targetGroup, SymptomsSearched = new List<SymptomViewModel>() }; foreach (var selectedSymptomIdAsString in selectedSymptomsIds) { var selectedSymptomId = int.Parse(selectedSymptomIdAsString); var symptom = this.Symptoms.All().SingleOrDefault(x => x.SymptomId == selectedSymptomId); var symptomViewModel = this.Mapper.Map<SymptomViewModel>(symptom); search.SymptomsSearched.Add(symptomViewModel); } // If the search has valid symptoms & // If the same item does not exist in the list // Insert the search request if (search.SymptomsSearchedIds.Count > 0 && !lastSearches.Contains(search, new SearchViewModelEqualityComparer())) { lastSearches.Add(search); if (lastSearches.Count > 10) { var itemToRemove = lastSearches.FirstOrDefault(); lastSearches.Remove(itemToRemove); } } }
public string GetCacheKeyFromSymptomsList(IEnumerable<string> selectedSymptomsIds, DiseaseTargetType TargetGroup) { var cacheKeyBuilder = new StringBuilder(); cacheKeyBuilder.Append(TargetGroup.GetDisplayName()); selectedSymptomsIds = selectedSymptomsIds.OrderBy(x => x); foreach (var symptomId in selectedSymptomsIds) { cacheKeyBuilder.Append(symptomId); cacheKeyBuilder.Append('/'); } return cacheKeyBuilder.ToString(); }