public async Task <Pedido> GetPedidoAsync()
        {
            var pedidoId = httpHelper.GetPedidoId();
            var pedido   =
                await dbSet
                .Include(p => p.Itens)
                .Include(p => p.Cadastro)
                .Where(p => p.Id == pedidoId)
                .SingleOrDefaultAsync();

            if (pedido == null)
            {
                var claimsPrincipal = contextAccessor.HttpContext.User;
                var clienteId       = userManager.GetUserId(claimsPrincipal);

                pedido = new Pedido(clienteId);
                await dbSet.AddAsync(pedido);

                await contexto.SaveChangesAsync();

                httpHelper.SetPedidoId(pedido.Id);
            }

            return(pedido);
        }
Esempio n. 2
0
        public async Task <Pedido> GetPedidoAsync()
        {
            var pedidoId = httpHelper.GetPedidoId();
            var pedido   =
                await dbSet
                .Include(p => p.Itens)
                .ThenInclude(i => i.Produto)
                .ThenInclude(prod => prod.Categoria)
                .Include(p => p.Cadastro)
                .Where(p => p.Id == pedidoId)
                .SingleOrDefaultAsync();

            if (pedido == null)
            {
                var claimsPrincipal = contextAccessor.HttpContext.User;
                var clienteId       = claimsPrincipal.FindFirst("sub")?.Value; //subject, ou id do usuário

                pedido = new Pedido(clienteId);
                await dbSet.AddAsync(pedido);

                await contexto.SaveChangesAsync();

                httpHelper.SetPedidoId(pedido.Id);
            }

            return(pedido);
        }
        public async Task <Pedido> GetPedidoAsync()
        {
            var pedidoId = httpHelper.GetPedidoId();
            var pedido   =
                await dbSet
                .Include(p => p.Itens)
                .ThenInclude(i => i.Produto)
                .ThenInclude(prod => prod.Categoria)
                .Include(p => p.Cadastro)
                .Where(p => p.Id == pedidoId)
                .SingleOrDefaultAsync();

            if (pedido == null)
            {
                var claimsPrincipal = contextAccessor.HttpContext.User;
                //var clienteId = userManager.GetUserId(claimsPrincipal);//essa linha não funciona se estiver usando o OpenIdConnect com o IdentityServer
                //usar essa linha ao invés
                var clienteId = claimsPrincipal.FindFirst("sub")?.Value; //subject, ou id do usuario

                pedido = new Pedido(clienteId);
                await dbSet.AddAsync(pedido);

                await contexto.SaveChangesAsync();

                httpHelper.SetPedidoId(pedido.Id);
            }

            return(pedido);
        }
Esempio n. 4
0
        public async Task <Pedido> GetPedidoAsync()
        {
            var pedidoId = httpHelper.GetPedidoId();
            var pedido   =
                await dbSet
                .Include(p => p.Itens)
                .ThenInclude(i => i.Produto)
                .ThenInclude(prod => prod.Categoria)
                .Include(p => p.Cadastro)
                .Where(p => p.Id == pedidoId)
                .SingleOrDefaultAsync();

            if (pedido == null)
            {
                var clienteId = userManager.GetUserId(contextAccessor.HttpContext.User);
                pedido = new Pedido(httpHelper.GetCadastro(), clienteId);
                await dbSet.AddAsync(pedido);

                await context.SaveChangesAsync();

                httpHelper.SetPedidoId(pedido.Id);
            }

            return(pedido);
        }
        public async Task <Pedido> GetPedidoAsync()
        {
            var pedidoId = httpHelper.GetPedidoId();
            var pedido   =
                await dbSet
                .Include(p => p.Itens)
                .ThenInclude(i => i.Produto)
                .ThenInclude(prod => prod.Categoria)
                .Include(p => p.Cadastro)
                .Where(p => p.Id == pedidoId)
                .SingleOrDefaultAsync();

            if (pedido == null)
            {
                pedido = new Pedido();
                await dbSet.AddAsync(pedido);

                await contexto.SaveChangesAsync();

                httpHelper.SetPedidoId(pedido.Id);
            }

            return(pedido);
        }