public ActionResult Bet(ActiveRoundViewModel model) { //Return back to view if any non predicted match var anyNonPredictedMatch = model.Matches.Any(x => x.UserPrediction == null); if (anyNonPredictedMatch) { this.TempData.AddErrorMessage(MessageResources.msgBetAllMatches); return(View(model)); } List <int> activeMatchesIds = this.predictionService.GetCurrentActiveMatchesIds(); Dictionary <int, ResultEnum> predictions = new Dictionary <int, ResultEnum>(); //Compare client side matches with in db active matches. foreach (var match in model.Matches) { bool activeMatch = activeMatchesIds.Contains(match.Id); if (activeMatch) { predictions.Add(match.Id, match.UserPrediction.Value); } } this.predictionService.AddPrediction(predictions, GetUserId()); this.TempData.AddSuccessMessage(MessageResources.msgSuccessfulPredictions); return(RedirectToAction(nameof(Bet))); }
public void BetPostSuccessfulySavedScoreAndRedirectToGetBet() { // Arrange ActiveRoundViewModel model = GetActiveRoundViewModel(ResultEnum.DRAW); string successMessage = null; var tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[WebConstants.TempDataSuccessMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => successMessage = message as string); var userManager = UserManagerMock.New; userManager.Setup(u => u.GetUserId(It.IsAny <ClaimsPrincipal>())) .Returns(userId); var predictionService = new Mock <IPredictionService>(); predictionService.Setup(p => p.GetCurrentActiveMatchesIds()) .Returns(new List <int>() { firstMatch.Id, secondMatch.Id }); predictionService.Setup(p => p.AddPrediction(null, null)); var controller = new PredictionController(predictionService.Object, userManager.Object); controller.TempData = tempData.Object; // Act var result = controller.Bet(model); // Assert result.Should().BeOfType <RedirectToActionResult>(); var redirectToActionResult = Assert.IsType <RedirectToActionResult>(result); redirectToActionResult.ControllerName.Should().Be(null); redirectToActionResult.ActionName.Should().BeEquivalentTo("Bet"); successMessage.Should().Be(MessageResources.msgSuccessfulPredictions); }
public ActionResult Bet() { var userId = GetUserId(); var matches = this.predictionService.GetActiveMatches(userId); var model = new ActiveRoundViewModel() { RoundNumber = this.predictionService.GetActiveRoundNumber(), AlreadyPredicted = this.predictionService.IsCurrentRoundPredicted(userId), Matches = matches .Where(x => x.IsPredictionAllowed) .ProjectTo <MatchViewModel>() .ToList(), StartedMatches = matches .Where(x => x.IsPredictionAllowed == false) .ProjectTo <MatchViewModel>() .ToList() }; model.StartedMatches = SetTeamEmblems(model.StartedMatches); model.Matches = SetTeamEmblems(model.Matches); return(View(model)); }
public void BetPostReturnViewWithModelWhenAnyNonPredictedMatch() { // Arrange string errorMessage = null; ActiveRoundViewModel model = GetActiveRoundViewModel(); var tempData = new Mock <ITempDataDictionary>(); tempData .SetupSet(t => t[WebConstants.TempDataErrorMessageKey] = It.IsAny <string>()) .Callback((string key, object message) => errorMessage = message as string); var controller = new PredictionController(null, null); controller.TempData = tempData.Object; // Act var result = controller.Bet(model); // Assert result.Should().BeOfType <ViewResult>(); var viewModel = result.As <ViewResult>().Model; viewModel.Should().BeOfType <ActiveRoundViewModel>(); var formModel = viewModel.As <ActiveRoundViewModel>(); formModel.AlreadyPredicted.Should().Be(false); formModel.Matches.Count.Should().Be(2); formModel.Matches.First().HomeTeam.Should().Be(firstMatch.HomeTeam); formModel.Matches.First().AwayTeam.Should().Be(firstMatch.AwayTeam); formModel.Matches.Last().HomeTeam.Should().Be(secondMatch.HomeTeam); formModel.Matches.Last().AwayTeam.Should().Be(secondMatch.AwayTeam); formModel.RoundNumber.Should().Be(RoundNumber); errorMessage.Should().Be(MessageResources.msgBetAllMatches); }