public void CancelOrder(int UserOrderID) { using (TransactionScope lScope = new TransactionScope()) { using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer()) { Order UserOrder = lContainer.Orders.Find(UserOrderID); //re-add the stock quantities from the order back to the stock UserOrder.ResetStockLevels(); //make use of the message queues here try { //give the customer their money back TransferFundsToCustomer(UserProvider.ReadUserById(UserOrder.Customer.Id).BankAccountNumber, UserOrder.Total ?? 0.0, (UserOrder.Id).ToString(), UserOrder.Customer.Email); } catch (EndpointNotFoundException) { Debug.WriteLine("Bank process not found please switch on for a full refund."); } //soft delete order from order table UserOrder.Deleted = true; //save changes lContainer.SaveChanges(); lScope.Complete(); } } }
public Order ConfirmOrder(Entities.Order pOrder) //checks if the order is possible { using (TransactionScope lScope = new TransactionScope()) { //LoadBookStocks(pOrder); //MarkAppropriateUnchangedAssociations(pOrder); using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer()) { try { pOrder.OrderNumber = Guid.NewGuid(); pOrder.Store = "OnLine"; pOrder.ProcessStatus = 0; // Book objects in pOrder are missing the link to their Stock tuple (and the Stock GUID field) // so fix up the 'books' in the order with well-formed 'books' with 1:1 links to Stock tuples foreach (OrderItem lOrderItem in pOrder.OrderItems) { int bookId = lOrderItem.Book.Id; lOrderItem.Book = lContainer.Books.Where(book => bookId == book.Id).First(); System.Guid stockId = lOrderItem.Book.Stock.Id; lOrderItem.Book.Stock = lContainer.Stocks.Where(stock => stockId == stock.Id).First(); } // confirm the order can be completed and from which warehouses int[][] confirmedOrders = ConfirmOrderWarehouseLogic(pOrder); Debug.WriteLine(pOrder.ProcessStatus); // an error has occured when confirming the order if (confirmedOrders[0][0] == -1) { SendOrderFailedConfirmation(pOrder); pOrder.ProcessStatus = 1; Debug.WriteLine(pOrder.ProcessStatus); return(pOrder); } Debug.WriteLine(pOrder.ProcessStatus); // and update the stock levels try { pOrder.UpdateStockLevels(); } catch (Exception) { pOrder.ProcessStatus = 1; return(pOrder); } // add the modified Order tree to the Container (in Changed state) lContainer.Orders.Add(pOrder); // ask the Bank service to transfer fundss lContainer.SaveChanges(); //TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0, pOrder.OrderNumber.ToString()); try { TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0, pOrder.Id.ToString(), pOrder.Customer.Email); } catch (EndpointNotFoundException) { pOrder.ProcessStatus = 2; return(pOrder); } // and save the order lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { try { SendOrderErrorMessage(pOrder, lException); } catch { Debug.WriteLine("Email process is off. Switch on please in order to maintain communication with BookStore"); } } } } return(pOrder); }