public async Task <IActionResult> OnPost() { if (ModelState.IsValid) { if (!(await ProcessDataAccess.CheckIfNameIsUnique(ProcessName))) { IsMessageGood = false; Message = "A process with that name already exists."; return(Page()); } var processToAdd = new ProcessModel() { Name = ProcessName }; var newRevision = new ProcessRevisionModel() { Comments = Comment, CreatedByEmp = EmployeeId }; var tempRevList = new List <ProcessRevisionModel>(); tempRevList.Add(newRevision); processToAdd.Revisions = tempRevList; var result = await ProcessDataAccess.PostNewProcess(processToAdd); ProcessId = result.ProcessId; IsMessageGood = true; Message = "Process saved successfully!"; return(Page()); } else { return(Page()); //The page will load with the validation errors if this is hit. } }
public async Task SetUpProperties(int aProcessId) { var theProcesses = await ProcessDataAccess.GetHydratedProcessesWithCurrentAnyRev(); AllProcesses = theProcesses.ToList(); foreach (var process in AllProcesses) { process.Revisions.OrderByDescending(i => i.ProcessRevId); } var theSteps = await StepDataAccess.GetAllSteps(); AllSteps = theSteps.OrderBy(i => i.StepName).ToList(); var theOperations = await OperationDataAccess.GetAllOperations(); AllOperations = theOperations.OrderBy(i => i.Name).ToList(); CurrentProcess = new ProcessModel(); //This needs to be created even if there isn't a process being passed in so the front-end doesn't throw a null reference exception when looking for a name. CurrentRev = new ProcessRevisionModel(); //This also needs to be created right away so the front-end doesn't throw a null reference exception when loading the current step list. CurrentRev.StepSeqs = new List <StepSeqModel>(); CurrentStepIds = null; CurrentOperationIds = null; if (aProcessId > 0) { CurrentProcess = AllProcesses.FirstOrDefault(i => i.ProcessId == aProcessId); CurrentProcessId = aProcessId; if (CurrentProcess.Revisions.Any()) { ModelState.Remove("CurrentRevId"); //The input field wasn't updating when deleting an unlocked revision. This clears the model state for just this property CurrentRev = CurrentProcess.Revisions.OrderByDescending(i => i.ProcessRevId).FirstOrDefault(); CurrentRevId = CurrentRev.ProcessRevId; Comment = CurrentRev.Comments; EmpCreatedCurrentRev = await EmployeeDataAccess.GetEmployeeById(CurrentRev.CreatedByEmp); CurrentOperations = new List <OperationModel>(); //Finds each unique operation within the revision's steps and loads it into CurrentOperations. foreach (var stepSeq in CurrentRev.StepSeqs) { var shouldThisStepBeAdded = true; foreach (var operation in CurrentOperations) { if (operation.Id == stepSeq.Operation.Id) { shouldThisStepBeAdded = false; } } if (shouldThisStepBeAdded) { CurrentOperations.Add(stepSeq.Operation); } } } else { ModelState.Remove("CurrentRevId"); //The input field wasn't updating when deleting an unlocked revision. This clears the model state for just this property CurrentRevId = 0; } } }