public void CheckStockTest()
        {
            ProductsRepository products = new ProductsRepository();
            var list = products.CheckStock(2, 2);

            Assert.IsTrue(list == true);
        }
Beispiel #2
0
        public bool AddProducttoShoppingcar(ShoppingcartDetails model)
        {
            var shoppingcarRepository = new ShoppingcartDetailsRepository();
            var productsRepository    = new ProductsRepository();
            var stock = productsRepository.CheckStock(model.ProductID, model.Quantity);

            if (stock)
            {
                var product = shoppingcarRepository.FindById(model.CustomerID, model.ProductID);
                if (product == null)
                {
                    shoppingcarRepository.Create(model);
                }
                else
                {
                    shoppingcarRepository.Update(model);
                }

                return(true);
            }
            else
            {
                return(false);
            }
        }
 public bool CheckStock(int productid, int carquantity)
 {
     return(repository.CheckStock(productid, carquantity));
 }
Beispiel #4
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");
        }