Exemple #1
0
 public void Create(T entity)
 {
     context.Entry(entity).State = EntityState.Added;
 }
Exemple #2
0
        /// <summary>
        /// 建立訂單
        /// </summary>
        /// <param name="httpcontext"></param>
        /// <param name="order"></param>
        /// <param name="ecpayNumber"></param>
        /// <returns></returns>
        public OperationResult CreateOrder(HttpContextBase httpcontext, OrderViewModel order, string ecpayNumber)
        {
            OperationResult operationResult = new OperationResult();
            var             dbContext       = new XbooxLibraryDBContext();

            using (var transaction = dbContext.Database.BeginTransaction())
            {
                try
                {
                    var  orderRepo       = new GeneralRepository <Order>(dbContext);
                    var  orderDetailRepo = new GeneralRepository <OrderDetails>(dbContext);
                    var  cartRepo        = new GeneralRepository <Cart>(dbContext);
                    var  cartItemRepo    = new GeneralRepository <CartItems>(dbContext);
                    var  productRepo     = new GeneralRepository <Product>(dbContext);
                    var  couponRepo      = new GeneralRepository <Coupons>(dbContext);
                    Guid newOrderID      = Guid.NewGuid();
                    var  userId          = httpcontext.User.Identity.GetUserId();
                    // 建立一筆新訂單
                    Order newOrder = new Order()
                    {
                        OrderId          = newOrderID,
                        EcpayOrderNumber = ecpayNumber,
                        UserId           = userId,
                        OrderDate        = DateTime.UtcNow,
                        PurchaserName    = order.PurchaserName,
                        City             = order.City,
                        District         = order.District,
                        Road             = order.Road,
                        PurchaserEmail   = order.PurchaserEmail,
                        PurchaserPhone   = order.PurchaserPhone,
                        Paid             = false,
                        Payment          = order.Payment,
                        Build            = true,
                        Remember         = order.Remember
                    };
                    orderRepo.Create(newOrder);
                    orderRepo.SaveContext();
                    // 先拿會員CartItems 裡資料
                    var cartItems = cartItemRepo.GetAll().Where(item => item.CartId.ToString() == userId).ToList();
                    var cart      = cartRepo.GetFirst(item => item.CartId.ToString() == userId);
                    var Coupon    = couponRepo.GetFirst(item => item.CouponCode == order.Discount);
                    foreach (var item in cartItems)
                    {
                        var products = productRepo.GetAll().Where(pd => pd.ProductId == item.ProductId);
                        foreach (var p in products)
                        {
                            if (p.UnitInStock >= item.Quantity)
                            {
                                p.UnitInStock = p.UnitInStock - item.Quantity;
                                OrderDetails orderDetails = new OrderDetails()
                                {
                                    OrderId   = newOrderID,
                                    ProductId = p.ProductId,
                                    Quantity  = item.Quantity,
                                };
                                if (Coupon != null)
                                {
                                    orderDetails.Discount = Coupon.Id;
                                }
                                orderDetailRepo.Create(orderDetails);
                                item.Quantity = 0;
                                Debug.WriteLine(dbContext.Entry(p).State);
                            }
                            else
                            {
                                break;
                            }
                        }
                        cartItemRepo.Delete(item);
                    }
                    cartRepo.Delete(cart);
                    orderRepo.SaveContext();
                    operationResult.isSuccessful = true;
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    operationResult.isSuccessful = false;
                    operationResult.exception    = ex;
                    transaction.Rollback();
                }
                return(operationResult);
            }
        }