Exemplo n.º 1
0
        public void AddPhrase_AddsNewPhraseToPhrasesRepository()
        {
            _phrasesService.AddPhrase("Labas");

            _phrasesRepository.Received().AddPhrase(Arg.Any <Phrase>());
            _phrasesRepository.ReceivedCalls().ShouldNotBeEmpty();
            _phrasesRepository.ReceivedCalls().ToList().Count.ShouldBe(1);
        }
Exemplo n.º 2
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);
        }