public IList <Anagram> GetAnagrams(string word, User user) { var stopWatch = new Stopwatch(); var timeElapsed = 0L; stopWatch.Start(); if (word == null) { return(new List <Anagram>()); } var phrase = _phrasesService.GetPhrase(word); if (phrase == null) { _phrasesService.AddPhrase(word); phrase = _phrasesService.GetPhrase(word); } var anagrams = _cachedWordsService.GetAnagrams(phrase).ToList(); if (anagrams.Count() != 0) { stopWatch.Stop(); timeElapsed = stopWatch.ElapsedMilliseconds; _userLogsService.LogUserInfo(phrase, user, anagrams, Convert.ToInt32(timeElapsed)); _usersService.UpdateUserSearchesCount(user.Id, user.SearchesLeft + 1); return(anagrams); } var resultCount = 1000; var words = _wordsService.GetWordsForSearch(word); anagrams = FindAnagrams(words, phrase.Text, new List <List <Anagram> >()) .Take(resultCount) .Select(a => new Anagram { Text = String.Join(' ', a.Select(t => t.Text)) }) .Where(a => a.Text.Replace(" ", "").ToLower() != phrase.Text.Replace(" ", "").ToLower()) .ToList(); _cachedWordsService.AddCachedWord(phrase, anagrams); anagrams = _cachedWordsService.GetAnagrams(phrase).ToList(); stopWatch.Stop(); timeElapsed = stopWatch.ElapsedMilliseconds; _userLogsService.LogUserInfo(phrase, user, anagrams, Convert.ToInt32(timeElapsed)); _usersService.UpdateUserSearchesCount(user.Id, user.SearchesLeft - 1); return(anagrams); }
public void GetAnagrams_GetsCachedAnagramsBasedOnInputPhrase() { _cachedWordsRepository.GetCachedWords().Returns(new List <CachedWord> { new CachedWord { Id = 1, AnagramId = 1, PhraseId = 1, Anagram = new Anagram { Id = 1, Text = "rizvanas" }, Phrase = new Phrase { Id = 1, Text = "Sanavzir" } }, new CachedWord { Id = 2, AnagramId = 2, PhraseId = 1, Anagram = new Anagram { Id = 1, Text = "vanas riz" }, Phrase = new Phrase { Id = 1, Text = "Sanavzir" } } }); var phrase = new Phrase { Id = 1, Text = "Sanavzir" }; var anagramsResult = _cachedWordsService.GetAnagrams(phrase); _cachedWordsRepository.Received(1).GetCachedWords(); anagramsResult.Count.ShouldBe(2); anagramsResult.First().Text.ShouldBe("rizvanas"); anagramsResult.Last().Text.ShouldBe("vanas riz"); }