Exemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("Word,Case")] Anagram anagram)
        {
            try
            {
                if (string.IsNullOrEmpty(anagram.Word) || string.IsNullOrEmpty(anagram.Category))
                {
                    throw new Exception("You must fill all the fields");
                }

                _cookiesHandler.ClearAllCookies();
                Stopwatch sw = new Stopwatch();
                sw.Start();
                await _wordService.InsertWord(anagram);

                sw.Stop();

                await _userLogService.AddLog(sw.Elapsed, UserActionTypes.InsertWord);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                @ViewData["Error"] = ex.Message;
                return(View(anagram));
            }
        }
Exemplo n.º 2
0
        public async Task IndexSuccessWhenSearchingDBForResult()
        {
            var returnList = new List <string>()
            {
                "word1", "word2"
            };

            _userLogServiceMock.CountAnagramsLeftForIpToSolve().Returns(5);
            _searchHistoryServiceMock.GetSearchedAnagrams(Arg.Any <string>()).Returns((List <string>)null);
            _anagramSolverMock.GetAnagrams(Arg.Any <string>()).Returns(returnList);
            await _userLogServiceMock.AddLog(Arg.Any <TimeSpan>(), Arg.Any <UserActionTypes>(), Arg.Any <string>());

            _cookiesHandlerServiceMock.AddCookie(Arg.Any <string>(), Arg.Any <string>());
            await _contextMock.SaveChangesAsync();

            var result = await _controller.Index("labas") as ViewResult;

            var data = result.ViewData.Model as List <string>;

            await _userLogServiceMock.Received().CountAnagramsLeftForIpToSolve();

            await _searchHistoryServiceMock.Received().GetSearchedAnagrams(Arg.Any <string>());

            await _anagramSolverMock.Received().GetAnagrams(Arg.Any <string>());

            await _userLogServiceMock.Received().AddLog(Arg.Any <TimeSpan>(), Arg.Any <UserActionTypes>(), Arg.Any <string>());

            _cookiesHandlerServiceMock.Received().AddCookie(Arg.Any <string>(), Arg.Any <string>());
            await _contextMock.Received().SaveChangesAsync();

            Assert.AreEqual(returnList.Count, data.Count);
            Assert.AreEqual(returnList[0], data[0]);
            Assert.AreEqual(returnList[1], data[1]);
        }
        public async Task CreateSuccessWhenAllMandatoryFieldsFilled()
        {
            _cookiesHandlerServiceMock.ClearAllCookies();
            await _wordServiceMock.InsertWord(Arg.Any <Anagram>());

            await _userLogServiceMock.AddLog(Arg.Any <TimeSpan>(), Arg.Any <UserActionTypes>());

            await _contextMock.SaveChangesAsync();

            var result = await _controller.Create(_anagram) as RedirectToActionResult;

            _cookiesHandlerServiceMock.Received().ClearAllCookies();
            await _wordServiceMock.Received().InsertWord(Arg.Any <Anagram>());

            await _userLogServiceMock.Received().AddLog(Arg.Any <TimeSpan>(), Arg.Any <UserActionTypes>());

            await _contextMock.Received().SaveChangesAsync();

            Assert.AreEqual("Index", result.ActionName);
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Index([Required] string id)
        {
            try
            {
                if (string.IsNullOrEmpty(id))
                {
                    return(View());
                }

                IList <string> anagrams = new List <string>();

                var solvesLeft = await _userLogService.CountAnagramsLeftForIpToSolve();

                if (solvesLeft <= 0)
                {
                    throw new Exception("You reached your solve limit. Add new word to increase your limit");
                }

                anagrams = await _searchHistoryService.GetSearchedAnagrams(id);

                if (anagrams != null && anagrams.Count > 0)
                {
                    return(View(anagrams));
                }

                Stopwatch sw = new Stopwatch();
                sw.Start();
                anagrams = await _anagramSolver.GetAnagrams(id);

                sw.Stop();

                await _userLogService.AddLog(sw.Elapsed, UserActionTypes.GetAnagrams, id);

                //removing input element
                anagrams.Remove(id);

                _cookiesHandler.AddCookie(id, string.Join(";", anagrams.ToArray()));

                await _context.SaveChangesAsync();

                return(View(anagrams));
            }
            catch (Exception ex)
            {
                @ViewData["Error"] = ex.Message;
                return(View());
            }
        }
 public void FailToAddLogIfActionIsNotGetAnagrams()
 {
     Assert.ThrowsAsync <Exception>(
         async() => await _logService.AddLog(TimeSpan.Zero, UserActionTypes.DeleteWord));
 }