public void SubmitOrder(Entities.Order pOrder) { using (TransactionScope lScope = new TransactionScope()) { LoadMediaStocks(pOrder); MarkAppropriateUnchangedAssociations(pOrder); using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer()) { try { pOrder.OrderNumber = Guid.NewGuid(); TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0); pOrder.UpdateStockLevels(); PlaceDeliveryForOrder(pOrder); lContainer.Orders.ApplyChanges(pOrder); lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { SendOrderErrorMessage(pOrder, lException); throw; } } } SendOrderPlacedConfirmation(pOrder); }
public void SubmitOrder(Entities.Order pOrder) { using (TransactionScope lScope = new TransactionScope()) using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer()) { try { // Only Request the transfer, delivery will be placed after the trasfer succeed pOrder.OrderNumber = Guid.NewGuid(); // update the stock levels for this order before the fund is transferred, //because the items should be reserved for this user // if there is anything wrong with the fund transfer, the stock levels will be added back pOrder.UpdateStockLevels(); TransferFundsFromCustomer(pOrder.OrderNumber, pOrder.Customer.BankAccountNumber, pOrder.Total ?? 0.0); Console.WriteLine("Fund transfer of order: " + pOrder.OrderNumber + " has been requested to the bank"); lContainer.Orders.ApplyChanges(pOrder); lContainer.SaveChanges(); } catch (Exception lException) { SendTransferErrorEmail(pOrder.OrderNumber, lException.Message); Console.WriteLine("Something wrong happened. The fund transferr request were not able to be placed."); Console.WriteLine("Exception Message:" + lException.Message); throw; } finally { lScope.Complete(); } } }
public void SubmitOrder(Entities.Order pOrder) { using (TransactionScope lScope = new TransactionScope()) { LoadMediaStocks(pOrder); MarkAppropriateUnchangedAssociations(pOrder); using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer()) { try { pOrder.OrderNumber = Guid.NewGuid(); if (pOrder.CheckStockLevels()) { pOrder.UpdateStockLevels(); TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0, pOrder.OrderNumber.ToString()); } else { Console.WriteLine("Insufficient stock"); throw new Exception("Insufficient stock!"); } lContainer.Orders.ApplyChanges(pOrder); lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { Console.WriteLine("Error occured while upload stocks: " + lException.Message); throw; } } } }
public void SubmitOrder(Entities.Order pOrder) { using (TransactionScope lScope = new TransactionScope()) using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer()) { AttachEntitiesToContext(lContainer, pOrder); pOrder.UpdateStockLevels(); lContainer.SaveChanges(); lScope.Complete(); } }
public void SubmitOrder(Entities.Order pOrder) { using (TransactionScope lScope = new TransactionScope()) { LoadMediaStocks(pOrder); MarkAppropriateUnchangedAssociations(pOrder); using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer()) { try { //Specify the binding to be used for the client. NetMsmqBinding binding = new NetMsmqBinding(NetMsmqSecurityMode.None); //Specify the address to be used for the client. EndpointAddress address = new EndpointAddress("net.msmq://localhost/private/PublisherMessageQueue"); pOrder.OrderNumber = Guid.NewGuid(); OrderToOrderMessage lVisitor = new OrderToOrderMessage(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, RetrieveVideoStoreAccountNumber()); lVisitor.Visit(pOrder); PublisherServiceClient lClient = new PublisherServiceClient(binding, address); lClient.Publish(lVisitor.Result); //TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0); pOrder.UpdateStockLevels(); pOrder.OrderStatus = "unpayed"; //PlaceDeliveryForOrder(pOrder); lContainer.Orders.ApplyChanges(pOrder); lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { // SendOrderErrorMessage(pOrder, lException); // throw; } } } }
public void AfterTransferResultReturns(Boolean Success, Guid pOrderNumber, String pMsg) {//continue delivery and emailing after received bank's transfer message using (TransactionScope lScope = new TransactionScope()) { using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer()) { Entities.Order pOrder = lContainer.Orders.Include("Delivery").Include("OrderItems.Media").Include("Customer") .Where((pOrder1) => pOrder1.OrderNumber == pOrderNumber).FirstOrDefault(); foreach (Entities.OrderItem oi in pOrder.OrderItems) { Entities.Media media = oi.Media; Entities.Stock stock = lContainer.Stocks.Where((p) => p.Media.Id == media.Id).FirstOrDefault(); media.Stocks = stock; } if (Success) { pOrder.UpdateStockLevels(); PlaceDeliveryForOrder(pOrder); lContainer.Orders.ApplyChanges(pOrder); lContainer.SaveChanges(); } else { Common.Model.SendEmailMessage emailMessage = new Common.Model.SendEmailMessage() { Topic = "Email", Message = pMsg + " Order number: " + pOrderNumber + ".", ToAddresses = pOrder.Customer.Email, Date = new DateTime() }; PublisherServiceClient lClient = new PublisherServiceClient(); lClient.Publish(emailMessage); } lScope.Complete(); } } }
public void SubmitOrder(Entities.Order pOrder) { using (TransactionScope lScope = new TransactionScope()) using (VideoStoreEntityModelContainer lContainer = new VideoStoreEntityModelContainer()) { try { pOrder.OrderNumber = Guid.NewGuid(); pOrder.UpdateStockLevels(); lContainer.Orders.ApplyChanges(pOrder); TransferFundsFromCustomer(pOrder.Customer.BankAccountNumber, pOrder.Total ?? 0.0, pOrder.OrderNumber); lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { SendOrderErrorMessage(pOrder, lException); throw; } } }
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); }
public string SubmitOrder(Entities.Order pOrder) { string lCustomerEmail = ""; Guid lOrderId = Guid.Empty; using (TransactionScope lScope = new TransactionScope()) { //LoadBookStocks(pOrder); //MarkAppropriateUnchangedAssociations(pOrder); string lResult = ""; using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer()) { try { lContainer.Users.Attach(pOrder.Customer); pOrder.OrderNumber = Guid.NewGuid(); pOrder.Store = "OnLine"; lCustomerEmail = pOrder.Customer.Email; lOrderId = pOrder.OrderNumber; // 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(); lOrderItem.Book.Stocks = lContainer.Stocks.Where(stock => bookId == stock.Book.Id).ToList <Stock>(); } List <Warehouse> bestWares = LoadOptimalWarehouseStocks(pOrder); // cannot satisfy if (bestWares.Count == 0) { throw new Exception("Insufficient stock"); } // and update the stock levels List <Tuple <Stock, OrderItem, int> > lConsumedStocks = pOrder.UpdateStockLevels(bestWares, lContainer); // record the stocks that have been consumed RecordPurchasedBooksFromStocks(lConsumedStocks, lContainer); // add the modified Order tree to the Container (in Changed state) lContainer.Orders.Add(pOrder); // ask the Bank service to transfer fundss lResult = TransferFundsFromCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0); if (!lResult.Equals("Transfer Success")) { // Email the user about the cause of error through this exception throw new Exception(lResult); } Console.WriteLine("=============Order Submit============="); Console.WriteLine("Order ID: " + pOrder.Id); Console.WriteLine("Status: SUCCESS"); Console.WriteLine("Time: " + DateTime.Now); Console.WriteLine("======================================"); Console.WriteLine(" "); // and save the order lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { Console.WriteLine("=============Order Submit============="); Console.WriteLine("Order ID: " + pOrder.Id); Console.WriteLine("Status: FAILED"); Console.WriteLine("Time: " + DateTime.Now); Console.WriteLine("======================================"); Console.WriteLine(" "); // need to rollback bank transfer if the transfer happened if (lResult == "Transfer Success") { Console.WriteLine("=============Calling BANK - Rollback============="); Console.WriteLine("Intiating ROLLBACK on bank trasnfer"); Console.WriteLine("Order ID: " + pOrder.Id); Console.WriteLine("Acc Number: " + UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber); Console.WriteLine("Total: " + pOrder.Total); Console.WriteLine("Time: " + DateTime.Now); Console.WriteLine("======================================"); Console.WriteLine(" "); TransferFundsToCustomer(UserProvider.ReadUserById(pOrder.Customer.Id).BankAccountNumber, pOrder.Total ?? 0.0); } SendOrderErrorMessage(pOrder, lException); IEnumerable <System.Data.Entity.Infrastructure.DbEntityEntry> entries = lContainer.ChangeTracker.Entries(); return(lResult); } } } SendOrderConfirmedConfirmation(pOrder); // User has 20 seconds to cancel the order Thread.Sleep(20000); using (TransactionScope lScope = new TransactionScope()) using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer()) { try { Order lOrder = lContainer.Orders.Where(o => o.Id == pOrder.Id).FirstOrDefault <Order>(); if (lOrder == null) { throw new Exception("Cannot place order as your order was cancelled"); } CreateDelivery(lOrder); lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { SendOrderDeletedErrorMessage(lCustomerEmail, lOrderId); return("Order Failed"); } } // 5 seconds later the order will be scheduled for delivery Thread.Sleep(5000); using (TransactionScope lScope = new TransactionScope()) using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer()) { try { Order lOrder = lContainer.Orders.Where(o => o.Id == pOrder.Id).FirstOrDefault <Order>(); if (lOrder == null) { throw new Exception("Cannot place order as your order was cancelled"); } Delivery lDelivery = lContainer.Deliveries.Where(d => d.Order.Id == lOrder.Id).FirstOrDefault <Delivery>(); PlaceDeliveryForOrder(lOrder, lDelivery); lContainer.SaveChanges(); lScope.Complete(); } catch (Exception lException) { SendOrderDeletedErrorMessage(lCustomerEmail, lOrderId); return("Order Failed"); } } SendOrderPlacedConfirmation(pOrder); return("Order Submitted"); }