Example #1
0
        public JsonResult SaveManualResults(ResultSummary manualResultsFromBrowser)
        {
            ResultSummary resultSummary       = null;
            var           resultSummaryCacher = new ResultSummaryCacher(Db);

            if (manualResultsFromBrowser.C_RowId != 0)
            {
                resultSummary = resultSummaryCacher.AllForThisElection.FirstOrDefault(rs =>
                                                                                      rs.C_RowId == manualResultsFromBrowser.C_RowId &&
                                                                                      rs.ResultType == ResultType.Manual);
            }
            if (resultSummary == null)
            {
                resultSummary = new ResultSummary
                {
                    ElectionGuid = UserSession.CurrentElectionGuid,
                    ResultType   = ResultType.Manual
                };
                Db.ResultSummary.Add(resultSummary);
                Db.SaveChanges();

                resultSummaryCacher.UpdateItemAndSaveCache(resultSummary);
            }
            else
            {
                Db.ResultSummary.Attach(resultSummary);
            }

            var editableFields = new
            {
                resultSummary.BallotsNeedingReview,
                resultSummary.BallotsReceived,
                resultSummary.CalledInBallots,
                //        resultSummary.OnlineBallots,
                //        resultSummary.ImportedBallots,
                resultSummary.DroppedOffBallots,
                resultSummary.InPersonBallots,
                resultSummary.MailedInBallots,
                resultSummary.Custom1Ballots,
                resultSummary.Custom2Ballots,
                resultSummary.Custom3Ballots,
                resultSummary.NumEligibleToVote,
                resultSummary.SpoiledManualBallots,
            }.GetAllPropertyInfos().Select(pi => pi.Name).ToArray();

            var changed = manualResultsFromBrowser.CopyPropertyValuesTo(resultSummary, editableFields);

            if (!changed)
            {
                return(new
                {
                    Message = "No changes"
                }.AsJsonResult());
            }

            Db.SaveChanges();

            _analyzer.AnalyzeHub.StatusUpdate("Starting Analysis from " + UserSession.CurrentComputerCode);

            resultSummaryCacher.UpdateItemAndSaveCache(resultSummary);

            _analyzer.AnalyzeEverything();
            //.PrepareResultSummaries();
            //_analyzer.FinalizeSummaries();

            var resultSummaries = _analyzer.ResultSummaries;

            return(new
            {
                Saved = true,
                ResultsManual =
                    (resultSummaries.FirstOrDefault(rs => rs.ResultType == ResultType.Manual) ??
                     new ResultSummary())
                    .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
                ResultsFinal =
                    resultSummaries.First(rs => rs.ResultType == ResultType.Final)
                    .GetPropertiesExcept(null, new[] { "ElectionGuid" }),
            }.AsJsonResult());
        }