Пример #1
0
        public void Deve_Criar_Campos_Do_Formulario()
        {
            for (int i = 0; i < 9; i++)
            {
                var field = new CustomField($"Campo {i + 1}", $"Campo {i + 1} descricao", (EFieldType)i, _customForm.Id, true, _tenant.Id);
                _customForm.AddField(field);

                Assert.IsTrue(field.Valid);
                Assert.AreEqual(field.EntityStatus, EntityStatus.Activated);
                Assert.AreEqual(_customForm.Id, field.CustomFormId);
                Assert.AreEqual(_tenant.Id, field.TenantId);
            }
            Assert.IsTrue(_customForm.Valid);
            Assert.AreEqual(9, _customForm.Fields.Count);
        }
Пример #2
0
        public ICommandResult Handle(CreateCustomFormRequest command)
        {
            if (!command.IsValid())
            {
                return(new CommandResult(false, "Request inválida", command));
            }

            var customForm = new CustomForm(command.Name, _tenant.Id);

            AddNotifications(customForm.Notifications);

            if (command.Fields != null)
            {
                foreach (var fieldCommand in command.Fields)
                {
                    var customField = new CustomField(fieldCommand.Name, fieldCommand?.Description, fieldCommand.Type, customForm.Id, fieldCommand.Mandatory, _tenant.Id);

                    AddNotifications(customField.Notifications);

                    if (customField.HasOptions())
                    {
                        if (fieldCommand.Options == null || fieldCommand.Options.Count == 0)
                        {
                            AddNotification("Type", $"Campo {fieldCommand.Name} deve conter opções.");
                        }
                    }

                    if (fieldCommand.Options != null)
                    {
                        foreach (var optionCommand in fieldCommand.Options)
                        {
                            var customFieldOption =
                                new CustomFieldOption(optionCommand.Name, customField.Id, _tenant.Id);

                            AddNotifications(customFieldOption.Notifications);

                            customField.AddOption(customFieldOption);
                        }
                    }

                    customForm.AddField(customField);
                }
            }
            else
            {
                AddNotification("Fields", "Os campos do formulário não foram inseridos.");
            }

            if (Valid)
            {
                _customFormRepository.Save(customForm);

                if (command.PageComponentId.HasValue)
                {
                    var formPageComponent = new FormPageComponent(command.PageComponentId.Value, customForm.Id, _tenant.Id);

                    AddNotifications(formPageComponent.Notifications);

                    _formPageComponentRepository.Save(formPageComponent);
                }
            }

            if (Valid)
            {
                return(new CommandResult(true, "Formulário cadastrado com sucesso", new { Id = customForm.Id, Name = customForm.Name }));
            }
            else
            {
                return(new CommandResult(false, "Erro ao cadastrar formulário", Notifications));
            }
        }