MVC3 how to update stock of multiple items

2019-04-13 10:38发布

问题:

I am following this tutorial http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9

(not exactly same as this)

I am trying to control the stock.

When order created, I want to deduct amount of stock (Stock - count(number of quantity));

I have 3 tables; Order, OrderDetails, and Product.

After creating OrderDetails list, I want to modify the stock of product.

I don't know how to modify stock? Can I use like 'db.OrderDetails.Add(orderDetail)'?

Could you help me? I am giving you some coding.

Please advice me. Thanks!

public int CreateOrder(Order order)
    {
        decimal orderTotal = 0;

        var cartItems = GetCartItems();

        // Iterate over the items in the cart, adding the order details for each
        foreach (var item in cartItems)
        {
            var orderDetail = new OrderDetails
            {
                productId = item.productId,
                orderId = order.orderId,
                unitPrice = item.priceValue,
                rentalPeriod = item.rentalPeriod,
                startDate = item.dateCreated.AddDays(2),
                endDate = item.dateCreated.AddDays(2 + item.rentalPeriod),
                quantity = item.count
            };


            var productStock = new Product
            {   //Is it right coding??
                stock = item.Product.stock - item.count
            };

            // Set the order total of the shopping cart
            orderTotal += (item.count * item.priceValue);

            //Could be we need some coding here for stock control.
            db.OrderDetails.Add(orderDetail);

            db.SaveChanges();
        }

        // Set the order's total to the orderTotal count
        order.total = orderTotal;

        // Save the order
        db.SaveChanges();

        // Empty the shopping cart
        EmptyCart();

        // Return the OrderId as the confirmation number
        return order.orderId;
    }

ViewModels

public class ShoppingCartViewModel
{
    public List<Cart> CartItems { get; set; }
    public decimal CartTotal { get; set; }
}

OrderDetails

public class OrderDetails
{
    [Key] public int orderDetailId { get; set; }
    public int orderId { get; set; }
    public int productId { get; set; }
    public int quantity { get; set; }
    public decimal unitPrice { get; set; }
    public int rentalPeriod { get; set; }
    public DateTime startDate { get; set; }
    public DateTime endDate { get; set; }
    public virtual Product Product { get; set; }
    public virtual Order Order { get; set; }
}

Order

public class Order
{
    rentalDB db = new rentalDB();

    [Key] public int orderId { get; set; }
    public int customerId { get; set; }
    public decimal total { get; set; }
    public virtual Customer Customer { get; set; }
    public List<OrderDetails> OrderDetails { get; set; }
}

Cart

public class Cart
{
    [Key]
    public int recordId { get; set; }
    public string cartId { get; set; }
    public int productId { get; set; }
    public decimal priceValue { get; set; }
    public int count { get; set; }
    public int rentalPeriod { get; set; }
    public DateTime dateCreated { get; set; }
    public virtual Product Product { get; set; }
}

回答1:

This snippet:

var productStock = new Product
        {   //Is it right coding??
            stock = item.Product.stock - item.count
        };

        // Set the order total of the shopping cart
        orderTotal += (item.count * item.priceValue);

        //Could be we need some coding here for stock control.
        db.OrderDetails.Add(orderDetail);

        db.SaveChanges();

To do a stock check before creating a new product, you should probably make sure you have some left, if not, do something about it, something like this:

if (item.Product.stock - item.Count <= 0) 
{
    //you're out of stock! do something about it here? 
}
else
{
    //do your code above to create the new Product
}

Edit: To just adjust the stock for that product, just do the following:

Instead of doing this snipped:

var productStock = new Product
        {   //Is it right coding??
            stock = item.Product.stock - item.count
        };

Change it to:

item.Product.stock = item.Product.stock - item.count;

That'll subtract the ordered number from the Product stock number. Then when you call the following line (which you already do in your code), the changes are saved to the database:

db.SaveChanges();