AccountController.cs
using System;
using System.Web;
using System.Web.Security;
//PetShop specific references
using PetShop.Model;
using PetShop.BLL;
namespace PetShop.Web.ProcessFlow
{
/// <summary>
/// Acount Process Flow, controls navigation for account events
/// </summary>
public class AccountController {
// Navigation constants
private const string ACCOUNT_KEY = "ACCOUNT_KEY";
private const string URL_DEFAULT = "default.aspx";
private const string URL_SIGNIN = "SignIn.aspx";
private const string URL_ACCOUNTCREATE = "MyAccount.aspx?action=create";
private const string URL_ACCOUNTSIGNIN = "MyAccount.aspx?action=signIn";
private const string URL_ACCOUNTUPDATE = "MyAccount.aspx?action=update";
/// <summary>
/// Default constructor
/// </summary>
public AccountController(){
}
/// <summary>
/// Verify Login process
/// User passes in a user name and password and will be redirected on if successful
/// </summary>
/// <param name="userId">User name the customer is authenticating with</param>
/// <param name="password">Password the customer is using</param>
/// <returns>true if the login is successful</returns>
public bool ProcessLogin(string userId, string password){
// Use the account business logic layer to login
Account account = new Account();
AccountInfo myAccountInfo = account.SignIn(userId, password);
//If login is successful then store the state in session and redirect
if (myAccountInfo != null) {
HttpContext.Current.Session[ACCOUNT_KEY] = myAccountInfo;
// Determine where to redirect the user back too
// If they came in from the home page, take them to a similar page
if (FormsAuthentication.GetRedirectUrl(userId, false).EndsWith(URL_DEFAULT)) {
FormsAuthentication.SetAuthCookie(userId, false);
HttpContext.Current.Response.Redirect(URL_ACCOUNTSIGNIN, true);
}else{
// Take the customer back to where the came from
FormsAuthentication.SetAuthCookie(userId, false);
HttpContext.Current.Response.Redirect(FormsAuthentication.GetRedirectUrl(userId, false), true);
}
return true;
}else {
// Login has failed so return false
return false;
}
}
public bool CreateAccount(AccountInfo newAccountInfo){
try {
// Creata a new business logic tier
Account account = new Account();
// Call the insert method
account.Insert(newAccountInfo);
// Store the data in session state and store the authenticated cookie
HttpContext.Current.Session[ACCOUNT_KEY] = newAccountInfo;
FormsAuthentication.SetAuthCookie(newAccountInfo.UserId, false);
//Finally forward to the welcome page
HttpContext.Current.Response.Redirect(URL_ACCOUNTCREATE, true);
}catch {
return false;
}
return true;
}
/// <summary>
/// A method to process an updated account
/// </summary>
/// <param name="updatedAccountInfo">Updated account information</param>
public void UpdateAccount(AccountInfo updatedAccountInfo){
// Create the business logic tier
Account account = new Account();
// Call the udpate method
account.Update(updatedAccountInfo);
//Store the update info back in session state
HttpContext.Current.Session[ACCOUNT_KEY] = updatedAccountInfo;
//Redirect the user to the my account page
HttpContext.Current.Response.Redirect(URL_ACCOUNTUPDATE, true);
}
/// <summary>
/// Retrieves the account information for a customer who has already logged in
/// The method assume the account information is in session state
/// If it can't find it the function will direct the user to login
/// </summary>
/// <returns>The account info for the currently logged in user</returns>
public AccountInfo GetAccountInfo(bool required){
AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY];
if (myAccount == null){
if(required){
HttpContext.Current.Response.Redirect(URL_SIGNIN, true);
}
return null;
}else{
return myAccount;
}
}
/// <summary>
/// Retrieves favourtie category of a customer if we know who they are
/// The method assume the account information is in session state
/// </summary>
/// <returns>The customers favourite category</returns>
public string GetFavouriteCategory(){
AccountInfo myAccount = (AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY];
if (myAccount != null && myAccount.IsShowFavorites) {
return myAccount.Category;
}else{
return null;
}
}
/// <summary>
/// Method to log the user out of the application
/// When the user logs out there session is cleared and their authentication ticket is reset
/// </summary>
public void LogOut(){
// Clear the authentication ticket
FormsAuthentication.SignOut();
// Clear the contents of their session
HttpContext.Current.Session.Clear();
// Tell the system to drop the session reference so that it does
// not need to be carried around with the user
HttpContext.Current.Session.Abandon();
}
}
}
CartController.cs
using System;
using System.Web;
using System.Web.Security;
// PetShop specific imports
using PetShop.Model;
using PetShop.BLL;
namespace PetShop.Web.ProcessFlow{
/// <summary>
/// Cart Process Flow, controls navigation for cart based events
/// </summary>
public class CartController{
private const string BILLING_KEY = "BILLING_KEY";
private const string SHIPPING_KEY = "SHIPPING_KEY";
private const string ACCOUNT_KEY = "ACCOUNT_KEY";
private const string CART_KEY = "CART_KEY";
private const string CREDITCARD_KEY = "CREDITCARD_KEY";
private const string URL_NOCART = "ShoppingCart.aspx";
public CartController(){}
/// <summary>
/// A method to return the current state of the cart
/// </summary>
/// <param name="create">Specifies whether a cart should be created if one does not exist</param>
/// <returns>Cart object</returns>
public Cart GetCart(bool create){
// Fetch the cart object from session state
Cart myCart = (Cart)HttpContext.Current.Session[CART_KEY];
if ( myCart == null ){
if (create){
myCart = new Cart();
}else{
HttpContext.Current.Server.Transfer(URL_NOCART);
return null;
}
}
return myCart;
}
/// <summary>
/// A method to return the current state of the cart
/// </summary>
/// <param name="create">Specifies whether a cart should be created if one does not exist</param>
/// <returns>Cart object</returns>
public void StoreCart(Cart cart){
// Store the cart object in session state
HttpContext.Current.Session[CART_KEY] = cart;
}
/// <summary>
/// A method to purchase the contents of the cart
/// </summary>
/// <returns>Order object with information about the new Order</returns>
public OrderInfo PurchaseCart(){
// Fetch the cart from session
Cart myCart = (Cart)HttpContext.Current.Session[CART_KEY];
// Make some checks on the cart
if ( ( myCart == null ) || ( myCart.Count==0 ) ) {
HttpContext.Current.Server.Transfer(URL_NOCART);
//HttpContext.Current.Response.Redirect(URL_NOCART, false);
return null;
}else{
// Build up the order
OrderInfo newOrder = new OrderInfo();
newOrder.UserId = ((AccountInfo)HttpContext.Current.Session[ACCOUNT_KEY]).UserId;
newOrder.CreditCard = (CreditCardInfo)HttpContext.Current.Session[CREDITCARD_KEY];
newOrder.BillingAddress = (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
newOrder.ShippingAddress = (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];
newOrder.LineItems = (LineItemInfo[])myCart.GetOrderLineItems().ToArray(typeof(LineItemInfo));
newOrder.OrderTotal = myCart.Total;
newOrder.Date = DateTime.Now;
// Send the order to the middle tier
OrderInsert order = new OrderInsert();
newOrder.OrderId = order.Insert(newOrder);
// clear the session objects used
HttpContext.Current.Session[CART_KEY] = null;
HttpContext.Current.Session[CREDITCARD_KEY] = null;
HttpContext.Current.Session[BILLING_KEY] = null;
HttpContext.Current.Session[SHIPPING_KEY] = null;
return newOrder;
}
}
/// <summary>
/// A method to store credit card state information
/// </summary>
public void StoreCreditCard(CreditCardInfo creditCard){
HttpContext.Current.Session[CREDITCARD_KEY] = creditCard;
}
/// <summary>
/// A method to store the billing address information
/// </summary>
public void StoreBillingAddress(AddressInfo billingAddress){
HttpContext.Current.Session[BILLING_KEY] = billingAddress;
}
/// <summary>
/// A method to store the shipping address information
/// </summary>
public void StoreShippingAddress(AddressInfo shippingAddress){
HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
}
/// <summary>
/// A method to get the billing address information from the state store
/// </summary>
public AddressInfo GetBillingAddress(){
return (AddressInfo)HttpContext.Current.Session[BILLING_KEY];
}
/// <summary>
/// A method to get the shipping address information from the state store
/// </summary>
public AddressInfo GetShippingAddress(){
return (AddressInfo)HttpContext.Current.Session[SHIPPING_KEY];
}
/// <summary>
/// A method to control the flow when 'continuing' an order
/// The customer will have entered creditcard information and some address information
/// </summary>
public void ContinueOrder(bool useBillingAddress){
// Set the billing address depending on what the user has selected
if (useBillingAddress) {
HttpContext.Current.Session[SHIPPING_KEY] = HttpContext.Current.Session[BILLING_KEY];
} else {
//If the user wants to use a different address then take them to the next page
HttpContext.Current.Response.Redirect("OrderShipping.aspx", true);
}
}
/// <summary>
/// A method to control the flow when picking an alternate shipping address
/// </summary>
public void SetAlternateShippingAddress(AddressInfo shippingAddress){
HttpContext.Current.Session[SHIPPING_KEY] = shippingAddress;
}
}
}