Exemple #1
0
        public bool Salvar(AgenciaDTO dto)
        {
            if (!EhPermitidoSalvar())
            {
                messageQueue.Add(Resource.Sigim.ErrorMessages.PrivilegiosInsuficientes, TypeMessage.Error);
                return(false);
            }

            if (dto == null)
            {
                throw new ArgumentNullException("dto");
            }

            bool novoItem = false;

            var agencia = agenciaRepository.ObterPeloId(dto.Id, l => l.Banco);

            if (agencia == null)
            {
                agencia  = new Agencia();
                novoItem = true;
            }

            if (!agencia.Id.HasValue)
            {
                agencia.BancoId = dto.BancoId;
            }
            agencia.AgenciaCodigo   = dto.AgenciaCodigo;
            agencia.DVAgencia       = dto.DVAgencia;
            agencia.Nome            = dto.Nome;
            agencia.NomeContato     = dto.NomeContato;
            agencia.TelefoneContato = dto.TelefoneContato;
            agencia.TipoLogradouro  = dto.TipoLogradouro;
            agencia.Logradouro      = dto.Logradouro;
            agencia.Complemento     = dto.Complemento;
            agencia.Numero          = dto.Numero;
            agencia.Cidade          = dto.Cidade;

            if (Validator.IsValid(agencia, out validationErrors))
            {
                try
                {
                    if (novoItem)
                    {
                        agenciaRepository.Inserir(agencia);
                    }
                    else
                    {
                        agenciaRepository.Alterar(agencia);
                    }

                    agenciaRepository.UnitOfWork.Commit();

                    dto.Id = agencia.Id;
                    messageQueue.Add(Resource.Sigim.SuccessMessages.SalvoComSucesso, TypeMessage.Success);
                    return(true);
                }
                catch (Exception exception)
                {
                    QueueExeptionMessages(exception);
                }
            }
            else
            {
                messageQueue.AddRange(validationErrors, TypeMessage.Error);
            }

            return(false);
        }