Example #1
0
        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);
        }
Example #2
0
        public void AddCachedWord_AddsNewCachedWordToRepository()
        {
            var phrase = new Phrase {
                Id = 1, Text = "Dievas"
            };
            var anagrams = new List <Anagram>
            {
                new Anagram {
                    Id = 1, Text = "deivas"
                },
                new Anagram {
                    Id = 2, Text = "veidas"
                }
            };

            _cachedWordsService.AddCachedWord(phrase, anagrams);
            _cachedWordsRepository.Received().AddCachedWord(Arg.Is <int>(1), Arg.Any <IEnumerable <Anagram> >());
            _cachedWordsRepository.ReceivedCalls().ShouldNotBeEmpty();
            _cachedWordsRepository.ReceivedCalls().ToList().Count.ShouldBe(1);
        }