public static PizzaDO DataRowToPizzaDO(DataRow row) { PizzaDO pizzaDO = new PizzaDO(); try { pizzaDO.OrderID = row["OrderID"] as long?; pizzaDO.PizzaID = long.Parse(row["PizzaID"].ToString()); pizzaDO.Crust = row["Crust"].ToString(); pizzaDO.Size = byte.Parse(row["Size"].ToString()); pizzaDO.Toppings = row["Toppings"].ToString(); pizzaDO.Sauce = row["Sauce"].ToString(); pizzaDO.Cheese = bool.Parse(row["Cheese"].ToString()); pizzaDO.Price = decimal.Parse(row["Price"].ToString()); pizzaDO.ImagePath = row["ImagePath"].ToString(); pizzaDO.Description = row["Description"].ToString(); } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw exception; } finally { } return(pizzaDO); }
public ActionResult UpdatePizzaInOrder(long ID) { ActionResult response = null; try { PizzaDO pizzaDOtoUpdate = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDOtoUpdate != null) { // This pizza exists in the database. OrderDO pizzaOrderDO = _orderDAO.GetOrderByID((long)pizzaDOtoUpdate.OrderID); if (pizzaOrderDO.UserID == GetSessionUserID() || GetSessionRole() == 1) { // The user is associated with this pizza OR the admin is trying to update the pizza. PizzaPO pizzaPOtoUpdate = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDOtoUpdate); FillPizzaSelectItems(pizzaPOtoUpdate); response = View(pizzaPOtoUpdate); } else { // A regular user tried to update someone elses pizza. Logger.Log("WARNING", "PizzaController", "UpdatePizzaInOrder", "UserID: " + GetSessionUserID() + " tried to update someone else's pizza."); response = RedirectToAction("MyOrders", "Order"); } } else { // Pizza doesn't exist. if (GetSessionRole() == 1) // If the admin is using { TempData["ErrorMessage"] = "That doesn't exist."; RedirectToAction("ViewPendingOrders", "Order"); } else { response = RedirectToAction("MyOrders", "Order"); } } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
/// <summary> /// Attempts to add a pizza to the database. Return a bool, /// true if successfull otherwise false. /// </summary> public bool AddNewPizza(PizzaDO newPizza) { bool isSuccess = false; // This will be are return, for whether or not the // pizza was added to the database. SqlConnection sqlConnection = null; SqlCommand sqlCommand = null; try { // Instantiate the sqlConnection. sqlConnection = new SqlConnection(_dataSource); // Instantiate our SQL command with our Stored Procedure name. sqlCommand = new SqlCommand("CREATE_NEW_PIZZA", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; // Add all of the parameters that the Stored Procedure needs. sqlCommand.Parameters.AddWithValue("@OrderID", (object)newPizza.OrderID ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Crust", newPizza.Crust); sqlCommand.Parameters.AddWithValue("@Size", newPizza.Size); sqlCommand.Parameters.AddWithValue("@Toppings", (object)newPizza.Toppings ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Sauce", (object)newPizza.Sauce ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Cheese", newPizza.Cheese); sqlCommand.Parameters.AddWithValue("@Price", newPizza.Price); sqlCommand.Parameters.AddWithValue("@ImagePath", (object)newPizza.ImagePath ?? DBNull.Value); sqlConnection.Open(); // Check if the Stored Procedure was successfull or not. isSuccess = sqlCommand.ExecuteNonQuery() == 1; // Log that the Stored Procedure was unable to add a new Pizza. if (!isSuccess) { Logger.Log("Warning", "Pizza DAO", "AddNewPizza", "Unable to add a new pizza to the database."); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw exception; } finally { // Manually dispose of any connections or anything that might be using up resources. if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } if (sqlCommand != null) { sqlCommand.Dispose(); } } // Return whether or not the command was successfull. return(isSuccess); }
/// <summary> /// Attempts to update a Pizza in the Pizza Table. /// </summary> public int UpdatePizza(PizzaDO updatedPizza) { int rowsAffected = 0; SqlConnection sqlConnection = null; SqlCommand sqlCommand = null; try { // Instantiate the sqlConnection. sqlConnection = new SqlConnection(_dataSource); sqlCommand = new SqlCommand("UPDATE_PIZZA", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.AddWithValue("@PizzaID", updatedPizza.PizzaID); sqlCommand.Parameters.AddWithValue("@Crust", updatedPizza.Crust); sqlCommand.Parameters.AddWithValue("@Size", updatedPizza.Size); sqlCommand.Parameters.AddWithValue("@Toppings", (object)updatedPizza.Toppings ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Sauce", (object)updatedPizza.Sauce ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Cheese", updatedPizza.Cheese); sqlCommand.Parameters.AddWithValue("@Price", updatedPizza.Price); sqlCommand.Parameters.AddWithValue("@ImagePath", (object)updatedPizza.ImagePath ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Description", (object)updatedPizza.Description ?? DBNull.Value); sqlConnection.Open(); // Capture the number of rows affected after executing the update stored procedure. rowsAffected = sqlCommand.ExecuteNonQuery(); } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw exception; } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } if (sqlCommand != null) { sqlCommand.Dispose(); } // If the Stored procedure updated the Pizza table, then log some information about it. if (rowsAffected > 0) { Logger.Log("INFO", "PizzaDAO", "UpdatePizza", "Updated PizzaID " + updatedPizza.PizzaID + " Rows Affected: " + rowsAffected); } } // Return the number of rows affected. return(rowsAffected); }
/// <summary> /// Attempts to add a Admin created pizza to the database. /// </summary> public bool AddNewPrefabPizza(PizzaDO newPrefabPizza) { bool success = false; SqlConnection sqlConnection = null; SqlCommand sqlCommand = null; try { sqlConnection = new SqlConnection(_dataSource); sqlCommand = new SqlCommand("CREATE_NEW_PREFAB_PIZZA", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.Parameters.AddWithValue("@Crust", newPrefabPizza.Crust); sqlCommand.Parameters.AddWithValue("@Size", newPrefabPizza.Size); sqlCommand.Parameters.AddWithValue("@Toppings", (object)newPrefabPizza.Toppings ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Sauce", (object)newPrefabPizza.Sauce ?? DBNull.Value); sqlCommand.Parameters.AddWithValue("@Cheese", newPrefabPizza.Cheese); sqlCommand.Parameters.AddWithValue("@Price", newPrefabPizza.Price); sqlCommand.Parameters.AddWithValue("@ImagePath", newPrefabPizza.ImagePath); sqlCommand.Parameters.AddWithValue("@Description", (object)newPrefabPizza.Description ?? DBNull.Value); sqlConnection.Open(); success = sqlCommand.ExecuteNonQuery() > 0; if (!success) { Logger.Log("WARNING", "PizzaDAO", "AddNewPrefabPizza", "Unable to add a new prefab pizza to the database."); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw exception; } finally { if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } if (sqlCommand != null) { sqlCommand.Dispose(); } } return(success); }
public ActionResult UpdatePrefabPizza(PizzaPO form) { ActionResult response = null; try { PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(form.PizzaID); if (pizzaDO != null) // If that pizza exists { PizzaPO pizzaPO = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO); if (pizzaPO.OrderID == null) // If this pizza is a prefab pizza. { string imagesPath = "/Content/Images/"; // Path to the images folder. form.Price = form.Price < 4.99M ? 4.99M : form.Price; // If the price is less than 4.99 set the price to 4.99. // If the images path doesn't exist then set the form image to the NoImageAvailable picture. if (!System.IO.File.Exists(Server.MapPath("~/") + form.ImagePath)) { form.ImagePath = imagesPath + "NoImageAvailable.png"; } _pizzaDAO.UpdatePizza(Mapping.PizzaMapper.PizzaPOtoPizzaDO(form)); TempData["SuccessMessage"] = "Pizza was successfully updated."; } } else // The pizza doesn't exist. { TempData["ErrorMessage"] = "That pizza doens't exist."; } response = RedirectToAction("PrefabPizzas", "Pizza"); } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public ActionResult AddPizzaToCart(long ID) { ActionResult response = null; try { PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDO == null) // If that prefab doesn't exist... { // redirect to home. response = RedirectToAction("Index", "Home"); } else { // First check if this pizza is actually a prefab pizza created // by the Admin. Prefabs won't have an OrderID. if (pizzaDO.OrderID != null) { response = RedirectToAction("Index", "Pizza"); } else { // Add the pizza to the cart. List <PizzaPO> cart = (List <PizzaPO>)Session["Cart"]; cart.Add(Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO)); TempData["SuccessMessage"] = "Item added to cart."; response = RedirectToAction("Index", "Pizza"); } } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public static PizzaBO PizzaDOtoPizzaBO(PizzaDO from) { PizzaBO to = new PizzaBO(); to.PizzaID = from.PizzaID; to.OrderID = from.OrderID; to.Cheese = from.Cheese; to.Crust = from.Crust; to.ImagePath = from.ImagePath; to.Price = from.Price; to.Sauce = from.Sauce; to.Size = from.Size; to.Toppings = from.Toppings; to.Description = from.Description; return(to); }
public ActionResult UpdatePrefabPizza(long ID) { ActionResult response = null; try { PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(ID); // Get the pizza by the ID. if (pizzaDO != null) // If a pizza by that Id exists. { PizzaPO pizzaPO = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO); FillPizzaSelectItems(pizzaPO); if (pizzaPO.OrderID == null) // If the pizza is a prefab pizza. { pizzaPO.Description = null; response = View(pizzaPO); } else // It's not a prefab pizza. { TempData["ErrorMessage"] = "That pizza was not a prefab please choose another pizza."; response = RedirectToAction("PrefabPizzas", "Pizza"); } } else // That pizza doesn't exist. { RedirectingPage("The product with ID " + ID + " doesn't exist.", ""); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public ActionResult UpdatePrefabPizza(long ID) { ActionResult response = null; try { PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDO != null) // If a pizza by that Id exists. { PizzaPO pizzaPO = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO); FillPizzaSelectItems(pizzaPO); if (pizzaPO.OrderID == null) { pizzaPO.Description = null; response = View(pizzaPO); } else // It's not a prefab pizza. { // TODO: Show admin the messup. Also log this. } } else { RedirectingPage("The product with ID " + ID + " doesn't exist.", ""); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public ActionResult DeletePrefabPizza(long ID) { ActionResult response = null; try { PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDO != null) // If that pizza exists { PizzaPO existingPizza = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO); if (existingPizza.OrderID == null) // If the pizza is in fact a prefab { _pizzaDAO.DeletePizza(ID); TempData["SuccessMessage"] = "Pizza was successfully deleted"; response = RedirectToAction("PrefabPizzas", "Pizza"); } else // Otherwise, the pizza the Admin is trying to delete is not a prefab pizza. { response = RedirectingPage("That pizza is not a prefab.", "../PrefabPizzas"); } } else // Otherwise, the pizza didn't exist. { response = RedirectToAction("That pizza doesn't exist.", "../PrefabPizzas"); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public ActionResult DeleteFromOrder(long ID) { ActionResult response = null; int rowsAffected = 0; try { // Get the pizza the user is currently trying to delete from the DB. PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDO != null) // If the pizza exists in the DB { PizzaPO pizzaPO = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO); if (pizzaPO.OrderID == null) // If this pizza is a prefab pizza. { // Thats a prefab pizza and that shouldn't be deleted from this action. if (GetSessionRole() == 1) { TempData["ErrorMessage"] = "You must delete that pizza from this page."; response = RedirectToAction("PrefabPizzas", "Pizza"); } else { } } else // Otherwise, the pizza isn't a prefab. { // Get the order that this pizza is associated with the pizza. // Use this later to update the new total for the order. OrderPO orderPO = Mapping .OrderMapper .OrderDOtoOrderPO(_orderDAO.GetOrderByID((long)pizzaPO.OrderID)); if (GetSessionRole() == 1) // If current user is an Admin. { // Delete the pizza from the order. rowsAffected = _pizzaDAO.DeletePizza(ID); } else { // Check to make sure that the current user is associated with the pizza's order. if (GetSessionUserID() != orderPO.UserID) // If the order is not tied to the current user... { Logger.Log("WARNING", "PizzaController", "DeletePizza", "User #" + GetSessionUserID() + " tried to delete someone elses pizza"); response = RedirectingPage("You do not have enough permissions to change a customers order.", "../../"); } else // The user is trying to delete their own pizza. { if (orderPO.Paid) // If the order has already been paid for. { // Send the user back to the Order Details page. TempData["ErrorMessage"] = "The order cannot be changed since it has already been paid for."; response = RedirectToAction("OrderDetails", "Order", new { ID = orderPO.OrderID }); } else { // The order hasn't been paid for yet, so it's oaky to delete the pizza. rowsAffected = _pizzaDAO.DeletePizza(ID); response = RedirectToAction("OrderDetails", "Order", new { ID = orderPO.OrderID }); } } } if (rowsAffected > 0) // If a database call was made and it was successfull. { // Recalculate the total for the order. // Get all of the pizzas associated with this order List <PizzaBO> pizzaBOList = Mapping .PizzaMapper .PizzaDOListToPizzaBOList(_pizzaDAO.GetPizzasByOrderID(orderPO.OrderID)); if (pizzaBOList.Count == 0) // If there are no pizzas tied to this order... { // Delete the order. response = RedirectToAction("DeleteOrder", "Order", new { ID = orderPO.OrderID }); } else { // Calculate the new total decimal newTotal = _pizzaBLO.GetCostOfPizzas(pizzaBOList); // Update the order's total. _orderDAO.UpdateOrderTotal(orderPO.OrderID, newTotal); // Redirect the user to the order details page. TempData["SuccessMessage"] = "Successfully delete the pizza from the order."; response = RedirectToAction("OrderDetails", "Order", new { ID = orderPO.OrderID }); } } } } else { TempData["ErrorMessage"] = "That pizza doesn't exists."; } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public ActionResult UpdatePizzaInOrder(long ID) { ActionResult response = null; try { PizzaDO pizzaDOtoUpdate = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDOtoUpdate != null) { // This pizza exists in the database. // Get the order that the pizza is associated with. OrderPO pizzaOrderPO = Mapping .OrderMapper .OrderDOtoOrderPO( _orderDAO.GetOrderByID((long)pizzaDOtoUpdate.OrderID) ); // If the current user is tied to the Pizza's order OR if the current user is an Admin. if (pizzaOrderPO.UserID == GetSessionUserID() || GetSessionRole() == 1) { // Map the pizza the user is trying to update to a PizzaPO PizzaPO pizzaPOtoUpdate = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDOtoUpdate); if (pizzaOrderPO.Paid) // If the order has already been paid for. { // Redirect the user to the order's details. TempData["ErrorMessage"] = "You cannot update a pizza on an order that has already been paid for."; response = RedirectToAction("OrderDetails", "Order", new { ID = pizzaOrderPO.OrderID }); } else // Otherwise, the pizza can be updated. { FillPizzaSelectItems(pizzaPOtoUpdate); // Pass the PizzaPO to the view. response = View(pizzaPOtoUpdate); } } else { // A regular user tried to update someone elses pizza. Logger.Log("WARNING", "PizzaController", "UpdatePizzaInOrder", "UserID: " + GetSessionUserID() + " tried to update someone else's pizza."); response = RedirectToAction("MyOrders", "Order"); } } else // The pizza doesn't exist. { if (GetSessionRole() == 1) // If the current user is an Admin { TempData["ErrorMessage"] = "That doesn't exist."; RedirectToAction("ViewPendingOrders", "Order"); } else { response = RedirectToAction("MyOrders", "Order"); } } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
public ActionResult CreateOrder(CartPaymentVM form) { ActionResult response = null; if (ModelState.IsValid) { // Make sure at least one payment method is correct. if (!form.PaymentPO.PayWithCash && !ValidCreditCard(form.PaymentPO.CreditCard)) { TempData["PaymentErrorMessage"] = "You must fill out the credit card info."; response = RedirectToAction("Index", "Cart"); } else // Otherwise, a payment method was supplied. { try { // Get the cart from Session. List <PizzaPO> cart = Session["Cart"] as List <PizzaPO>; if (cart.Count == 0) // If the cart is empty.. { // The user shouldn't be doing this. response = RedirectToAction("Index", "Pizza"); } else { // Sets up a variable to check if the user should update their account or not. bool isUserInfoValid = true; if (form.PaymentPO.ForDelivery) // If the user wants the order to be delivered to them. { // Get the current user based of the Session's UserID UserPO currentUser = Mapping.UserMapper.UserDOtoUserPO(_userDAO.GetUserByID(GetSessionUserID())); // Get any invalid info that is required for a delivery order to be placed. List <string> invalidInfo = GetInvalidDeliveryInfo(currentUser); // ** This is a fallback if the AJAX version doesn't work when the user is creating a delivery order. ** if (invalidInfo.Count > 0) // If there is any invalid info { isUserInfoValid = false; // The user has not entered the correct information for a delivery order. string errorMessage = "Some information is required before a delivery order can be submitted: "; errorMessage += string.Join(", ", invalidInfo); if (GetSessionRole() == 2) // If the current user is a driver.. { errorMessage += " Your manager must update your account."; } TempData["ErrorMessage"] = errorMessage; response = RedirectToAction("Update", "Account", new { ID = GetSessionUserID() }); } } if (isUserInfoValid) // If the user's information is correct { // Instantiate a new Order. OrderDO newOrder = new OrderDO(); // Fill some of the order's properties. newOrder.IsDelivery = form.PaymentPO.ForDelivery; newOrder.UserID = GetSessionUserID(); newOrder.Status = "Prepping"; newOrder.OrderDate = DateTime.Now; // Get the total for the order. newOrder.Total = _pizzaBLO.GetCostOfPizzas(Mapping.PizzaMapper.PizzaPOListToPizzaBOList(cart)); if (form.PaymentPO.PayWithCash) { newOrder.Paid = false; } else { newOrder.Paid = true; } // Get the newly created primary key after running the insert command. long createdOrderID = _orderDAO.CreateOrder(newOrder); if (createdOrderID > -1) { // Add each pizza in the cart to the new order. foreach (PizzaPO pizzaPO in cart) { pizzaPO.OrderID = createdOrderID; PizzaDO pizzaDO = Mapping.PizzaMapper.PizzaPOtoPizzaDO(pizzaPO); if (!_pizzaDAO.AddNewPizza(pizzaDO)) { Logger.Log("WARNING", "CartController", "CreateOrder", "Unable to add a pizza from the cart to the database."); } else { } } Session["Cart"] = new List <PizzaPO>(); // Create a new cart. TempData["SuccessMessage"] = "Successfully created the order."; response = RedirectToAction("MyOrders", "Order"); } else // An execption didn't occur but the order wasn't created. { TempData["ErrorMessage"] = "Something happened while creating your order, please try again."; response = RedirectToAction("Index", "Cart"); } } } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } } } else { // If the credit card field was not in a correct format if (!ModelState.IsValidField("PaymentPO.CreditCard")) { TempData["CreditCardError"] = "Invalid credit card number."; } else { } TempData["PaymentErrorMessage"] = "Please fix the errors shown below"; response = RedirectToAction("Index", "Cart"); } return(response); }
public ActionResult CreateOrder(CartPaymentVM form) { ActionResult response = null; if (ModelState.IsValid) { if (!form.PaymentPO.PayWithCash && !ValidCreditCard(form.PaymentPO.CreditCard)) { TempData["PaymentErrorMessage"] = "You must fill out the credit card info."; response = RedirectToAction("Index", "Cart"); } else { try { List <PizzaPO> cart = Session["Cart"] as List <PizzaPO>; if (cart.Count == 0) { response = RedirectToAction("Index", "Pizza"); } else { bool isUserInfoValid = true; if (form.PaymentPO.ForDelivery) { // TODO: Check for null UserPO currentUser = Mapping.UserMapper.UserDOtoUserPO(_userDAO.GetUserByID(GetSessionUserID())); List <string> invalidInfo = GetInvalidDeliveryInfo(currentUser); if (invalidInfo.Count > 0) { isUserInfoValid = false; string errorMessage = "Some information is required before a delivery order can be submitted: "; errorMessage += string.Join(", ", invalidInfo); if (GetSessionRole() == 2) { errorMessage += " Your manager must update your account."; } TempData["ErrorMessage"] = errorMessage; response = RedirectToAction("Update", "Account", new { ID = GetSessionUserID() }); } } if (isUserInfoValid) { OrderDO newOrder = new OrderDO(); newOrder.IsDelivery = form.PaymentPO.ForDelivery; newOrder.UserID = GetSessionUserID(); newOrder.Status = "Prepping"; newOrder.OrderDate = DateTime.Now; newOrder.Total = _pizzaBLO.GetCostOfPizzas(Mapping.PizzaMapper.PizzaPOListToPizzaBOList(cart)); if (form.PaymentPO.PayWithCash) { newOrder.Paid = false; } else { newOrder.Paid = true; } long createdOrderID = _orderDAO.CreateOrder(newOrder); foreach (PizzaPO pizzaPO in cart) { pizzaPO.OrderID = createdOrderID; PizzaDO pizzaDO = Mapping.PizzaMapper.PizzaPOtoPizzaDO(pizzaPO); if (!_pizzaDAO.AddNewPizza(pizzaDO)) { Logger.Log("WARNING", "CartController", "CreateOrder", "Unable to add a pizza from the cart to the database."); } } Session["Cart"] = new List <PizzaPO>(); TempData["SuccessMessage"] = "Successfully created the order."; response = RedirectToAction("MyOrders", "Order"); } } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } } } else { if (!ModelState.IsValidField("PaymentPO.CreditCard")) { TempData["CreditCardError"] = "Invalid credit card number."; } TempData["PaymentErrorMessage"] = "Please fix the errors shown below"; response = RedirectToAction("Index", "Cart"); } return(response); }
public ActionResult DeleteFromOrder(long ID) { ActionResult response = null; int rowsAffected = 0; try { PizzaDO pizzaDO = _pizzaDAO.ViewPizzaByID(ID); if (pizzaDO != null) { PizzaPO pizzaPO = Mapping.PizzaMapper.PizzaDOtoPizzaPO(pizzaDO); if (pizzaPO.OrderID == null) { // Thats a prefab pizza and that shouldn't be deleted from this action. if (GetSessionRole() == 1) { TempData["ErrorMessage"] = "You must delete that pizza from this page."; response = RedirectToAction("PrefabPizzas", "Pizza"); } } else { // Get the order that this pizza is associated with the pizza. // Use this later to update the new total for the order. OrderPO orderPO = Mapping .OrderMapper .OrderDOtoOrderPO(_orderDAO.GetOrderByID((long)pizzaPO.OrderID)); if (GetSessionRole() == 1) { rowsAffected = _pizzaDAO.DeletePizza(ID); } else { // Check to make sure that the current user is associated with the pizza's order. if (GetSessionUserID() == orderPO.UserID) { // The user is deleting their own pizza, so that's okay. rowsAffected = _pizzaDAO.DeletePizza(ID); response = RedirectToAction("OrderDetails", "Order", new { ID = orderPO.OrderID }); } else { Logger.Log("WARNING", "PizzaController", "DeletePizza", "User #" + GetSessionUserID() + " tried to delete someone elses pizza"); response = RedirectingPage("You do not have enough permissions to change a customers order.", "../../"); } } if (rowsAffected > 0) { // Recalculate the total for the order. // Get all of the pizza associated with this order List <PizzaBO> pizzaBOList = Mapping .PizzaMapper .PizzaDOListToPizzaBOList(_pizzaDAO.GetPizzasByOrderID(orderPO.OrderID)); // Calculate the new total decimal newTotal = _pizzaBLO.GetCostOfPizzas(pizzaBOList); // Update the order's total. _orderDAO.UpdateOrderTotal(orderPO.OrderID, newTotal); } } } else { TempData["ErrorMessage"] = "That pizza doesn't exists."; } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); } finally { if (response == null) { response = RedirectToAction("Index", "Home"); } } return(response); }
/// <summary> /// Attempts to find a Pizza by an ID. Returns a filled out PizzaDO if /// the Stored Procedure was able to find a Pizza by the parameter or /// false if the was no Pizza in the database with that ID. /// </summary> public PizzaDO ViewPizzaByID(long pizzaID) { PizzaDO pizzaDO = null; SqlConnection sqlConnection = null; SqlCommand sqlCommand = null; SqlDataAdapter adapter = null; try { // Instantiate the sqlConnection sqlConnection = new SqlConnection(_dataSource); // Instantiate our SQL command with our Stored Procedure name. sqlCommand = new SqlCommand("OBTAIN_PIZZA_BY_ID", sqlConnection); sqlCommand.CommandType = CommandType.StoredProcedure; // Add the PizzaID parameter for the Stored Procedure. sqlCommand.Parameters.AddWithValue("@PizzaID", pizzaID); // Instantiate the adapter. adapter = new SqlDataAdapter(sqlCommand); // Create a new Table to hold the information we recieve from the database. DataTable pizzaTable = new DataTable(); sqlConnection.Open(); // Fill our DataTable with the information we recieved. adapter.Fill(pizzaTable); if (pizzaTable.Rows.Count > 0) { // The sqlCommand should only find one pizza by the pizzaID, // If for some reason the table has more than 1 row, then // log the pizza ID. if (pizzaTable.Rows.Count > 1) { Logger.Log("WARNING", "PizzaDAO", "ViewPizzaByID", "Two pizzas have the same ID number. Is the PizzaID a primary key? " + "PizzaID: " + pizzaID); } pizzaDO = PizzaDataTableMapper.DataRowToPizzaDO(pizzaTable.Rows[0]); } } catch (Exception exception) { Logger.LogExceptionNoRepeats(exception); throw; } finally { // Manually dispose of any connections or anything that might be using up resources. if (sqlConnection != null) { sqlConnection.Close(); sqlConnection.Dispose(); } if (sqlCommand != null) { sqlCommand.Dispose(); } if (adapter != null) { adapter.Dispose(); } } return(pizzaDO); }