Ordering Pizza Application

This was a big challenge. As you’ve probably seen from some of my other posts I’ve been going thru courses over a Learnvisualstudio.net, courtesy of Bob Tabor. He has some challenges built into some of his course tracks and videos. So that’s where this program comes from. I’ve also added some of my little tweaks here and there. So this might seem simple but it took me a few weeks of working with things, testing, and tweaking.

Basically it’s a web based program used to track pizza orders. There are multiple tables in the database that keep track of various items. I wanted every aspect of the program to get any and all information from the database. Having said that you will see multiple entities, dto objects, etc… There are areas I can clean up and optimize the code that I will work on later. Also I’m working on some error logging for a later version. Hope you enjoy it and keep a look out for changes. I’m working on it as time permits.

PizzaProgram PizzaProgram2 PizzaProgram3

PizzaProgramSolution PizzaProgramDbTables PizzaProgramEntities

 
Default.aspx HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PapaBobs.Presentation.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Papa Bob's Pizza</title>
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/jquery-2.1.4.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
</head>
<body>
    <form id="mainForm" runat="server">
    <div class="container">
        <div class="page-header">
            <h1>Papa Bob's Pizza</h1>
            <p class="lead">Pizza To Code By</p>
        </div>
        <br />
        <asp:Label ID="errorLabel" runat="server" CssClass="text-danger input-lg" Visible="False"></asp:Label>
        <div class="form-group">
            <label>Size:</label>
            <br />
                <asp:DropDownList ID="pizzaSizeDropDownList" runat="server" CssClass="form-control" Width="30%" AutoPostBack="True" OnSelectedIndexChanged="pizzaSizeDropDownList_SelectedIndexChanged">
            </asp:DropDownList>
            <br />
            <label>Crust:</label>
                <asp:DropDownList ID="crustDropDownList" runat="server" CssClass="form-control" Width="30%" AutoPostBack="True" OnSelectedIndexChanged="crustDropDownList_SelectedIndexChanged">
            </asp:DropDownList>
            <br />
                <label>Toppings Selection</label>
            <div class="checkbox">
                <asp:CheckBoxList ID="toppingsCheckBoxList" runat="server" CssClass="checkbox-inline" AutoPostBack="True" OnSelectedIndexChanged="toppingsCheckBoxList_SelectedIndexChanged"></asp:CheckBoxList>
            </div>
            <br />
                <h3>Deliver To:</h3>
            <br />
            <label>Name:</label>
            <br />
                <asp:TextBox ID="customerNameTextBox" runat="server" CssClass="form-control" placeholder="Enter Full Name" Width="40%"></asp:TextBox>
            <br />
            <label>Address:</label>
            <br />
                <asp:TextBox ID="customerAddressTextBox" runat="server" CssClass="form-control" placeholder="Enter Address" Width="40%"></asp:TextBox>
            <br />
            <label>Zip:</label>
            <br />
                <asp:TextBox ID="customerZipCodeTextBox" runat="server" CssClass="form-control" placeholder="Enter ZIP Code" Width="40%"></asp:TextBox>
            <br />
            <label>Phone:</label>
            <br />
                <asp:TextBox ID="customerPhoneNumberTextBox" runat="server" CssClass="form-control" placeholder="Enter Phone Number" Width="40%"></asp:TextBox>
            <h3>Payment:</h3>
            <div class="radio">
                <asp:RadioButtonList ID="paymentTypeRadioButtonList" runat="server" CssClass="radio-inline"></asp:RadioButtonList>
                <br />
            </div>
            <div class="btn-default">
                <asp:Button ID="orderButton" runat="server" Text="Order" OnClick="orderButton_Click" CssClass="btn btn-primary btn-lg" />
            </div>
            <br />
            <h4><asp:Label ID="totalCostLabel" runat="server" Text="Total Cost:"></asp:Label>&nbsp;<asp:Label ID="costNumberLabel" runat="server"></asp:Label>
            </h4>
        </div>
    </div>
    </form>
</body>
</html>

 
Default.aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PapaBobs.Presentation
{
    public partial class Default : System.Web.UI.Page
    {
        // Method used to get all of the available crust sizes.
        private void getCrustSizes()
        {
            try
            {
                // Getting the available pizza crust sizes and displaying them in the
                // drop down box.
                List<DTO.PizzaSizeDTO> pizzaCrustSizes = Domain.PapaBobsPizzaManager.GetCrustSizes();
                foreach (DTO.PizzaSizeDTO pizzaCrustSize in pizzaCrustSizes)
                {
                    pizzaSizeDropDownList.Items.Add(String.Format("{0} ({1} - {2:C})",
                        pizzaCrustSize.Name, pizzaCrustSize.Description, pizzaCrustSize.Price));
                }
            }
            catch (Exception ex)
            {
                errorLabel.Text += String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
        }

        // Method used to get all of the available crust types.
        private void getCrustTypes()
        {
            try
            {
                List<DTO.PizzaCrustDTO> pizzaCrustTypes = Domain.PapaBobsPizzaManager.GetCrustTypes();
                foreach (DTO.PizzaCrustDTO pizzaCrustType in pizzaCrustTypes)
                {
                    crustDropDownList.Items.Add(String.Format("{0} ({1:C})",
                        pizzaCrustType.Name, pizzaCrustType.Price));
                }
            }
            catch (Exception ex)
            {
                errorLabel.Text += String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
        }

        // Method used to get all of the available toppings.
        private void getToppings()
        {
            try
            {
                List<DTO.PizzaToppingDTO> pizzaToppings = Domain.PapaBobsPizzaManager.GetToppings();
                foreach (DTO.PizzaToppingDTO pizzaTopping in pizzaToppings)
                {
                    toppingsCheckBoxList.Items.Add(String.Format("{0}",
                            pizzaTopping.Name));
                }
            }
            catch (Exception ex)
            {
                errorLabel.Text += String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
        }

        // Method used to get all of the available payment types.
        private void getPaymentTypes()
        {
            try
            {
                List<DTO.PaymentTypeDTO> availablePaymentTypes = Domain.PapaBobsPizzaManager.GetPaymentTypes();
                foreach (DTO.PaymentTypeDTO availablePaymentType in availablePaymentTypes)
                {
                    paymentTypeRadioButtonList.Items.Add(String.Format("{0}",
                        availablePaymentType.PaymentTypeName));
                }

                // Setting the cash option as the default.
                // This is used to prevent an exception when the order button is clicked.
                paymentTypeRadioButtonList.Items[0].Selected = true;
            }
            catch (Exception ex)
            {
                errorLabel.Text += String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
        }

        // Method used to figure out what crust size the user selected.
        private DTO.PizzaSizeDTO getUsersCrustSize()
        {
            DTO.PizzaSizeDTO selectedCrustSize = new DTO.PizzaSizeDTO();
            try
            {
                selectedCrustSize = Domain.PapaBobsPizzaManager.FindSelectedCrustSize(pizzaSizeDropDownList.SelectedIndex);
            }
            catch(Exception ex)
            {
                errorLabel.Text = String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
            return selectedCrustSize;
        }

        // Method used to figure out what crust type the user selected.
        private DTO.PizzaCrustDTO getUsersCrustType()
        {
            DTO.PizzaCrustDTO selectedCrustType = new DTO.PizzaCrustDTO();
            try
            {
                selectedCrustType = Domain.PapaBobsPizzaManager.FindSelectedCrustType(crustDropDownList.SelectedIndex);
            }
            catch (Exception ex)
            {
                errorLabel.Text = String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
            return selectedCrustType;
        }

        // Method used to figure out what payment type the user selected.
        private DTO.PaymentTypeDTO getUsersPaymentType()
        {
            DTO.PaymentTypeDTO selectedPaymentType = new DTO.PaymentTypeDTO();
            try
            {
                selectedPaymentType = Domain.PapaBobsPizzaManager.FindSelectedPaymentType(paymentTypeRadioButtonList.SelectedIndex);
            }
            catch(Exception ex)
            {
                errorLabel.Text = String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
            return selectedPaymentType;
        }

        // Method used to figure out what toppings the user selected.
        private List<DTO.PizzaToppingDTO> getUsersToppings()
        {
            // Getting a list of toppings from the checkbox list.
            List<ListItem> toppingsList = new List<ListItem>();
            List<DTO.PizzaToppingDTO> selectedToppings = new List<DTO.PizzaToppingDTO>();

            try
            {
                // Gathering a list of items from the checkbox list.
                foreach (ListItem toppingListItem in toppingsCheckBoxList.Items)
                    toppingsList.Add(toppingListItem);

                selectedToppings = Domain.PapaBobsPizzaManager.FindSelectedToppings(toppingsList);
            }
            catch(Exception ex)
            {
                errorLabel.Text = String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
            return selectedToppings;
        }

        private decimal calculatePizzaOptionsCost()
        {
            decimal pizzaSelectedOptionsCost = 0;
            DTO.PizzaSizeDTO selectedCrustSize = new DTO.PizzaSizeDTO();
            DTO.PizzaCrustDTO selectedCrustType = new DTO.PizzaCrustDTO();
            try
            {
                // Getting all of the current selected pizza options.
                List<DTO.PizzaToppingDTO> selectedPizzaToppings = getUsersToppings();
                selectedCrustSize = Domain.PapaBobsPizzaManager.FindSelectedCrustSize(pizzaSizeDropDownList.SelectedIndex);
                selectedCrustType = Domain.PapaBobsPizzaManager.FindSelectedCrustType(crustDropDownList.SelectedIndex);

                // Storing the calculated cost of all the selected pizza options. Then displaying the current price.
                pizzaSelectedOptionsCost = Domain.PapaBobsPizzaManager.CalculatePizzaOptionsCost(selectedCrustSize, selectedCrustType, selectedPizzaToppings);
            }
            catch (Exception ex)
            {
                errorLabel.Text = String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
            return pizzaSelectedOptionsCost;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Setting it so the users browser scroll position is remembered.
            MaintainScrollPositionOnPostBack = true;

            if (!Page.IsPostBack)
            {
                // Getting all of the available crust sizes.
                getCrustSizes();

                // Getting all of the available crust types.
                getCrustTypes();

                // Getting all of the available payment types.
                getPaymentTypes();

                // Getting all of the available toppings.
                getToppings();

                // Setting the costlabel to zero.
                costNumberLabel.Text = String.Format("{0:C}", calculatePizzaOptionsCost());
            }
            else
            {
                // Resetting various controls.
                errorLabel.Text = String.Empty;
                errorLabel.Visible = false;
            }
        }

        protected void orderButton_Click(object sender, EventArgs e)
        {
            // Finding which crust size and types the user selected.
            DTO.PizzaSizeDTO userCrustSizeSelection = getUsersCrustSize();
            DTO.PizzaCrustDTO userCrustTypeSelection = getUsersCrustType();

            // Finding which toppings the user selected.
            List<DTO.PizzaToppingDTO> userPizzaToppings = getUsersToppings();

            // Getting the payment type the user selected.
            DTO.PaymentTypeDTO userPaymentTypeSelection = getUsersPaymentType();

            // Start building the order to submit to the database table.
            try
            {
                DTO.PendingOrderDTO newPendingOrder = new DTO.PendingOrderDTO();
                newPendingOrder.OrderId = Guid.NewGuid();
                newPendingOrder.CrustSize = userCrustSizeSelection.Name;
                newPendingOrder.CrustType = userCrustTypeSelection.Name;
                foreach (DTO.PizzaToppingDTO toppings in userPizzaToppings)
                {
                    newPendingOrder.Toppings += String.Format("{0}<br />", toppings.Name);
                }
                newPendingOrder.Name = customerNameTextBox.Text;
                newPendingOrder.Address = customerAddressTextBox.Text;
                newPendingOrder.ZipCode = customerZipCodeTextBox.Text;
                newPendingOrder.PhoneNumber = customerPhoneNumberTextBox.Text;
                newPendingOrder.PaymentType = userPaymentTypeSelection.PaymentTypeName;
                newPendingOrder.TotalCost = Domain.PapaBobsPizzaManager.CalculateTotalCost(userCrustSizeSelection, 
                    userCrustTypeSelection, userPizzaToppings);
                Domain.PapaBobsPizzaManager.AddPendingOrder(newPendingOrder);
                Response.Redirect("OrderSuccess.aspx");

                // Updating the total cost label with the order amount.
                //totalCostLabel.Text = String.Format("Total Cost: {0:C}", newPendingOrder.TotalCost);
            }
            catch(Exception ex)
            {
                errorLabel.Text = String.Format("<br />{0}", ex.Message);
                errorLabel.Visible = true;
            }
        }

        protected void pizzaSizeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Calculating the pizza options cost when the menu changes.
            costNumberLabel.Text = String.Format("{0:C}", calculatePizzaOptionsCost());
        }

        protected void crustDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Calculating the pizza options cost when the menu changes.
            costNumberLabel.Text = String.Format("{0:C}", calculatePizzaOptionsCost());
        }

        protected void toppingsCheckBoxList_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Calculating the pizza options cost when the menu changes.
            costNumberLabel.Text = String.Format("{0:C}", calculatePizzaOptionsCost());
        }
    }
}

The code behind for the default.aspx page has some neat things done. You will notice that I’m making calls to private methods right when the page loads. This is to populate the crust size and type drop lists along with the toppings checkbox list. Like I said earlier all of this data is pulled from their respective database tables.

One requirement for the challenge was that the total amount be updated whenever the user adds any type of option to their pizza. This is done first on the page load along with the associated event handlers for the dropdown lists and checkbox list. The private method then makes calls to methods at the domain level to find the selected crust size, type, and toppings. These domain level methods make calls to the persistence layer to find these items. Once that is done the text label is updated by calling a domain level method that takes the objects, extracts the price from the object, does some math, and returns a value.

Finally once the user has selected all the pizza options and filled in the required info they select the order button. Taking a quick step back there are checks put in place at the persistence layer to make sure the required info is actually entered. Otherwise an error will be displayed. I’m doing this by taking advantage of passing exceptions thru the application layers starting at the persistence layer. Reason for this is I’ve set almost all of the database table fields to not accept NULL values. The only null value allowed is the notes table fields. The notes function will present itself in later versions. Once all the input data is verified calls are made to the domain layer and then to the persistence layer to access the database, create the database object, and submit the object.

Once the order is completed the user is redirected to an order success page notifying them of a completed order.

 
OrderSuccess.aspx HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="OrderSuccess.aspx.cs" Inherits="PapaBobs.Presentation.Admin.OrderSuccess" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Order Success</title>
    <link href="Content/bootstrap.min.css" rel="stylesheet" />
    <script src="Scripts/jquery-2.1.4.min.js"></script>
    <script src="Scripts/bootstrap.min.js"></script>
</head>
<body>
    <form id="orderSuccessForm" runat="server">
    <div class="container">
        <h1>Order Success</h1>
        <p class="lead">Your Order Was Successful</p>
    </div>
    </form>
</body>
</html>

There isn’t much to the order success page. There’s no methods or logic other than displaying a label to the user after the order is completed. This was a requirement for the challenge with no real detail on how to do it. So I took the “keep it simple” route.

 
ViewPendingOrders.aspx HTML

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewPendingOrders.aspx.cs" Inherits="PapaBobs.Presentation.Admin.ViewPendingOrders" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Pending Orders</title>
    <link href="../Content/bootstrap.min.css" rel="stylesheet" />
    <script src="../Scripts/jquery-2.1.4.min.js"></script>
    <script src="../Scripts/bootstrap.min.js"></script>
</head>
<body>
    <form id="viewPendingOrdersForm" runat="server">
    <div id="mainForm">
        <h1>Pending Orders</h1>
        <asp:GridView ID="pendingOrderGridView" runat="server" AutoGenerateColumns="False" OnRowCommand="pendingOrderGridView_RowCommand" CssClass="table table-hover table-bordered table-responsive" DataKeyNames="OrderId">
            <Columns>
                <asp:BoundField DataField="OrderId" HeaderText="Order ID" Visible="False" />
                <asp:ButtonField ButtonType="Button" CommandName="saveOrder" HeaderText="Commands" Text="Complete Order" >
                <ControlStyle Font-Bold="True" />
                <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                </asp:ButtonField>
                <asp:BoundField DataField="Name" HeaderText="Customer Name" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="Address" HeaderText="Address" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="CrustSize" HeaderText="Crust Size" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="CrustType" HeaderText="Crust Type" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="Toppings" HeaderText="Toppings" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="TotalCost" DataFormatString="{0:C}" HeaderText="Total Cost" />
                <asp:BoundField DataField="OrderNotes" HeaderText="Notes" HtmlEncode="False" HtmlEncodeFormatString="False" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Label ID="resultsLabel" runat="server"></asp:Label>
    </div>
    </form>
    <p>
&nbsp;&nbsp;&nbsp;
    </p>
</body>
</html>

The view pending orders page gives the users the ability to view the current orders that are in the database in a gridview. This gives the user a complete view of what a customer has ordered, options, and price. When they are done with the order they will click the complete button. I’ll explain what all this does in the next section.

 
ViewPendingOrders.aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PapaBobs.Presentation.Admin
{
    public partial class ViewPendingOrders : System.Web.UI.Page
    {
        // Method used to get a list of the pending orders from the database.
        private void refreshGridView()
        {
            try
            {
                // Getting the list of pending orders from the database.
                List<DTO.PendingOrderDTO> pendingOrders = Domain.PapaBobsPizzaManager.GetPendingOrders();

                // Binding the gridview datasource.
                pendingOrderGridView.DataSource = pendingOrders.ToList();
                pendingOrderGridView.DataBind();
            }
            catch(Exception ex)
            {
                resultsLabel.Text = String.Format("<br />{0}", ex.Message);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            refreshGridView();
        }

        protected void pendingOrderGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                // Determining the row number that was clicked on.
                GridViewRow row = pendingOrderGridView.Rows[Convert.ToInt32(e.CommandArgument)];

                // Getting the order ID from the datakey setting on the gridview.
                // This is used because I wanted to hide the OrderID column but still pull the data.
                string orderID = pendingOrderGridView.DataKeys[row.RowIndex].Values["OrderId"].ToString();

                // Converting the order ID to a GUID.
                Guid pendingOrderGuid = Guid.Parse(orderID);

                // Executing the methods to complete the order.
                Domain.PapaBobsPizzaManager.CompletePendingOrder(pendingOrderGuid);
                refreshGridView();
            }
            catch(Exception ex)
            {
                resultsLabel.Text = String.Format("<br />{0}", ex.Message);
            }
        }
    }
}

The back end code for this page is really simple. There a private method that pulls the pending order information from the database table. This method is called upon during the inital page load and when the complete button is selected.

For the complete button I have code working in the gridview row command event handler. This figured out which row complete button was clicked on and pulls the orderID from the hidden gridview field. Then it converts the string value to a Guid and calls methods in the domain layer.

 
ViewSavedOrders.aspx HTML code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ViewSavedOrders.aspx.cs" Inherits="PapaBobs.Presentation.Admin.ViewSavedOrders" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Saved Orders</title>
    <link href="../Content/bootstrap.min.css" rel="stylesheet" />
    <script src="../Scripts/jquery-2.1.4.min.js"></script>
    <script src="../Scripts/bootstrap.min.js"></script>
</head>
<body>
    <form id="viewSavedOrdersForm" runat="server">
    <div id="mainForm">
        <h1>Saved Orders</h1>
        <asp:GridView ID="savedOrderGridView" runat="server" AutoGenerateColumns="False" CssClass="table table-hover table-bordered table-responsive">
            <Columns>
                <asp:BoundField DataField="OrderId" HeaderText="Order ID" />
                <asp:BoundField DataField="Name" HeaderText="Customer Name" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="Address" HeaderText="Address" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="ZipCode" HeaderText="Zip Code" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="CrustSize" HeaderText="Crust Size" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="CrustType" HeaderText="Crust Type" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="Toppings" HeaderText="Toppings" HtmlEncode="False" HtmlEncodeFormatString="False" />
                <asp:BoundField DataField="TotalCost" DataFormatString="{0:C}" HeaderText="Total Cost" />
                <asp:BoundField DataField="OrderNotes" HeaderText="Notes" HtmlEncode="False" HtmlEncodeFormatString="False" />
            </Columns>
        </asp:GridView>
        <br />
        <asp:Label ID="resultsLabel" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>

The view saved orders page is essentially the same page as the view pending orders page. Except the function of this page is to store and view all the orders that have been made.

 
ViewSavedOrders.aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PapaBobs.Presentation.Admin
{
    public partial class ViewSavedOrders : System.Web.UI.Page
    {
        // Method used to get a list of saved orders from the database.
        private void refreshGridView()
        {
            try
            {
                // Getting a list of the saved orders from the database.
                List<DTO.SavedOrderDTO> savedOrders = Domain.PapaBobsPizzaManager.GetSavedOrders();

                // Binding the gridview data source.
                savedOrderGridView.DataSource = savedOrders.ToList();
                savedOrderGridView.DataBind();
            }
            catch(Exception ex)
            {
                resultsLabel.Text = String.Format("<br />{0}", ex.Message);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            refreshGridView();
        }
    }
}

The same thing here with the code behind file. The only method being called upon during page load is a private refreshGridView method. This pulls all the information from the saved orders table in the database.

 
PapaBobsPizzaManager.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace PapaBobs.Domain
{
    public class PapaBobsPizzaManager
    {
        // Method used to populate the crust size drop-down list.
        public static List<DTO.PizzaSizeDTO> GetCrustSizes()
        {
            try
            {
                List<DTO.PizzaSizeDTO> pizzaCrustSizes = Persistence.PapaBobsPizzaRepository.GetCrustSizes();
                return pizzaCrustSizes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to populate the crust type drop down list.
        public static List<DTO.PizzaCrustDTO> GetCrustTypes()
        {
            try
            {
                List<DTO.PizzaCrustDTO> pizzaCrustTypes = Persistence.PapaBobsPizzaRepository.GetCrustTypes();
                return pizzaCrustTypes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to populate the toppings checkbox list.
        public static List<DTO.PizzaToppingDTO> GetToppings()
        {
            try
            {
                List<DTO.PizzaToppingDTO> pizzaToppings = Persistence.PapaBobsPizzaRepository.GetToppings();
                return pizzaToppings;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to populate the payment types bullet list.
        public static List<DTO.PaymentTypeDTO> GetPaymentTypes()
        {
            try
            {
                List<DTO.PaymentTypeDTO> availablePaymentTypes = Persistence.PapaBobsPizzaRepository.GetPaymentTypes();
                return availablePaymentTypes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected crust size.
        public static DTO.PizzaSizeDTO FindSelectedCrustSize(int crustSizeId)
        {
            try
            {
                return Persistence.PapaBobsPizzaRepository.FindSelectedCrustSize(crustSizeId);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected crust type.
        public static DTO.PizzaCrustDTO FindSelectedCrustType(int crustTypeId)
        {
            try
            {
                return Persistence.PapaBobsPizzaRepository.FindSelectedCrustType(crustTypeId);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected payment type.
        public static DTO.PaymentTypeDTO FindSelectedPaymentType(int paymentTypeId)
        {
            try
            {
                return Persistence.PapaBobsPizzaRepository.FindSelectedPaymentType(paymentTypeId);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected toppings.
        public static List<DTO.PizzaToppingDTO> FindSelectedToppings(List<ListItem> toppingsList)
        {
            try
            {
                return Persistence.PapaBobsPizzaRepository.FindSelectedToppings(toppingsList);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to add an order to the pending order database table.
        public static void AddPendingOrder(DTO.PendingOrderDTO newPendingOrder)
        {
            try
            {
                Persistence.PapaBobsPizzaRepository.AddPendingOrder(newPendingOrder);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to get all of the pending orders.
        public static List<DTO.PendingOrderDTO> GetPendingOrders()
        {
            try
            {
                return Persistence.PapaBobsPizzaRepository.GetPendingOrders();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to get all of the saved orders.
        public static List<DTO.SavedOrderDTO> GetSavedOrders()
        {
            try
            {
                return Persistence.PapaBobsPizzaRepository.GetSavedOrders();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used when the Complete Order button is clicked from the ViewPendingOrders.aspx page.
        // The method removes the pending order from the pending order database table 
        // and moves the info to the saved order database table.
        public static void CompletePendingOrder(Guid PendingOrderGuid)
        {
            try
            {
                Persistence.PapaBobsPizzaRepository.CompletePendingOrder(PendingOrderGuid);
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to calculate the selected pizza options cost before the order is placed.
        public static decimal CalculatePizzaOptionsCost(DTO.PizzaSizeDTO selectedCrustSize, DTO.PizzaCrustDTO selectedCrustType, List<DTO.PizzaToppingDTO> selectedToppings)
        {
            try
            {
                // Extract the selection options prices from the method parameters.
                decimal selectedCrustPrice = selectedCrustSize.Price;
                decimal selectedCrustTypePrice = selectedCrustType.Price;
                decimal selectedToppingsPrice = 0.0M;
                foreach (DTO.PizzaToppingDTO toppingPrice in selectedToppings)
                {
                    selectedToppingsPrice += toppingPrice.Price;
                }

                // Calculate the selection options prices.
                decimal pizzaOptionsCost = 0.0M;
                pizzaOptionsCost = selectedCrustPrice + selectedCrustTypePrice + selectedToppingsPrice;

                return pizzaOptionsCost;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to calculate the total cost of the order.
        public static decimal CalculateTotalCost(DTO.PizzaSizeDTO pizzaSize, DTO.PizzaCrustDTO crustType, List<DTO.PizzaToppingDTO> toppings)
        {
            // Variable to hold the cost of the toppings.
            decimal toppingsTotalCost = 0;
            foreach (DTO.PizzaToppingDTO toppingCost in toppings)
            {
                toppingsTotalCost += toppingCost.Price;
            }
            return pizzaSize.Price + crustType.Price + toppingsTotalCost;
        }
    }
}

For now there’s only one class in the domain layer. This class connects the presentation layer to the persistence layer. Meaning that all the database table updates, changes, deletes, lookups, etc.., all flow thru here. If any exceptions are thrown they will float up from the persistence layer, thru the domain layer, and finally to the presentation layer.

There are a couple of methods that take care of calculating the total cost when the order is submitted. And calculating the cost of while the selections are being made in real time. One of the project requirements was that the user must see the total cost of their order before finalizing the order.

 
PaymentTypeDTO.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PapaBobs.DTO
{
    public class PaymentTypeDTO
    {
        public System.Guid PaymentId { get; set; }
        public string PaymentTypeName { get; set; }
        public int PaymentTypeEnumId { get; set; }
    }
}

 
SavedOrderDTO.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PapaBobs.DTO
{
    public class SavedOrderDTO
    {
        public System.Guid OrderId { get; set; }
        public string CrustSize { get; set; }
        public string CrustType { get; set; }
        public string Toppings { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string ZipCode { get; set; }
        public string PhoneNumber { get; set; }
        public string PaymentType { get; set; }
        public decimal TotalCost { get; set; }
        public string OrderNotes { get; set; }
    }
}

 
PizzaCrustDTO.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PapaBobs.DTO
{
    public class PizzaCrustDTO
    {
        public System.Guid ProductId { get; set; }
        public string Name { get; set; }
        public int PizzaCrustEnumId { get; set; }
        public decimal Price { get; set; }
        public string Description { get; set; }
    }
}

 
PizzaSizeDTO.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PapaBobs.DTO
{
    public class PizzaSizeDTO
    {
        public System.Guid ProductId { get; set; }
        public string Name { get; set; }
        public int PizzaSizeEnumId { get; set; }
        public decimal Price { get; set; }
        public string Description { get; set; }
    }
}

 
PizzaToppingDTO.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PapaBobs.DTO
{
    public class PizzaToppingDTO
    {
        public System.Guid ProductId { get; set; }
        public string Name { get; set; }
        public int ToppingEnumId { get; set; }
        public decimal Price { get; set; }
        public string Description { get; set; }
    }
}

 
SavedOrderDTO.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PapaBobs.DTO
{
    public class SavedOrderDTO
    {
        public System.Guid OrderId { get; set; }
        public string CrustSize { get; set; }
        public string CrustType { get; set; }
        public string Toppings { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string ZipCode { get; set; }
        public string PhoneNumber { get; set; }
        public string PaymentType { get; set; }
        public decimal TotalCost { get; set; }
        public string OrderNotes { get; set; }
    }
}

The data transfers objects in this program perform the same tasks as the Inventory database program. Giving the program the ability to create objects for the database without having to call on the Entity objects residing in the presentation layer.

You are seeing multiple DTO objects here because of the different Entity objects created for the different tables in the database. I wanted the ability to pass whatever kind of object I needed to in the program withoug having to call on the persistence methods directly.

 
PapaBobsPizzaRepository.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Web.UI.WebControls;

namespace PapaBobs.Persistence
{
    public class PapaBobsPizzaRepository
    {
        // Method used to populate the crust size drop-down list.
        public static List<DTO.PizzaSizeDTO> GetCrustSizes()
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Creating a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();

                // Creating a list of the pizza sizes stored in the database.
                List<PizzaSize> pizzaSizes = db.PizzaSizes.OrderBy(item => item.PizzaSizeEnumId).ToList();

                // Creating a data transfer list of objects for the pizza sizes.
                List<DTO.PizzaSizeDTO> pizzaCrustSizes = new List<DTO.PizzaSizeDTO>();

                // Iterating thru the pizza sizes, creating DTO objects, and adding them
                // to the DTO list.
                foreach (PizzaSize pizzaSizeItem in pizzaSizes)
                {
                    // Creating the DTO object and adding it the DTO list.
                    DTO.PizzaSizeDTO crustSizeDtoItem = new DTO.PizzaSizeDTO();
                    crustSizeDtoItem.ProductId = pizzaSizeItem.ProductId;
                    crustSizeDtoItem.Name = pizzaSizeItem.Name;
                    crustSizeDtoItem.PizzaSizeEnumId = pizzaSizeItem.PizzaSizeEnumId;
                    crustSizeDtoItem.Price = pizzaSizeItem.Price;
                    crustSizeDtoItem.Description = pizzaSizeItem.Description;
                    pizzaCrustSizes.Add(crustSizeDtoItem);
                }
                // Returning the DTO list.
                return pizzaCrustSizes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to populate the crust type drop down list.
        public static List<DTO.PizzaCrustDTO> GetCrustTypes()
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Creating a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();

                // Creating a list of pizza crust types from the database.
                List<PizzaCrust> pizzaCrusts = db.PizzaCrusts.OrderBy(item => item.PizzaCrustEnumId).ToList();

                // Creating a data transfer list of objects for the pizza crust types.
                List<DTO.PizzaCrustDTO> pizzaCrustTypes = new List<DTO.PizzaCrustDTO>();

                // Iterating thru the pizza crusts, creating a DTO object, and adding them
                // to the DTO list.
                foreach (PizzaCrust pizzaCrustItem in pizzaCrusts)
                {
                    DTO.PizzaCrustDTO crustTypeDtoItem = new DTO.PizzaCrustDTO();
                    crustTypeDtoItem.ProductId = pizzaCrustItem.ProductId;
                    crustTypeDtoItem.Name = pizzaCrustItem.Name;
                    crustTypeDtoItem.PizzaCrustEnumId = pizzaCrustItem.PizzaCrustEnumId;
                    crustTypeDtoItem.Price = pizzaCrustItem.Price;
                    crustTypeDtoItem.Description = pizzaCrustItem.Description;
                    pizzaCrustTypes.Add(crustTypeDtoItem);
                }
                // Returning the DTO list.
                return pizzaCrustTypes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to populate the toppings checkbox list.
        public static List<DTO.PizzaToppingDTO> GetToppings()
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Creating a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();

                // Creating a list of toppings from the database.
                List<PizzaTopping> pizzaToppings = db.PizzaToppings.OrderBy(item => item.ToppingEnumId).ToList();

                // Creating a data transfer list of objects for the available toppings.
                List<DTO.PizzaToppingDTO> availableToppings = new List<DTO.PizzaToppingDTO>();

                // Iterating thru the pizzaToppings list, creating DTO objects, and adding them
                // to the DTO list.
                foreach (PizzaTopping pizzaTopping in pizzaToppings)
                {
                    DTO.PizzaToppingDTO toppingDTOItem = new DTO.PizzaToppingDTO();
                    toppingDTOItem.ProductId = pizzaTopping.ProductId;
                    toppingDTOItem.Name = pizzaTopping.Name;
                    toppingDTOItem.ToppingEnumId = pizzaTopping.ToppingEnumId;
                    toppingDTOItem.Price = pizzaTopping.Price;
                    toppingDTOItem.Description = pizzaTopping.Description;
                    availableToppings.Add(toppingDTOItem);
                }
                // Returning the DTO list.
                return availableToppings;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to populate the payment types bullet list.
        public static List<DTO.PaymentTypeDTO> GetPaymentTypes()
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Creating a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();

                // Creating a list of payment types from the database.
                List<PaymentType> paymentTypes = db.PaymentTypes.OrderBy(item => item.PaymentTypeEnumId).ToList();

                // Creating a data transfer list of object for the available payment types.
                List<DTO.PaymentTypeDTO> availablePaymentTypes = new List<DTO.PaymentTypeDTO>();

                // Iterating thru the pizzaToppings list, creating DTO objects, and adding them
                // to the DTO list
                foreach (PaymentType paymentType in paymentTypes)
                {
                    DTO.PaymentTypeDTO paymentTypeDTOItem = new DTO.PaymentTypeDTO();
                    paymentTypeDTOItem.PaymentId = paymentType.PaymentId;
                    paymentTypeDTOItem.PaymentTypeName = paymentType.PaymentTypeName;
                    paymentTypeDTOItem.PaymentTypeEnumId = paymentType.PaymentTypeEnumId;
                    availablePaymentTypes.Add(paymentTypeDTOItem);
                }
                // Returning the DTO list.
                return availablePaymentTypes;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected crust size from the database.
        public static DTO.PizzaSizeDTO FindSelectedCrustSize(int crustSizeId)
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Getting the database entity and DbSet from said entity to use.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();
                DbSet<PizzaSize> pizzaSizeDbSet = db.PizzaSizes;

                // Creating a DTO object to return.
                DTO.PizzaSizeDTO userSelectedSize = new DTO.PizzaSizeDTO();

                // Searching the DbSet for the selected pizza size based on the crust ID number.
                PizzaSize pizzaSize = pizzaSizeDbSet.SingleOrDefault(item => item.PizzaSizeEnumId == crustSizeId);

                // Building the DTO objects properties.
                userSelectedSize.ProductId = pizzaSize.ProductId;
                userSelectedSize.Name = pizzaSize.Name;
                userSelectedSize.PizzaSizeEnumId = pizzaSize.PizzaSizeEnumId;
                userSelectedSize.Price = pizzaSize.Price;
                userSelectedSize.Description = pizzaSize.Description;

                // Returning the pizza size object.
                return userSelectedSize;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected crust type.
        public static DTO.PizzaCrustDTO FindSelectedCrustType(int crustTypeId)
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Getting the database entity and DbSet from said entity to use.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();
                DbSet<PizzaCrust> pizzaCrustDbSet = db.PizzaCrusts;

                // Creating a DTO object to return.
                DTO.PizzaCrustDTO userSelectedCrustType = new DTO.PizzaCrustDTO();

                // Searching the DbSet for the pizza crust object based on the crust type ID.
                PizzaCrust pizzaCrust = pizzaCrustDbSet.SingleOrDefault(item => item.PizzaCrustEnumId == crustTypeId);

                // Building the DTO objects properties.
                userSelectedCrustType.ProductId = pizzaCrust.ProductId;
                userSelectedCrustType.Name = pizzaCrust.Name;
                userSelectedCrustType.PizzaCrustEnumId = pizzaCrust.PizzaCrustEnumId;
                userSelectedCrustType.Price = pizzaCrust.Price;
                userSelectedCrustType.Description = pizzaCrust.Description;

                // Returning the built object.
                return userSelectedCrustType;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected payment type.
        public static DTO.PaymentTypeDTO FindSelectedPaymentType(int paymentTypeId)
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Getting the database entity and DbSet from said entity to use.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();
                DbSet<PaymentType> paymentTypeDbSet = db.PaymentTypes;

                // Searching the DbSet for the payment type object based on the payment type ID.
                PaymentType paymentType = paymentTypeDbSet.SingleOrDefault(item => item.PaymentTypeEnumId == paymentTypeId);

                // Creating a DTO object to return
                DTO.PaymentTypeDTO userSelectedPaymentType = new DTO.PaymentTypeDTO();

                // Building the DTO object properties.
                userSelectedPaymentType.PaymentId = paymentType.PaymentId;
                userSelectedPaymentType.PaymentTypeName = paymentType.PaymentTypeName;
                userSelectedPaymentType.PaymentTypeEnumId = paymentType.PaymentTypeEnumId;

                // Returning the built object.
                return userSelectedPaymentType;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to find the user selected toppings.
        public static List<DTO.PizzaToppingDTO> FindSelectedToppings(List<ListItem> toppingsList)
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Getting the database entity and DbSet from said entity to use.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();
                DbSet<PizzaTopping> toppingsDbSet = db.PizzaToppings;

                // Creating a DTO toppings list to return.
                List<DTO.PizzaToppingDTO> userSelectedToppings = new List<DTO.PizzaToppingDTO>();

                //Looping thru the ListItem to see which toppings were selected.
                foreach (ListItem toppingListItem in toppingsList)
                {
                    // If the item was selected look for the item in the database
                    // and add it to the DTO list.
                    if (toppingListItem.Selected)
                    {
                        // Find the pizza topping object in the database.
                        int index = toppingsList.IndexOf(toppingListItem);
                        PizzaTopping toppingObjectFromDb = toppingsDbSet.SingleOrDefault(item => item.ToppingEnumId == index);

                        // Build the DTO object and add it to the DTO List.
                        DTO.PizzaToppingDTO userSelectedToppingDTO = new DTO.PizzaToppingDTO();
                        userSelectedToppingDTO.ProductId = toppingObjectFromDb.ProductId;
                        userSelectedToppingDTO.Name = toppingObjectFromDb.Name;
                        userSelectedToppingDTO.ToppingEnumId = toppingObjectFromDb.ToppingEnumId;
                        userSelectedToppingDTO.Price = toppingObjectFromDb.Price;
                        userSelectedToppingDTO.Description = toppingObjectFromDb.Description;
                        userSelectedToppings.Add(userSelectedToppingDTO);
                    }
                }

                // If no toppings were selected add a simple object to display in the gridview
                // notifying of no toppings were selected.
                if (userSelectedToppings.Count == 0)
                {
                    // Create a blank toppings object to add.
                    DTO.PizzaToppingDTO blankObject = new DTO.PizzaToppingDTO();
                    blankObject.Name = String.Format("No Toppings Selected");
                    userSelectedToppings.Add(blankObject);
                }

                // Returning the built object.
                return userSelectedToppings;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to add an order to the pending order database table.
        public static void AddPendingOrder(DTO.PendingOrderDTO newPendingOrder)
        {
            // Getting the database entity and DbSet from said entity to use.
            PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();
            DbSet<PendingOrder> pendingOrderDbSet = db.PendingOrders;

            // Making sure some required fields are filled in.
            if (newPendingOrder.Name.Trim().Length == 0)
                throw new Exception("The Name field must be filled in.");
            if (newPendingOrder.Address.Trim().Length == 0)
                throw new Exception("The Address field must be filled in.");
            if (newPendingOrder.ZipCode.Trim().Length == 0)
                throw new Exception("The Zip Code field must be filled in.");
            if (newPendingOrder.PhoneNumber.Trim().Length == 0)
                throw new Exception("The Phone Number field must be filled in.");

            // Getting the pending order information, building the object,
            // and putting the info into the database.
            PendingOrder pendingOrderItem = new PendingOrder();
            pendingOrderItem.OrderId = newPendingOrder.OrderId;
            pendingOrderItem.CrustSize = newPendingOrder.CrustSize;
            pendingOrderItem.CrustType = newPendingOrder.CrustType;
            pendingOrderItem.Toppings = newPendingOrder.Toppings;
            pendingOrderItem.Name = newPendingOrder.Name;
            pendingOrderItem.Address = newPendingOrder.Address;
            pendingOrderItem.ZipCode = newPendingOrder.ZipCode;
            pendingOrderItem.PhoneNumber = newPendingOrder.PhoneNumber;
            pendingOrderItem.PaymentType = newPendingOrder.PaymentType;
            pendingOrderItem.TotalCost = newPendingOrder.TotalCost;
            pendingOrderItem.OrderNotes = newPendingOrder.OrderNotes;

            // Adding the pending order item to the data table.
            try
            {
                pendingOrderDbSet.Add(pendingOrderItem);
                db.SaveChanges();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to get all of the pending orders.
        public static List<DTO.PendingOrderDTO> GetPendingOrders()
        {
            // Add some logging capabilities to this later.
            try
            {
                // Getting a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();
                
                // Creating a list of pending orders stored in the database.
                List<PendingOrder> databasePendingOrders = db.PendingOrders.ToList();

                // Iterate thru the pending order list from the database, create the DTO object, and add to the DTO list.
                List<DTO.PendingOrderDTO> pendingOrders = new List<DTO.PendingOrderDTO>();
                foreach (PendingOrder pendingOrderItem in databasePendingOrders)
                {
                    DTO.PendingOrderDTO dtoPendingOrder = new DTO.PendingOrderDTO();
                    dtoPendingOrder.OrderId = pendingOrderItem.OrderId;
                    dtoPendingOrder.CrustSize = pendingOrderItem.CrustSize;
                    dtoPendingOrder.CrustType = pendingOrderItem.CrustType;
                    dtoPendingOrder.Toppings = pendingOrderItem.Toppings;
                    dtoPendingOrder.Name = pendingOrderItem.Name;
                    dtoPendingOrder.Address = pendingOrderItem.Address;
                    dtoPendingOrder.ZipCode = pendingOrderItem.ZipCode;
                    dtoPendingOrder.PhoneNumber = pendingOrderItem.PhoneNumber;
                    dtoPendingOrder.PaymentType = pendingOrderItem.PaymentType;
                    dtoPendingOrder.TotalCost = pendingOrderItem.TotalCost;
                    dtoPendingOrder.OrderNotes = pendingOrderItem.OrderNotes;
                    pendingOrders.Add(dtoPendingOrder);
                }
                return pendingOrders;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        // Method used to get all of the saved orders.
        public static List<DTO.SavedOrderDTO> GetSavedOrders()
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Create a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();

                // Create a list of saved orders stored in the database.
                List<SavedOrder> databaseSavedOrders = db.SavedOrders.ToList();

                // Iterate thru the pending order list from the database, create the DTO object, and add to the DTO list.
                List<DTO.SavedOrderDTO> savedOrders = new List<DTO.SavedOrderDTO>();
                foreach (SavedOrder savedOrderItem in databaseSavedOrders)
                {
                    DTO.SavedOrderDTO savedOrderDtoItem = new DTO.SavedOrderDTO();
                    savedOrderDtoItem.OrderId = savedOrderItem.OrderId;
                    savedOrderDtoItem.CrustSize = savedOrderItem.CrustSize;
                    savedOrderDtoItem.CrustType = savedOrderItem.CrustType;
                    savedOrderDtoItem.Toppings = savedOrderItem.Toppings;
                    savedOrderDtoItem.Name = savedOrderItem.Name;
                    savedOrderDtoItem.Address = savedOrderItem.Address;
                    savedOrderDtoItem.ZipCode = savedOrderItem.ZipCode;
                    savedOrderDtoItem.PhoneNumber = savedOrderItem.PhoneNumber;
                    savedOrderDtoItem.PaymentType = savedOrderItem.PaymentType;
                    savedOrderDtoItem.TotalCost = savedOrderItem.TotalCost;
                    savedOrderDtoItem.OrderNotes = savedOrderItem.OrderNotes;
                    savedOrders.Add(savedOrderDtoItem);
                }
                return savedOrders;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // Method used when the Complete Order button is clicked from the ViewPendingOrders.aspx page.
        // The method removes the pending order from the pending order database table 
        // and moves the info to the saved order database table.
        public static void CompletePendingOrder(Guid PendingOrderGuid)
        {
            // Will add some logging capabilities to this later.
            try
            {
                // Getting a database entity to work with.
                PapaBobsPizzaEntities db = new PapaBobsPizzaEntities();

                // Getting the pending and saved order dbsets.
                DbSet<PendingOrder> pendingOrderDbSet = db.PendingOrders;
                DbSet<SavedOrder> savedOrderDbSet = db.SavedOrders;

                // Finding the order to remove from the pending orders dbset.
                PendingOrder orderToRemove = pendingOrderDbSet.Find(PendingOrderGuid);

                // From properties of the orderToRemove build an object and save in the SavedOrder dbset.
                SavedOrder orderToSave = new SavedOrder();
                orderToSave.OrderId = orderToRemove.OrderId;
                orderToSave.CrustSize = orderToRemove.CrustSize;
                orderToSave.CrustType = orderToRemove.CrustType;
                orderToSave.Toppings = orderToRemove.Toppings;
                orderToSave.Name = orderToRemove.Name;
                orderToSave.Address = orderToRemove.Address;
                orderToSave.ZipCode = orderToRemove.ZipCode;
                orderToSave.PhoneNumber = orderToRemove.PhoneNumber;
                orderToSave.PaymentType = orderToRemove.PaymentType;
                orderToSave.TotalCost = orderToRemove.TotalCost;
                orderToSave.OrderNotes = orderToRemove.OrderNotes;
                
                // Save the order in one table and remove it from another.
                savedOrderDbSet.Add(orderToSave);
                pendingOrderDbSet.Remove(orderToRemove);
                db.SaveChanges();
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }
    }
}

Finally the repository class. Just like in the inventory database program this class performs similar operations. Taking care of database calls to view, add, update, and remove table items.

In the future I’ll be adding some logging capability. Still trying to decide to use something like ELMAH or roll something of my own. And tighten up the exception handling based on what might come up. Speaking of exception handling this is the base clas by design. Any errors here will float up to the presentation layer to be displayed. However when I implement logging that will be taken care of either here or in another class. Still deciding on that.

In the next version there are areas that I’ll be consolidating some of the code. There are areas that I’m repeating certain tasks. I just wanted to get a working model done for this challenge. I’ll add this to ongoing versions as I learn things.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s