Exemple #1
0
        public void Armazenar(EmpresaDto dto)
        {
            if (!ValidadorCpfCnpj.CnpjValido(dto.Cnpj))
            {
                _notificationContext.AddNotification("500", "Cnpj inválido.");
                return;
            }

            var empresaExistente = _empresaRepository.ObterPorCnpj(dto.Cnpj);

            if (empresaExistente != null && empresaExistente.Id != dto.Id)
            {
                _notificationContext.AddNotification("500", "Uma empresa com esse Cnpj já foi cadastrada.");

                return;
            }


            if (dto.Id == 0)
            {
                var empresa = new Empresa(dto.Nome, dto.Cnpj, dto.DataFundacao.Value);

                if (!empresa.Validar())
                {
                    _notificationContext.AddNotifications(empresa.ValidationResult);

                    return;
                }

                _empresaRepository.Adicionar(empresa);
            }
            else
            {
                var empresa = _empresaRepository.ObterPorId(dto.Id);

                if (!empresa.Validar())
                {
                    _notificationContext.AddNotifications(empresa.ValidationResult);
                }

                empresa.AlterarNome(dto.Nome);
                empresa.AlterarCnpj(dto.Cnpj);
                empresa.AlterarDataFundacao(dto.DataFundacao.Value);

                _empresaRepository.Atualizar(empresa);
            }
        }
Exemple #2
0
        public void Armazenar(FuncionarioDto dto)
        {
            if (!ValidadorCpfCnpj.CpfValido(dto.Cpf))
            {
                _notificationContext.AddNotification("500", "Cpf inválido.");
                return;
            }

            if (dto.Id == 0)
            {
                var funcionario = new Funcionario(dto.Nome, dto.Cpf, dto.DataContratacao.Value);

                if (!funcionario.Validar())
                {
                    _notificationContext.AddNotifications(funcionario.ValidationResult);

                    return;
                }

                _funcionarioRepository.Adicionar(funcionario);
            }
            else
            {
                var funcionarioExistente = _funcionarioRepository.ObterPorCpf(dto.Cpf);

                if (funcionarioExistente != null && funcionarioExistente.Id != dto.Id)
                {
                    _notificationContext.AddNotification("500", "Um funcionário com este Cpf já foi cadastrado.");

                    return;
                }

                var funcionario = _funcionarioRepository.ObterPorId(dto.Id);

                if (!funcionario.Validar())
                {
                    _notificationContext.AddNotifications(funcionario.ValidationResult);
                }

                funcionario.AlterarNome(dto.Nome);
                funcionario.AlterarCpf(dto.Cpf);
                funcionario.AlterarDataContratacao(dto.DataContratacao.Value);

                _funcionarioRepository.Atualizar(funcionario);
            }
        }