コード例 #1
0
ファイル: OrderBo.cs プロジェクト: tranhoangtam/ProjectK8
        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();
                }
            }
        }
コード例 #2
0
ファイル: PaymentBo.cs プロジェクト: tranhoangtam/ProjectK8
        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();
                }
            }
        }
コード例 #3
0
        public IActionResult Edit(int id, ProductModel model, IFormFile fHinh)
        {
            if (id != model.ProductId)
            {
                return(View());
            }
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("loi", "Còn lỗi");
                return(View());
            }
            var fileName = MyTools.UploadFile(fHinh, "products");

            if (!string.IsNullOrEmpty(fileName))
            {
                model.Image = fileName;
            }

            var productCreated = _productBo.Update(model, id);

            return(RedirectToAction("Edit", new { id = productCreated.ProductId }));
        }