コード例 #1
0
        public User GetUserFromWord(string word)
        {
            var userWord = _userWordsRepository
                           .GetUserWords()
                           .SingleOrDefault(uw => uw.Text.Trim().ToLower() == word.Trim().ToLower());

            return(_usersRepository.GetUser(userWord.UserId));
        }
コード例 #2
0
        public void RemoveUserWord(string word, string userIp)
        {
            var wordToRemove = _userWordsRepository
                               .GetUserWords()
                               .FirstOrDefault(uw => uw.Text.Trim().ToLower() == word.Trim().ToLower());

            if (wordToRemove == null)
            {
                throw new ArgumentException("The word you are trying to delete doesn't exist");
            }

            var user = _usersRepository
                       .GetUsers()
                       .FirstOrDefault(u => u.Ip == userIp);

            _userWordsRepository.DeleteUserWord(wordToRemove.Id);
            _usersService.UpdateUserSearchesCount(user.Id, user.SearchesLeft - 1);
        }
コード例 #3
0
        public void RemoveUserWord_RemovesUserWordFromUserWordsRepository()
        {
            _userWordsRepository.GetUserWords().Returns(new List <UserWord>
            {
                new UserWord {
                    Id = 1, UserId = 1, Text = "Unit"
                },
                new UserWord {
                    Id = 2, UserId = 1, Text = "Testas"
                },
                new UserWord {
                    Id = 3, UserId = 2, Text = "Mockinimo"
                },
                new UserWord {
                    Id = 3, UserId = 2, Text = "Karkasas"
                }
            });

            _usersRepository.GetUsers().Returns(new List <User>
            {
                new User {
                    Id = 1, Ip = "::1", SearchesLeft = 5
                },
                new User {
                    Id = 2, Ip = "::2", SearchesLeft = 4
                },
                new User {
                    Id = 3, Ip = "::3", SearchesLeft = 3
                },
                new User {
                    Id = 4, Ip = "::4", SearchesLeft = 2
                }
            });


            var testUser = _usersRepository.GetUsers().First();
            var testWord = _userWordsRepository.GetUserWords().First();

            _userWordsService.RemoveUserWord(testWord.Text, testUser.Ip);

            _userWordsRepository.ReceivedCalls().ShouldNotBeNull();
            _userWordsRepository.Received(1).DeleteUserWord(Arg.Is(testWord.Id));
        }
コード例 #4
0
        public void GetUserFromWord_GetsUserFromStringValue()
        {
            _userWordsRepository.GetUserWords().Returns(new List <UserWord>
            {
                new UserWord {
                    Id = 1, Text = "Anagrama", UserId = 1
                },
                new UserWord {
                    Id = 2, Text = "Lambda", UserId = 1
                },
                new UserWord {
                    Id = 3, Text = "Šilas", UserId = 2
                }
            });

            _usersRepository.GetUser(Arg.Is <int>(1))
            .Returns(new User {
                Id = 1, Ip = "::1", SearchesLeft = 4
            });

            _usersRepository.GetUser(Arg.Is <int>(2))
            .Returns(new User {
                Id = 2, Ip = "::2", SearchesLeft = 3
            });

            var userResult1 = _usersService.GetUserFromWord("Lambda");

            _usersRepository.Received().GetUser(Arg.Is <int>(1));
            userResult1.ShouldNotBeNull();
            userResult1.Id.ShouldBe(1);

            var userResult2 = _usersService.GetUserFromWord("Šilas");

            _usersRepository.Received().GetUser(Arg.Is <int>(2));
            userResult2.ShouldNotBeNull();
            userResult2.Id.ShouldBe(2);
        }