//Create public async Task <ProcessModel> CreateNewProcess(ProcessModel process) { var processEntity = process.ToEntity(); var lastUsedId = await context.Process.MaxAsync(i => i.ProcessId); processEntity.ProcessId = lastUsedId + 1; if (process.Revisions.Count() > 1) { throw new Exception("Cannot save a process and multiple revisions at once"); } var theRevision = process.Revisions.FirstOrDefault(); theRevision.DateTimeCreated = DateTime.Now; theRevision.ProcessId = processEntity.ProcessId; theRevision.ProcessRevId = 1; //This is the first revision of a new process, so it should always be 1. theRevision.RevStatusId = 2; //The revisions will start as UNLOCKED. processEntity.ProcessRevision.Add(theRevision.ToEntity()); context.Process.Add(processEntity); await context.SaveChangesAsync(); return(processEntity.ToHydratedModel()); }
//Takes a current process and makes a copy of it with the only revision on it being the locked rev of the existing process public async Task <ProcessModel> CopyToNewProcessFromExisting(ProcessModel aProcessModel) { var theProcessEntity = aProcessModel.ToEntity(); var lastUsedProcessId = await context.Process.MaxAsync(i => i.ProcessId); theProcessEntity.ProcessId = lastUsedProcessId + 1; //Grabs the locked revision of the process being copied. var theOldRevisionEntity = await context.ProcessRevision.Where(i => i.ProcessId == aProcessModel.ProcessId && i.RevStatusId == 1) //1 = LOCKED .Include(i => i.ProcessStepSeq) .ThenInclude(i => i.Operation) .ThenInclude(i => i.OperGroup) .Include(i => i.ProcessStepSeq) .ThenInclude(i => i.Step).FirstOrDefaultAsync(); var theRevisionEntity = new ProcessRevision(); theRevisionEntity.ProcessRevId = 1; theRevisionEntity.RevStatusId = 2; //2 = UNLOCKED theRevisionEntity.DateCreated = DateTime.Now; theRevisionEntity.TimeCreated = DateTime.Now.TimeOfDay; theRevisionEntity.ProcessId = theProcessEntity.ProcessId; theRevisionEntity.Comments = aProcessModel.Revisions.FirstOrDefault().Comments; theRevisionEntity.CreatedByEmp = aProcessModel.Revisions.FirstOrDefault().CreatedByEmp; var theNewStepSeqEntities = new List <ProcessStepSeq>(); foreach (var stepSeq in theOldRevisionEntity.ProcessStepSeq) { theNewStepSeqEntities.Add(new ProcessStepSeq { StepId = stepSeq.StepId, StepSeq = stepSeq.StepSeq, ProcessId = theProcessEntity.ProcessId, ProcessRevId = theRevisionEntity.ProcessRevId, OperationId = stepSeq.OperationId }); } theRevisionEntity.ProcessStepSeq = theNewStepSeqEntities; theProcessEntity.ProcessRevision.Add(theRevisionEntity); context.Process.Add(theProcessEntity); await context.SaveChangesAsync(); var result = await GetHydratedProcess(theProcessEntity.ProcessId); return(result); }