public async Task <ActionResult> Save([FromBody] CreateVote model)
        {
            //This needs fixed
            if (!VotesEnabled)
            {
                return(ErrorView(ErrorViewModel.GetErrorViewModel(ErrorType.Unauthorized)));
            }

            try
            {
                ModelState.Clear();

                //Map to Domain Model
                var domainModel = model.Map();

                ////Reinsert Cache
                //cache.Replace($"VoteCreate:{User.Identity.Name}", domainModel);


                var valResult = Voat.Validation.ValidationHandler.Validate(domainModel);
                if (valResult != null)
                {
                    valResult.ForEach(x => ModelState.AddModelError(x.MemberNames.FirstOrDefault(), x.ErrorMessage));
                }
                CommandResponse <SaveVoteResponse> response = new CommandResponse <SaveVoteResponse>(new SaveVoteResponse(), Status.Success, "");

                if (ModelState.IsValid)
                {
                    //Save Vote
                    var cmd    = new PersistVoteCommand(domainModel).SetUserContext(User);
                    var result = await cmd.Execute();

                    if (result.Success)
                    {
                        response.Response.ID   = result.Response.ID;
                        response.Response.Html = await _viewRenderService.RenderToStringAsync("_View", await VoteAggregate.Load(result.Response));

                        return(JsonResult(response));
                        //return PartialView("_View", result.Response);
                    }
                    else
                    {
                        ModelState.AddModelError("", result.Message);
                    }
                }

                response.Status        = Status.Error;
                response.Response.Html = await _viewRenderService.RenderToStringAsync("_Edit", domainModel, ModelState);

                return(JsonResult(response));
                //return PartialView("_Edit", domainModel);
            }
            catch (Exception ex)
            {
                var x = ex;
                throw ex;
            }
        }
Exemple #2
0
        public async Task Verify_Database_Stats()
        {
            var user = TestHelper.SetPrincipal("unit");

            var createVote = new PersistVoteCommand(new Domain.Models.Vote()
            {
                Title    = "Unit Test Vote For Vote Stats",
                Content  = "We often wonder if you are willing to do it, so are you?",
                Subverse = "unit",
                Options  = new List <Domain.Models.VoteOption>()
                {
                    new Domain.Models.VoteOption()
                    {
                        Title = "Yes", Content = "This is most likely a yes"
                    },
                    new Domain.Models.VoteOption()
                    {
                        Title = "No", Content = "This is most likely a no"
                    },
                    new Domain.Models.VoteOption()
                    {
                        Title = "Maybe", Content = "This is most likely a maybe"
                    }
                },
                DisplayStatistics = true
            }).SetUserContext(user);

            var response = await createVote.Execute();

            VoatAssert.IsValid(response);
            var vote = response.Response;

            var seed     = 5;
            var multiple = 0;

            using (var db = new VoatDataContext())
            {
                foreach (var option in vote.Options)
                {
                    for (int i = 0; i < seed; i++)
                    {
                        db.VoteTracker.Add(new VoteTracker()
                        {
                            CreationDate = DateTime.UtcNow, VoteID = vote.ID, VoteOptionID = option.ID, RestrictionsPassed = true, UserName = $"UnitTestUser{(multiple * (i + 1)).ToString().PadLeft(2,'0')}"
                        });
                        db.VoteTracker.Add(new VoteTracker()
                        {
                            CreationDate = DateTime.UtcNow, VoteID = vote.ID, VoteOptionID = option.ID, RestrictionsPassed = false, UserName = $"UnitTestUser{(multiple * (i + 1)).ToString().PadLeft(2, '0')}"
                        });
                    }
                    multiple += 1;
                }
                db.SaveChanges();
            }



            using (var repo = new Voat.Data.Repository())
            {
                var data = await repo.GetVoteStatistics(vote.ID);

                Assert.IsNotNull(data);
                var friendly = data.Friendly;
                Assert.AreEqual(3, friendly.Count);

                foreach (var x in friendly)
                {
                    foreach (var voteOption in x.Value)
                    {
                        Assert.AreEqual(x.Key == VoteRestrictionStatus.All ? seed * 2 : seed, voteOption.Value.Count);
                        //Assert.AreEqual(5, voteOption.Value.Count);
                    }
                }
            }
        }