示例#1
0
        public async Task <IActionResult> CalcularFrete([FromRoute] string cepDestino)
        {
            var produtos = await _carrinhoCompras.TodosItemsCarrinho();

            Frete frete = _cookieFrete.Consultar().Where(a => a.CEP == Convert.ToInt32(cepDestino) && a.CodCarrinho == StringMD5.GerarHash(produtos)).FirstOrDefault();

            if (frete != null)
            {
                return(Ok(frete));
            }
            //await _memoryCache.GetOrCreate("Itens",async () => await _carrinhoCompras.TodosItemsCarrinho());
            List <Pacote> pacotes = _calcularPacote.CalcularPacotesDeProdutos(produtos);

            ValorPrazoFrete valorPAC = await _wscorreios.CalcularFrete(cepDestino, TipoFreteConstant.PAC, pacotes);

            ValorPrazoFrete valorSEDEX = await _wscorreios.CalcularFrete(cepDestino, TipoFreteConstant.SEDEX, pacotes);

            ValorPrazoFrete valorSEDEX10 = await _wscorreios.CalcularFrete(cepDestino, TipoFreteConstant.SEDEX10, pacotes);

            List <ValorPrazoFrete> lista = new List <ValorPrazoFrete>();

            if (valorPAC != null)
            {
                lista.Add(valorPAC);
            }
            if (valorSEDEX != null)
            {
                lista.Add(valorSEDEX);
            }
            if (valorSEDEX10 != null)
            {
                lista.Add(valorSEDEX10);
            }

            frete = new Frete()
            {
                CEP          = Convert.ToInt32(cepDestino),
                CodCarrinho  = StringMD5.GerarHash(produtos),
                ListaValores = lista
            };
            _cookieFrete.Cadastrar(frete);

            return(Ok(frete));
        }
        public async Task <IActionResult> CalcularFrete(int cepDestino)
        {
            try
            {
                List <ProdutoItem> produtos = CarregarProdutoDB();

                List <Pacote> pacotes = _calcularPacote.CalcularPacotesDeProdutos(produtos);

                ValorPrazoFrete valorPAC = await _wscorreios.CalcularFrete(cepDestino.ToString(), TipoFreteConstant.PAC, pacotes);

                ValorPrazoFrete valorSEDEX = await _wscorreios.CalcularFrete(cepDestino.ToString(), TipoFreteConstant.SEDEX, pacotes);

                ValorPrazoFrete valorSEDEX10 = await _wscorreios.CalcularFrete(cepDestino.ToString(), TipoFreteConstant.SEDEX10, pacotes);

                List <ValorPrazoFrete> lista = new List <ValorPrazoFrete>();
                if (valorPAC != null)
                {
                    lista.Add(valorPAC);
                }
                if (valorSEDEX != null)
                {
                    lista.Add(valorSEDEX);
                }
                if (valorSEDEX10 != null)
                {
                    lista.Add(valorSEDEX10);
                }

                _cookieValorPrazoFrete.Cadastrar(lista);

                return(Ok(lista));
            }
            catch (Exception e)
            {
                _cookieValorPrazoFrete.Remover();

                return(BadRequest(e));
            }
        }