private async Task Salvar(Cli_Cliente model)
        {
            //Pega o Estado e atualiza os objetos
            var estado = await _Estado_Repositorio.ObterAsync(model.Estado.Sigla);

            if (estado == null)
            {
                throw new SystemException("Falha ao buscar o estado");
            }

            model.Estado   = model.Endereco.Estado = estado;
            model.EstadoId = model.Endereco.EstadoId = estado.Id;

            //Pega a Cidade e atualiza os objetos
            var cidade = await _Cidade_Repositorio.ObterAsync(model.Endereco.CodigoIBGE);

            if (cidade == null)
            {
                throw new SystemException("Falha ao buscar a cidade");
            }

            model.Cidade   = model.Endereco.Cidade = cidade;
            model.CidadeId = model.Endereco.Cidade.Id = model.Endereco.CidadeId = cidade.Id;

            //Valida se tem o endereço cadastrado, se não tiver cadastra.
            var temEndereco = await _Endereco_Repositorio.ObterAsync(model.Endereco.CEP);

            if (temEndereco != null)
            {
                model.Endereco = temEndereco;
            }
            else
            {
                var EnderecoIncluir = await _Endereco_Repositorio.CriarAsync(model.Endereco);

                var retNovoEndereco = await _Endereco_Repositorio.ObterAsync(model.Endereco.CEP);

                if (retNovoEndereco != null)
                {
                    model.Endereco = retNovoEndereco;
                }
                else
                {
                    throw new SystemException("Falha ao cadastrar endereço!");
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id)
        {
            var model = await _Estado_Repositorio.ObterAsync(id);

            return(View(model));
        }