Exemplo n.º 1
0
        public IActionResult Edit(int id)
        {
            var product = _productBo.GetById(id);

            //var pro = _productBo.GetFirstWithInclude(p => p.ProductId == id, p => p.Category, p => p.Supplier, p =>p.OrderDetails);

            ViewBag.Category = new SelectList(_categoryBo.GetAll(), "CategoryId", "CategoryName");
            ViewBag.Supplier = new SelectList(_supplierBo.GetAll(), "SupplierId", "SupplierName");

            return(View(product));
        }
Exemplo n.º 2
0
        public void Payment(List <CartItem> carts, string customerId)
        {
            using (var unitOfWork = NewDbContext())
            {
                unitOfWork.BeginTransaction();

                try
                {
                    var order = new OrderModel
                    {
                        CustomerId    = customerId,
                        OrderDate     = DateTime.Now,
                        OrderStatus   = OrderStatus.Open,
                        PaymentMethod = PaymentMethod.COD
                    };
                    var orderAdded = Insert(order, unitOfWork);

                    foreach (CartItem item in carts)
                    {
                        //tạo chi tiết
                        var orderDetail = new OrderDetailModel
                        {
                            OrderId   = orderAdded.OrderId,
                            ProductId = item.ProductId,
                            Quantity  = item.Quantity,
                            Price     = item.Price
                        };

                        _orderDetailBo.Insert(orderDetail, unitOfWork);

                        //cập nhật đơn hàng
                        //var product = _productBo.GetFirstWithInclude(p => p.ProductId == item.ProductId,
                        //    //params
                        //    p => p.Supplier, p => p.Category);
                        var product = _productBo.GetById(unitOfWork, item.ProductId);
                        product.Quantity -= item.Quantity;

                        _productBo.Update(product, item.ProductId, unitOfWork);
                    }

                    unitOfWork.CommitTransaction();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollbackTransaction();
                }
            }
        }
Exemplo n.º 3
0
        public void Payment(List <CartItem> carts, string customerId)
        {
            var newContext = _serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()
                             .ServiceProvider.GetRequiredService <MyDbContext>();

            using (var unitOfWork = new UnitOfWork(newContext))
            {
                unitOfWork.BeginTransaction();

                try
                {
                    var order = new OrderModel
                    {
                        CustomerId    = customerId,
                        OrderDate     = DateTime.Now,
                        OrderStatus   = OrderStatus.Open,
                        PaymentMethod = PaymentMethod.COD
                    };
                    var orderAdded = _orderBo.Insert(order, unitOfWork);

                    foreach (CartItem item in carts)
                    {
                        //tạo chi tiết
                        var orderDetail = new OrderDetailModel
                        {
                            OrderId   = orderAdded.OrderId,
                            ProductId = item.ProductId,
                            Quantity  = item.Quantity,
                            Price     = item.Price
                        };

                        _orderDetailBo.Insert(orderDetail, unitOfWork);

                        var product = _productBo.GetById(unitOfWork, item.ProductId);
                        product.Quantity -= item.Quantity;

                        _productBo.Update(product, item.ProductId, unitOfWork);
                    }

                    unitOfWork.CommitTransaction();
                }
                catch (Exception ex)
                {
                    unitOfWork.RollbackTransaction();
                }
            }
        }
Exemplo n.º 4
0
        public IActionResult AddToCart(int productId, int quantity)
        {
            var carts    = CartItems;
            var cartItem = carts.SingleOrDefault(p => p.ProductId == productId);

            if (cartItem != null)//đã có
            {
                cartItem.Quantity += quantity;
            }
            else
            {
                var hh = _productBo.GetById(productId);
                cartItem          = _mapper.Map <CartItem>(hh);
                cartItem.Quantity = quantity;

                carts.Add(cartItem);
            }
            HttpContext.Session.Set("GioHang", carts);

            return(RedirectToAction("Index"));
        }