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)); } }
public async Task <IActionResult> Post(string word, string category) { var anagram = new Anagram() { Category = category, Word = word }; await _wordService.InsertWord(anagram); await _context.SaveChangesAsync(); return(Ok()); }
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 InsertLogSuccess() { var log = new UserLog( "169.35", "phrase15", TimeSpan.FromSeconds(5), UserActionTypes.GetAnagrams.ToString()); await _repo.InsertLog(log); await _context.SaveChangesAsync(); var all = await _context.UserLogs.ToListAsync(); var insertedLog = await _context.UserLogs.Where(x => x.Phrase == log.SearchPhrase).SingleOrDefaultAsync(); Assert.AreEqual(log.SearchPhrase, insertedLog.Phrase); Assert.AreEqual(log.Ip, insertedLog.Ip); Assert.AreEqual(log.Action, insertedLog.Action); }
public async Task InsertNewWordSuccess() { var word = new Anagram() { Category = "dkt", Word = "alus" }; await _repo.AddNewWord(word); await _context.SaveChangesAsync(); var insertedWord = await _context.Words.Where(x => x.Word == word.Word).FirstOrDefaultAsync(); Assert.AreEqual(word.Category, insertedWord.Category); Assert.AreEqual(word.Word, insertedWord.Word); Assert.AreEqual(String.Concat(word.Word.OrderBy(x => x)), insertedWord.SortedWord); }
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); }
public async Task InsertAndFetchCachedWord() { var word = new CachedWord("test-phrase8", "1;2;3"); await _repo.InsertCachedWord(word); await _context.SaveChangesAsync(); var insertedWord = await _repo.GetCachedWord(word.SearchPhrase); Assert.AreEqual(word.SearchPhrase, insertedWord.Phrase); Assert.AreEqual(word.AnagramsIds, insertedWord.AnagramsIds); }
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 async Task <IActionResult> Get(string word) { try { if (string.IsNullOrEmpty(word)) { throw new Exception("You must enter at least one word"); } var anagrams = await _anagramSolver.GetAnagrams(word); //removing input element anagrams.Remove(word); //saves inserted cached word await _context.SaveChangesAsync(); return(Ok(anagrams)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }