public async Task <Tuple <int, string> > CreateNewImplementTemplate(NewImplementTemplateViewModel newImplementTemplate) { List <LU_COMPART_TYPE> components = new List <LU_COMPART_TYPE>(); // New components that need to be added to the database // Check if a template with this name exists for the same category and customer bool existingTemplate = _context.LU_IMPLEMENT.Where(i => i.implementdescription.ToLower() == newImplementTemplate.TemplateName.ToLower() && i.CustomerId == newImplementTemplate.CustomerId && i.implement_category_auto == (int)newImplementTemplate.ImplementCategory).Any(); if (existingTemplate) { return(Tuple.Create(-1, "A template with this name already exists. ")); } // Create new implement LU_IMPLEMENT template = new LU_IMPLEMENT { CustomerId = newImplementTemplate.CustomerId, implementdescription = newImplementTemplate.TemplateName, implement_category_auto = (int)newImplementTemplate.ImplementCategory, parentID = 0 }; _context.LU_IMPLEMENT.Add(template); // Create any new components that don't exist yet, and then add all new and existing components to list foreach (ImplementComponentTypeViewModel componentType in newImplementTemplate.ComponentTypes) { if (componentType.Id == 0) // If ID = 0 then it is a new component we need to create { // Generate a stupid second ID we don't need. string id = componentType.Name; if (id.Length > 10) { id = id.Substring(0, 10); } // GET-68 if (containsReservedName(id)) { return(Tuple.Create(-1, "Unable to create component as it contains a reserved keyword!")); } // Work out the component type category for GET or Dump Body short componentCategory; if (newImplementTemplate.ImplementCategory == ImplementCategory.GET) { componentCategory = (int)ComponentTypeCategory.GET; } else { componentCategory = (int)ComponentTypeCategory.DumpBody; } LU_COMPART_TYPE newComponent = new LU_COMPART_TYPE() { comparttypeid = id, comparttype = componentType.Name, _protected = false, system_auto = componentCategory }; components.Add(newComponent); _context.LU_COMPART_TYPE.Add(newComponent); } else // Add existing components to list, so we can map them to this implement template { components.Add(_context.LU_COMPART_TYPE.Where(c => c.comparttype_auto == componentType.Id).First()); } } try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to create template and new component types. Exception: " + e.Message)); } // If everything saved successfully, map all the components to the new template foreach (LU_COMPART_TYPE comp in components) { _context.GET_IMPLEMENT_COMPARTTYPE.Add(new GET_IMPLEMENT_COMPARTTYPE() { comparttype_auto = comp.comparttype_auto, implement_auto = template.implement_auto }); } try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to map new component types to the new template. Exception: " + e.Message)); } // Map the models to the template foreach (int modelId in newImplementTemplate.EquipmentModels) { _context.GET_IMPLEMENT_MAKE_MODEL.Add(new GET_IMPLEMENT_MAKE_MODEL() { implement_auto = template.implement_auto, model_auto = modelId }); } try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to map the new template to it's models. Exception: " + e.Message)); } return(Tuple.Create((int)template.implement_auto, "Template created successfully. ")); }
public async Task <Tuple <int, string> > UpdateExistingImplementTemplate(NewImplementTemplateViewModel existingImplementTemplate) { List <LU_COMPART_TYPE> components = new List <LU_COMPART_TYPE>(); // New components that need to be added to the database // Check if the template exists. var template = _context.LU_IMPLEMENT.Find(existingImplementTemplate.TemplateId); // An existing template does not exist, so an update action cannot be performed. if (template == null) { return(Tuple.Create(-1, "Could not update this template. Please refresh the page and try again. ")); } template.CustomerId = existingImplementTemplate.CustomerId; template.implementdescription = existingImplementTemplate.TemplateName; template.implement_category_auto = (int)existingImplementTemplate.ImplementCategory; template.parentID = 0; // Save any changes made to the template. try { _context.SaveChanges(); } catch (Exception ex1) { return(Tuple.Create(-1, "Unable to save changes to the template. Exception: " + ex1.Message)); } // Create any new components that don't exist yet, and then add all new and existing components to list foreach (ImplementComponentTypeViewModel componentType in existingImplementTemplate.ComponentTypes) { if (componentType.Id == 0) // If ID = 0 then it is a new component we need to create { // Generate a stupid second ID we don't need. string id = componentType.Name; if (id.Length > 10) { id = id.Substring(0, 10); } // GET-68 if (containsReservedName(id)) { return(Tuple.Create(-1, "Unable to create component as it contains a reserved keyword!")); } // Work out the component type category for GET or Dump Body short componentCategory; if (existingImplementTemplate.ImplementCategory == ImplementCategory.GET) { componentCategory = (int)ComponentTypeCategory.GET; } else { componentCategory = (int)ComponentTypeCategory.DumpBody; } LU_COMPART_TYPE newComponent = new LU_COMPART_TYPE() { comparttypeid = id, comparttype = componentType.Name, _protected = false, system_auto = componentCategory }; components.Add(newComponent); _context.LU_COMPART_TYPE.Add(newComponent); } else // Add existing components to list, so we can map them to this implement template { components.Add(_context.LU_COMPART_TYPE.Where(c => c.comparttype_auto == componentType.Id).First()); } } // Save any changes made to the list of component types. try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to update component types. Exception: " + e.Message)); } // If everything saved successfully, map all the components to the new template foreach (LU_COMPART_TYPE comp in components) { // Check whether the mapping already exists. var implement_comparttype = await _context.GET_IMPLEMENT_COMPARTTYPE .Where(ic => ic.comparttype_auto == comp.comparttype_auto && ic.implement_auto == template.implement_auto) .FirstOrDefaultAsync(); // Create the mapping if it doesn't exist. if (implement_comparttype == null) { _context.GET_IMPLEMENT_COMPARTTYPE.Add(new GET_IMPLEMENT_COMPARTTYPE() { comparttype_auto = comp.comparttype_auto, implement_auto = template.implement_auto }); } } // Save the mapping of component types to the implement template. try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to map new component types to the new template. Exception: " + e.Message)); } // Map the models to the template foreach (int modelId in existingImplementTemplate.EquipmentModels) { // Check whether the mapping already exists. var implement_make_model = await _context.GET_IMPLEMENT_MAKE_MODEL .Where(imm => imm.implement_auto == template.implement_auto && imm.model_auto == modelId) .FirstOrDefaultAsync(); // Create the mapping if it doesn't already exist. if (implement_make_model == null) { _context.GET_IMPLEMENT_MAKE_MODEL.Add(new GET_IMPLEMENT_MAKE_MODEL() { implement_auto = template.implement_auto, model_auto = modelId }); } } // Save the mapping of models to the implement template. try { _context.SaveChanges(); } catch (Exception e) { return(Tuple.Create(-1, "Failed to map the new template to it's models. Exception: " + e.Message)); } return(Tuple.Create((int)template.implement_auto, "Template updated successfully. ")); }