Exemplo n.º 1
0
        public ActionResult Create(IFormCollection collection)
        {
            Empresa empresa = null;

            string         cNPJ  = collection.FirstOrDefault(a => a.Key.Equals("cnpj", System.StringComparison.CurrentCultureIgnoreCase)).Value;
            string         nome  = collection.FirstOrDefault(a => a.Key.Equals("nome", System.StringComparison.CurrentCultureIgnoreCase)).Value;
            PorteDaEmpresa porte = Enum.Parse <PorteDaEmpresa>(collection.FirstOrDefault(a => a.Key.Equals("porte", System.StringComparison.CurrentCultureIgnoreCase)).Value);

            try
            {
                empresa = new Empresa(_validatorService, _empresaService, 0, nome, cNPJ, porte);
            }
            catch (RegrasException ex)
            {
                ex.CopiarErrosPara(ModelState);
            }

            if (ModelState.IsValid)
            {
                _empresaService.Add(empresa);
                return(RedirectToAction(nameof(Index)));
            }

            // Observar que para chegar nesta ponto
            // será reexibida a mesma view para que seja possível
            // mostrar os erros do ModelState para o usuário
            return(View(new EmpresaViewModel
            {
                CNPJ = cNPJ,
                Nome = nome,
                Porte = porte
            }));
        }
Exemplo n.º 2
0
        public Empresa(IValidatorService validatorService, IEmpresaService empresaService, int idempresa,
                       string nome, string cnpj, PorteDaEmpresa porte)
        {
            _empresaService = empresaService;
            IdEmpresa       = idempresa;
            Nome            = nome;
            CNPJ            = cnpj;
            Porte           = porte;

            _validatorService = validatorService;
            _empresaService   = empresaService;
            ValidarCNPJ(cnpj);
        }