public async Task <IActionResult> Create(GruntTaskModel taskModel) { try { GruntTask task = new GruntTask { Name = taskModel.Name, Description = taskModel.Description, Help = taskModel.Help, Code = taskModel.Code, UnsafeCompile = taskModel.UnsafeCompile, TokenTask = taskModel.TokenTask, Options = taskModel.Options }; taskModel.ReferenceSourceLibraries.ForEach(async RSL => { task.Add(await _context.GetReferenceSourceLibrary(RSL)); }); taskModel.ReferenceAssemblies.ForEach(async RA => { task.Add(await _context.GetReferenceAssembly(RA)); }); taskModel.EmbeddedResources.ForEach(async ER => { task.Add(await _context.GetEmbeddedResource(ER)); }); ViewBag.ReferenceSourceLibraries = await _context.GetReferenceSourceLibraries(); ViewBag.ReferenceAssemblies = await _context.GetReferenceAssemblies(); ViewBag.EmbeddedResources = await _context.GetEmbeddedResources(); GruntTask createdTask = await _context.CreateGruntTask(task); return(RedirectToAction(nameof(Edit), new { Id = createdTask.Id })); } catch (Exception e) when(e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException) { ViewBag.ReferenceSourceLibraries = await _context.GetReferenceSourceLibraries(); ViewBag.ReferenceAssemblies = await _context.GetReferenceAssemblies(); ViewBag.EmbeddedResources = await _context.GetEmbeddedResources(); return(View(new GruntTask())); } }
public async Task <GruntTask> CreateGruntTask(GruntTask task) { //Need to consider restructuring this method and the context class //The way it is currently done is built around interacting with a sqllite db. //need to decide if the Empire server will manage the DB directly. List <GruntTaskOption> options = task.Options.ToList(); List <EmbeddedResource> resources = task.EmbeddedResources.ToList(); List <ReferenceAssembly> assemblies = task.ReferenceAssemblies.ToList(); List <ReferenceSourceLibrary> libraries = task.ReferenceSourceLibraries.ToList(); task.Options = new List <GruntTaskOption>(); task.EmbeddedResources.ForEach(ER => task.Remove(ER)); task.ReferenceAssemblies.ForEach(RA => task.Remove(RA)); task.ReferenceSourceLibraries.ForEach(RSL => task.Remove(RSL)); task.Id = _context.GetNextTaskID(); foreach (GruntTaskOption option in options) { option.GruntTaskId = task.Id; //since the option is being added to the task not sure the options need to be stored separately //this was a structure done for the covenant Db _context.Add(option); task.Options.Add(option); } foreach (EmbeddedResource resource in resources) { await this.CreateEntities( new GruntTaskEmbeddedResource { EmbeddedResource = await this.GetEmbeddedResourceByName(resource.Name), GruntTask = task } ); task.Add(resource); } foreach (ReferenceAssembly assembly in assemblies) { //This is all Database schema based so doesn't work without the databasse await this.CreateEntities( new GruntTaskReferenceAssembly { ReferenceAssembly = await this.GetReferenceAssemblyByName(assembly.Name, assembly.DotNetVersion), GruntTask = task } ); //instead do this task.Add(assembly); } foreach (ReferenceSourceLibrary library in libraries) { /* await this.CreateEntities( * new GruntTaskReferenceSourceLibrary * { * ReferenceSourceLibrary = await this.GetReferenceSourceLibraryByName(library.Name), * GruntTask = task * } * );*/ task.Add(library); } //add the Grunt task to teh context list _context.Add(task); // _notifier.OnCreateGruntTask(this, task); return(await this.GetGruntTask(task.Id)); }