Implementing the Data Tier

Một phần của tài liệu Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 5 doc (Trang 59 - 62)

Because you have already created stored procedures in the previous chapters, we’ll move a bit quicker this time. You need to add the following stored procedures to the BalloonShop database:

• ShoppingCartAddItem adds a product to a shopping cart.

• ShoppingCartRemoveItem deletes a record from the ShoppingCart table.

• ShoppingCartUpdateItem modifies a shopping cart record.

• ShoppingCartGetItems gets the list of products in the specified shopping cart.

• ShoppingCartGetTotalAmount returns the total cost of the products in the specified product cart.

ShoppingCartAddItem

ShoppingCartAddItem is called when the visitor clicks the Add to Cart button for one of the products. If the selected product already exists in the shopping cart, its quantity is increased by one; if the product doesn’t exist, a new record is added to the shopping cart.

Not surprisingly, the parameters ShoppingCartAddItem receives are CartID and ProductID.

The stored procedure first searches to determine whether the product mentioned (ProductID, CartID) pair exists in the ShoppingCart table. If it does, the stored procedure updates the current product quantity in the shopping cart by adding one unit. Otherwise, the procedure creates a new record for the product in ShoppingCart with a default quantity of 1, but not before checking whether the mentioned @ProductID is valid.

Add the following stored procedure to your BalloonShop database:

CREATE Procedure ShoppingCartAddItem (@CartID char(36),

@ProductID int) AS

IF EXISTS

(SELECT CartID FROM ShoppingCart

WHERE ProductID = @ProductID AND CartID = @CartID) UPDATE ShoppingCart

SET Quantity = Quantity + 1

WHERE ProductID = @ProductID AND CartID = @CartID ELSE

IF EXISTS (SELECT Name FROM Product WHERE ProductID=@ProductID) INSERT INTO ShoppingCart (CartID, ProductID, Quantity, DateAdded) VALUES (@CartID, @ProductID, 1, GETDATE())

You use the GETDATE system function to retrieve the current date and manually populate the DateAdded field, but you could set the GETDATE function as the default value of that field instead.

ShoppingCartRemoveItem

Following is the stored procedure that removes a product from the shopping cart. This happens when the visitor clicks the Remove button for one of the products in the shopping cart. Add the ShoppingCartRemoveItem stored procedure to your BalloonShop database:

CREATE PROCEDURE ShoppingCartRemoveItem (@CartID char(36),

@ProductID int) AS

DELETE FROM ShoppingCart

WHERE CartID = @CartID and ProductID = @ProductID

ShoppingCartUpdateItem

ShoppingCartUpdateItem is used when you want to update the quantity of an existing shopping cart item. This stored procedure receives three values as parameters: @CartID, @ProductID, and

@Quantity.

If @Quantity is 0 or less, ShoppingCartUpdateItem calls ShoppingCartRemoveItem to remove the mentioned product from the shopping cart. Otherwise, it updates the quantity of the product in the shopping cart and updates DateAdded to accurately reflect the time the record was last modified.

Tip Updating the DateAdded field is important because the administrator can remove old shopping carts from the database, and you don’t want to remove carts that were recently updated!

Add the ShoppingCartUpdateItem stored procedure to your BalloonShop database:

CREATE Procedure ShoppingCartUpdateItem (@CartID char(36),

@ProductID int, @Quantity int) AS

IF @Quantity <= 0

EXEC ShoppingCartRemoveItem @CartID, @ProductID ELSE

UPDATE ShoppingCart

SET Quantity = @Quantity, DateAdded = GETDATE() WHERE ProductID = @ProductID AND CartID = @CartID ShoppingCartGetItems

This stored procedure returns the ID, Name, Price, Quantity, and Subtotal for each product in the shopping cart. Because the ShoppingCart table only stores the ProductID for each product it stores, you need to join the ShoppingCart and Product tables to get the information you need.

Add the ShoppingCartGetItems stored procedure to your BalloonShop database:

CREATE PROCEDURE ShoppingCartGetItems (@CartID char(36))

AS

SELECT Product.ProductID, Product.Name, Product.Price, ShoppingCart.Quantity, Product.Price * ShoppingCart.Quantity AS Subtotal

FROM ShoppingCart INNER JOIN Product

ON ShoppingCart.ProductID = Product.ProductID WHERE ShoppingCart.CartID = @CartID

Note Subtotal is a calculated column. It doesn’t exist in any of the tables you joined, but it’s generated using a formula, which in this case is the price of the product multiplied by its quantity. When sending back the results, Subtotal is regarded as a separate column.

ShoppingCartGetTotalAmount

ShoppingCartGetTotalAmount returns the total value of the products in the shopping cart. This is called when displaying the total amount for the shopping cart.

CREATE PROCEDURE ShoppingCartGetTotalAmount (@CartID char(36))

AS

SELECT ISNULL(SUM(Product.Price * ShoppingCart.Quantity), 0) FROM ShoppingCart INNER JOIN Product

ON ShoppingCart.ProductID = Product.ProductID WHERE ShoppingCart.CartID = @CartID

Note The ISNULL method is used to return 0 instead of a NULL total amount (this happens if the shopping cart is empty). You must do this because the business tier expects to receive a numerical value as the amount.

This stored procedure is different from the others in that it returns a single value instead of a result set. In the business tier, you’ll retrieve this value using the ExecuteScalar method of the DbCommand object.

Một phần của tài liệu Beginning ASP.NET 2.0 E-Commerce in C# 2005 From Novice to Professional PHẦN 5 doc (Trang 59 - 62)

Tải bản đầy đủ (PDF)

(70 trang)