Beispiel #1
0
        public ActionResult NewCourseRep(ElectionForm form)
        {
            ModelFieldsAccessibility fieldsInfo = NewDefaultFieldsInfo;

            // Ignore stuff that isn't supposed to be in new election
            fieldsInfo.ReplaceUneditableWithOldValues(form, new ElectionForm());
            this.RemoveIgnoredErrors(fieldsInfo);

            if (ModelState.IsValid)
            {
                Election election = Mapper.Map <Election>(form);
                election.Type = ElectionType.CourseRepresentative;
                election.PositionGenerationInProcess = true;

                ElectionStateChange createChangeInfo = new ElectionStateChange
                {
                    Election           = election,
                    PreviousState      = null,
                    TargetState        = election.State,
                    IsCausedByUser     = true,
                    InstigatorUsername = User.Identity.GetUserId(),
                    CompletedAt        = DateTime.Now
                };

                db.Elections.Add(election);
                db.ElectionStateChanges.Add(createChangeInfo);

                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    db.SaveChanges();

                    // This needs to be after the first big transaction - otherwise EF gets confused about order of actions
                    election.PopulateAutomaticStateTransitions();
                    db.SaveChanges();

                    transaction.Commit();
                }

                AuditLogManager.RecordNewElection(createChangeInfo);

                // Schedule the job to loop over the DB to generate the positions
                BackgroundJob.Enqueue(() => GeneratePositions.Execute(election.Id, JobCancellationToken.Null));

                return(RedirectToAction("Details", new { id = election.Id }));
            }

            ViewData[FormConstants.FieldsInfoKey] = fieldsInfo;

            return(View(form));
        }
Beispiel #2
0
        public ActionResult NewCouncil(CouncilElectionForm form)
        {
            ModelFieldsAccessibility fieldsInfo = NewCouncilFieldsInfo;

            // Ignore stuff that isn't supposed to be in new election
            fieldsInfo.ReplaceUneditableWithOldValues(form, new CouncilElectionForm());
            this.RemoveIgnoredErrors(fieldsInfo);

            if (ModelState.IsValid)
            {
                Election election = Mapper.Map <Election>(form);
                election.Type = ElectionType.StudentCouncil;

                CouncilElectionData councilData = Mapper.Map <CouncilElectionData>(form);
                councilData.Election = election;

                ElectionStateChange createChangeInfo = new ElectionStateChange
                {
                    Election           = election,
                    PreviousState      = null,
                    TargetState        = election.State,
                    IsCausedByUser     = true,
                    InstigatorUsername = User.Identity.GetUserId(),
                    CompletedAt        = DateTime.Now
                };

                db.Elections.Add(election);
                db.CouncilElectionData.Add(councilData);
                db.ElectionStateChanges.Add(createChangeInfo);

                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    db.SaveChanges();

                    // This needs to be after the first big transaction - otherwise EF gets confused about order of actions
                    election.PopulateAutomaticStateTransitions();
                    db.SaveChanges();

                    transaction.Commit();
                }

                AuditLogManager.RecordNewElection(createChangeInfo);

                return(RedirectToAction("Details", new { id = election.Id }));
            }

            ViewData[FormConstants.FieldsInfoKey] = fieldsInfo;

            return(View(form));
        }