private void UpdateSections( PageTemplateFile fileTemplate, PageTemplateFileInfo fileTemplateDetails, PageTemplate dbPageTemplate, IExecutionContext executionContext ) { // De-dup section names var duplicateSectionName = fileTemplateDetails .Sections .GroupBy(s => s.Name.ToLowerInvariant()) .FirstOrDefault(g => g.Count() > 1); if (duplicateSectionName != null) { throw new PageTemplateRegistrationException($"Dulpicate template section '{ duplicateSectionName.First().Name }' in template { fileTemplate.FullPath }"); } // Deletions var sectionsToDelete = dbPageTemplate .PageTemplateSections .Where(ts => !fileTemplateDetails.Sections.Any(fs => ts.Name.Equals(fs.Name, StringComparison.OrdinalIgnoreCase))) .ToList(); foreach (var sectionToDelete in sectionsToDelete) { _dbContext.PageTemplateSections.Remove(sectionToDelete); } // Updates/Additions foreach (var fileSection in fileTemplateDetails.Sections) { var existing = dbPageTemplate .PageTemplateSections .FirstOrDefault(s => s.Name.Equals(fileSection.Name, StringComparison.OrdinalIgnoreCase)); if (existing == null) { existing = new PageTemplateSection(); existing.PageTemplate = dbPageTemplate; existing.CreateDate = executionContext.ExecutionDate; _dbContext.PageTemplateSections.Add(existing); } // casing might have changed if (existing.Name != fileSection.Name) { existing.Name = fileSection.Name; existing.UpdateDate = executionContext.ExecutionDate; } // this will detach section data but there's no migrating that... if (existing.IsCustomEntitySection != fileSection.IsCustomEntitySection) { existing.IsCustomEntitySection = fileSection.IsCustomEntitySection; existing.UpdateDate = executionContext.ExecutionDate; } } }
private async Task <PageTemplate> UpdateTemplate( IExecutionContext executionContext, PageTemplate dbPageTemplate, PageTemplateFile fileTemplate, PageTemplateFileInfo fileTemplateDetails ) { bool isNew = false; if (dbPageTemplate == null) { isNew = true; dbPageTemplate = new PageTemplate(); dbPageTemplate.FileName = fileTemplate.FileName; dbPageTemplate.CreateDate = executionContext.ExecutionDate; _dbContext.PageTemplates.Add(dbPageTemplate); } if (!isNew && HasCustomEntityDefinitionChanged(dbPageTemplate, fileTemplateDetails) && await IsTemplateInUse(dbPageTemplate)) { var msg = "Cannot change the custom entity type associated with a page template once it is in use"; throw new Exception(msg); } if (fileTemplateDetails.CustomEntityDefinition == null) { dbPageTemplate.PageTypeId = (int)PageType.Generic; dbPageTemplate.CustomEntityDefinitionCode = null; dbPageTemplate.CustomEntityModelType = null; } else { dbPageTemplate.PageTypeId = (int)PageType.CustomEntityDetails; dbPageTemplate.CustomEntityModelType = fileTemplateDetails.CustomEntityModelType; dbPageTemplate.CustomEntityDefinitionCode = fileTemplateDetails.CustomEntityDefinition.CustomEntityDefinitionCode; } if (dbPageTemplate.IsArchived) { // Re-add template that was previously deleted dbPageTemplate.IsArchived = false; } dbPageTemplate.Name = TextFormatter.PascalCaseToSentence(fileTemplate.FileName); dbPageTemplate.Description = fileTemplateDetails.Description; dbPageTemplate.FullPath = fileTemplate.VirtualPath; dbPageTemplate.UpdateDate = executionContext.ExecutionDate; ValidateTemplateProperties(dbPageTemplate); return(dbPageTemplate); }