Example #1
0
        public void AddToCart(int userId, CartItem item, Transaction transaction = null)
        {
            //get user's cart
            var userCart = GetCart(userId);

            //save the item now
            item.CartId = userCart.Id;
            _cartItemService.Insert(item, transaction);
        }
Example #2
0
        public async Task <ActionResult <CartItem> > AddCart([FromBody] CartItem cartItem)
        {
            var userId = HttpContext.User.Identity.Name;

            var cart = await _cartItemService.Insert(cartItem, userId);

            return(Ok(cart));
        }
        public IActionResult Checkout(Order order)
        {
            var  cartSessionText = HttpContext.Session.GetString(SessionCart);
            Cart cart            = JsonConvert.DeserializeObject <Cart>(cartSessionText);
            var  customer        = _customerService.Get(Convert.ToInt32(User.Identity.AuthenticationType));



            if (User?.Identity?.IsAuthenticated == true && customer != null)
            {
                if (cart.Itens.Count > 0)
                {
                    order.CustomerId = customer.CustomerId;

                    order.Itens     = cart.Itens;
                    order.OrderDate = DateTime.Now;

                    order.PaymentDate = DateTime.Now;
                    //    order.PaymentMethod = Order.EPaymentMethod.Boleto;
                    if (order.PaymentMethod == Order.EPaymentMethod.Boleto)
                    {
                        order.OrderStatus = Order.EStatusOrder.Concluido;
                    }
                    else
                    {
                        order.OrderStatus = Order.EStatusOrder.AguardandoPagamento;
                    }

                    foreach (var item in order.Itens)
                    {
                        var p = _productService.Get(item.Product.ProductId);
                        p.Quantity -= item.Quantity;
                        _productService.Update(p);
                    }

                    order.OrderId = _orderService.Insert(order);

                    order.TicketNumber = 1000000 + order.OrderId;

                    foreach (var item in order.Itens)
                    {
                        item.ProductId = (int)item.Product.ProductId;
                        item.OrderId   = (int)order.OrderId;
                        item.Price     = item.Product.Price;
                        _cartItemService.Insert(item);
                    }


                    ViewBag.Message = "Compra Realizada com sucesso ! NĂºmero do pedido :" + order.TicketNumber;
                    HttpContext.Session.Clear();

                    if (order.PaymentMethod == Order.EPaymentMethod.Boleto)
                    {
                        BoletoViewModel boleto = new BoletoViewModel();
                        boleto.Order          = order;
                        boleto.Order.Customer = customer;

                        return(View("Boleto", boleto));
                    }
                    else if (order.PaymentMethod == Order.EPaymentMethod.PagamentoConta)
                    {
                        return(View("WaitingPayment", customer));
                    }


                    return(View());
                }
                else
                {
                    return(View());
                }
            }
            else
            {
                return(RedirectToAction("Login", "Account"));
            }
        }