public ActionResult RegeneratePositions(int id) { Election election = db.Elections.Find(id); if (election == null) { return(HttpNotFound()); } if (ElectionLifecycleInfo.CanForcePositionGeneration(election)) { using (DbContextTransaction transaction = db.Database.BeginTransaction()) { election.PositionGenerationInProcess = true; db.SaveChanges(); // Undo the PositionGenerationInProcess change if this fails - otherwise the election will be stuck in limbo forever BackgroundJob.Enqueue(() => GeneratePositions.Execute(election.Id, JobCancellationToken.Null)); transaction.Commit(); } } return(RedirectToAction("Details", new { id })); }
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)); }