public object DeleteForm(DeleteFormRequest request) { // Variables. var result = default(object); // Catch all errors. try { // Variables. var formId = GuidHelper.GetGuid(request.FormId); var configs = ConFormPersistence.RetrieveChildren(formId); // Delete the form and its configurations. foreach (var item in configs) { ConFormPersistence.Delete(item.Id); } Persistence.Delete(formId); // Success. result = new { Success = true }; } catch (Exception ex) { // Error. Logger.Error <FormsController>(ex, DeleteFormError); result = new { Success = false, Reason = UnhandledError }; } // Return the result. return(result); }
public object MoveForm(MoveFormRequest request) { // Variables. var result = default(object); var rootId = CoreConstants.System.Root.ToInvariantString(); var parentId = GuidHelper.GetGuid(request.NewParentId); // Catch all errors. try { // Declare list of anonymous type. var savedDescendants = new[] { new { Id = string.Empty, Path = new string[] { } } }.Take(0).ToList(); // Variables. var formId = GuidHelper.GetGuid(request.FormId); var form = Persistence.Retrieve(formId); var parentPath = Entities.Retrieve(parentId).Path; var oldFormPath = form.Path; var oldParentPath = oldFormPath.Take(oldFormPath.Length - 1).ToArray(); var configs = ConFormPersistence.RetrieveChildren(formId); // Move form and configurations. var path = EntityHelper.GetClientPath(Entities.MoveEntity(form, parentPath)); foreach (var config in configs) { var descendantParentPath = config.Path.Take(config.Path.Length - 1); var descendantPathEnd = descendantParentPath.Skip(oldParentPath.Length); var newParentPath = parentPath.Concat(descendantPathEnd).ToArray(); var clientPath = EntityHelper.GetClientPath( Entities.MoveEntity(config, newParentPath)); savedDescendants.Add(new { Id = GuidHelper.GetString(config.Id), Path = clientPath }); } // Success. result = new { Success = true, Id = GuidHelper.GetString(formId), Path = path, Descendants = savedDescendants.ToArray() }; } catch (Exception ex) { // Error. Logger.Error <FormsController>(MoveFormError, ex); result = new { Success = false, Reason = UnhandledError }; } // Return result. return(result); }
public object DuplicateForm(DuplicateFormRequest request) { // Variables. var result = default(object); // Catch all errors. try { // Variables. var oldFormId = GuidHelper.GetGuid(request.FormId); var parentId = GuidHelper.GetGuid(request.ParentId); var oldForm = Persistence.Retrieve(oldFormId); var oldConfigs = ConFormPersistence.RetrieveChildren(oldFormId); var newFormId = Guid.NewGuid(); var layoutMap = new Dictionary <Guid, Guid>(); // Get the new form ID path. var newFormPath = oldForm.Path .Take(oldForm.Path.Length - 1) .Concat(new[] { newFormId }) .ToArray(); // Create the duplicate form. var newForm = new Form() { Id = newFormId, Path = newFormPath, Alias = oldForm.Alias, Name = $"{oldForm.Name} (Copy)", Fields = oldForm.Fields, Handlers = oldForm.Handlers }; // Persist the duplicate form. Persistence.Persist(newForm); // Duplicate the existing layouts (ensuring each gets a new layout ID). var oldLayoutIds = oldConfigs .Select(x => x.LayoutId) .Where(x => x.HasValue) .Select(x => x.Value); foreach (var layoutId in oldLayoutIds) { // Duplicate layout. var oldLayout = LayoutPersistence.Retrieve(layoutId); var newLayout = oldLayout.Duplicate(newFormId); // Persist duplicated layout. LayoutPersistence.Persist(newLayout); // Map old layout ID to new layout ID. layoutMap[layoutId] = newLayout.Id; } // Duplicate form configurations. foreach (var oldConfig in oldConfigs) { // Duplicate the form configuration. var newConfigId = Guid.NewGuid(); var newConfig = new ConfiguredForm() { Id = newConfigId, Path = newFormPath.Concat(new[] { newConfigId }).ToArray(), Name = oldConfig.Name, TemplateId = oldConfig.TemplateId, LayoutId = oldConfig.LayoutId.HasValue ? layoutMap[oldConfig.LayoutId.Value] : default(Guid?) }; // Persist form configuration. ConFormPersistence.Persist(newConfig); } // Success. result = new { Success = true, FormId = GuidHelper.GetString(newFormId), Path = newFormPath }; } catch (Exception ex) { // Error. Logger.Error <FormsController>(ex, DuplicateFormError); result = new { Success = false, Reason = UnhandledError }; } // Return the result. return(result); }