Example #1
0
        public async Task <bool> DeletarAsync(Cli_Cliente modelo)
        {
            try
            {
                modelo.DataExclusao = DateTime.Now;
                _context.Cli_Cliente.Update(modelo);
                //_context.Cli_Cliente.Remove(modelo);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        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!");
                }
            }
        }
        public async Task <IActionResult> Create(Cli_Cliente model)
        {
            try
            {
                ModelState.Remove("Id");
                ModelState.Remove("EnderecoId");
                ModelState.Remove("DataInclusao");
                ModelState.Remove("CidadeId");
                ModelState.Remove("EstadoId");
                ModelState.Remove("Estado.Id");
                ModelState.Remove("Endereco.Id");
                ModelState.Remove("Endereco.CidadeId");
                ModelState.Remove("Endereco.EstadoId");
                ModelState.Remove("Endereco.Cidade.Id");
                ModelState.Remove("Endereco.EstadoId");
                ModelState.Remove("Estado.Nome");
                ModelState.Remove("Endereco.Cidade.CodigoIBGE");

                if (ModelState.IsValid)
                {
                    await Salvar(model);

                    var saved = await _ClienteRepositorio.CriarAsync(model);

                    if (saved != null)
                    {
                        return(RedirectToAction(nameof(Index)));
                    }

                    return(BadRequest());
                }

                TempData["msgDanger"] = string.Join(" | ", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage));

                //ViewData["Endereco"] = _Endereco_Repositorio.GerarSelectList(model.IdEndereco);
                return(View(model));
            }
            catch (Exception ex)
            {
                TempData["msgDanger"] = ex.Message;
                return(View(model));
            }
        }
Example #4
0
        public async Task <Cli_Cliente> EditarAsync(Cli_Cliente modelo)
        {
            try
            {
                if (!ValidarCPF(modelo.CPF))
                {
                    throw new SystemException("Informe um CPF válido");
                }

                _context.Update(modelo);
                await _context.SaveChangesAsync();

                return(modelo);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Example #5
0
        public async Task <Cli_Cliente> CriarAsync(Cli_Cliente modelo)
        {
            try
            {
                if (!ValidarCPF(modelo.CPF))
                {
                    throw new SystemException("Informe um CPF válido");
                }

                modelo.DataInclusao = DateTime.Now;
                _context.Add(modelo);
                await _context.SaveChangesAsync();

                return(modelo);
            }
            catch (Exception e)
            {
                throw e;
            }
        }