コード例 #1
0
        public string ConfirmOrders(List <ShoppingcartDetails> shoppingcar, Order order)//新增訂單
        {
            var shoppingcarRepository  = new ShoppingcartDetailsRepository();
            var productsRepository     = new ProductsRepository();
            var orderRepository        = new OrderRepository();
            var orderDetailsRepository = new OrderDetailsRepository();

            SqlConnection connection = new SqlConnection(sqlstr);

            connection.Open();
            var transactition = connection.BeginTransaction();

            try
            {
                foreach (var item in shoppingcar)//每一件是否都有庫存
                {
                    var stock = productsRepository.CheckStock(item.ProductID, item.Quantity);
                    if (!stock)
                    {
                        return($"{item.ProductID}");
                    }
                }

                orderRepository.Create(order);//新增訂單



                var orderid = orderRepository.FindLastOrderByCustomerID(order.CustomerID);
                foreach (var item in shoppingcar)//新增訂單明細
                {
                    var orderDetails = new OrderDetails()
                    {
                        OrderID   = orderid,
                        ProductID = item.ProductID,
                        Quantity  = item.Quantity
                    };
                    orderDetailsRepository.Create(orderDetails);
                }
                //扣庫存
                foreach (var item in shoppingcar)
                {
                    var product = new Products()
                    {
                        ProductID = item.ProductID
                    };
                    productsRepository.UpdateStockPminus(product, item.Quantity);
                }
                //刪購物車內容
                //先判斷購物車商品數量與訂單商品數量

                shoppingcarRepository.DeleteAllForUser(order.CustomerID);

                transactition.Commit();
            }
            catch (SqlException e)
            {
                transactition.Rollback();
                return(e.Message);
            }
            connection.Close();

            return("OrderSuccess");
        }
コード例 #2
0
        public void DeleteAllForUser(int CustomerID)
        {
            var shoppingcarRepository = new ShoppingcartDetailsRepository();

            shoppingcarRepository.DeleteAllForUser(CustomerID);
        }