Пример #1
0
        private void RestoreStock(List <OrderItem> orderItems, BookStoreEntityModelContainer pContainer)
        {
            List <int> orderIds = orderItems.Select(o => o.Id).ToList <int>();

            List <OrderStock> orderStocks = (from OrderStock1 in pContainer.OrderStocks.Include("Stock").Include("OrderItem")
                                             where orderIds.Contains(OrderStock1.OrderItem.Id)
                                             select OrderStock1).ToList <OrderStock>();

            Console.WriteLine("================Stock Restore================");
            Console.WriteLine("Order items:");
            foreach (OrderStock orderStock in orderStocks)
            {
                Stock stock = pContainer.Stocks.SingleOrDefault(r => r.Id == orderStock.Stock.Id);

                // the item was not chosen
                if (stock == null)
                {
                    continue;
                }

                Console.WriteLine("         " + orderStock.OrderItem.Book.Title + ": Quantity: " + orderStock.Quantity);

                stock.Quantity = stock.Quantity.Value + orderStock.Quantity;
                pContainer.Entry(orderStock).State = System.Data.Entity.EntityState.Deleted;
                pContainer.OrderStocks.Remove(orderStock);
                pContainer.Stocks.Attach(stock);
                pContainer.Entry(stock).Property(x => x.Quantity).IsModified = true;
            }
            Console.WriteLine("Time: " + DateTime.Now);
            Console.WriteLine("=============================================");
            Console.WriteLine(" ");
        }
Пример #2
0
 public void UpdateUser(User pUser)
 {
     using (TransactionScope lScope = new TransactionScope())
         using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer())
         {
             lContainer.Entry(pUser).State = EntityState.Modified;
             lContainer.SaveChanges();
             lScope.Complete();
         }
 }
Пример #3
0
        public void CancelOrder(int pOrderId)
        {
            String customerEmail = "";
            Guid   orderNumber   = Guid.Empty;

            using (TransactionScope lScope = new TransactionScope())
            {
                //LoadBookStocks(pOrder);
                //MarkAppropriateUnchangedAssociations(pOrder);

                using (BookStoreEntityModelContainer lContainer = new BookStoreEntityModelContainer())
                {
                    Order  lOrder = lContainer.Orders.FirstOrDefault(o => o.Id == pOrderId);
                    string result = "";
                    try
                    {
                        if (lOrder == null)
                        {
                            throw new OrderDoesNotExistException();
                        }
                        if (lOrder.Delivery != null)
                        {
                            throw new OrderHasAlreadyBeenDeliveredException();
                        }

                        customerEmail = lOrder.Customer.Email;
                        orderNumber   = lOrder.OrderNumber;

                        List <OrderItem> orderItems = lOrder.OrderItems.ToList <OrderItem>();

                        Console.WriteLine("================Cancel Order=================");
                        Console.WriteLine("             Order ID: " + pOrderId);
                        Console.WriteLine("             CustomerID: " + lOrder.Customer.Id);
                        Console.WriteLine("             Name: " + lOrder.Customer.Name);
                        Console.WriteLine("             Address: " + lOrder.Customer.Address);
                        Console.WriteLine("             Time: " + DateTime.Now);
                        Console.WriteLine("Items Restored: ");

                        foreach (OrderItem lOrderItem in orderItems)
                        {
                            Console.WriteLine("             " + lOrderItem.Book.Title + ": Quantity " + lOrderItem.Quantity);
                        }

                        Console.WriteLine("=============================================" + "\n");
                        Console.WriteLine(" ");

                        // Restore stocks
                        RestoreStock(orderItems, lContainer);

                        // ask the Bank service to transfer fundss
                        result = TransferFundsToCustomer(UserProvider.ReadUserById(lOrder.Customer.Id).BankAccountNumber, lOrder.Total ?? 0.0);

                        // Delete the delivery in the delivery table
                        DeleteDelivery(lOrder.OrderNumber.ToString());

                        lOrder.OrderItems.ToList().ForEach(o => { lContainer.Entry(o).State = System.Data.Entity.EntityState.Deleted; });
                        lContainer.Entry(lOrder).State = System.Data.Entity.EntityState.Deleted;
                        lContainer.Orders.Remove(lOrder);

                        // save the changes
                        lContainer.SaveChanges();
                        lScope.Complete();
                    }
                    catch (Exception lException)
                    {
                        Console.WriteLine("================Cancel Order=================");
                        Console.WriteLine("             Order ID: " + pOrderId);
                        Console.WriteLine("             CustomerID: " + lOrder.Customer.Id);
                        Console.WriteLine("             Name: " + lOrder.Customer.Name);
                        Console.WriteLine("             Address: " + lOrder.Customer.Address);
                        Console.WriteLine("             Time: " + DateTime.Now);
                        Console.WriteLine("Failed to restore the order items");

                        Console.WriteLine("=============================================" + "\n");
                        Console.WriteLine(" ");

                        // need to rollback bank transfer if the transfer happened
                        if (result == "Transfer Success")
                        {
                            TransferFundsFromCustomer(UserProvider.ReadUserById(lOrder.Customer.Id).BankAccountNumber, lOrder.Total ?? 0.0);
                        }
                        SendOrderErrorMessage(lOrder, lException);
                        IEnumerable <System.Data.Entity.Infrastructure.DbEntityEntry> entries = lContainer.ChangeTracker.Entries();
                    }
                }
            }
            // we have a customer to send the cancelled confirmation to
            if (customerEmail != "" && orderNumber != Guid.Empty)
            {
                SendOrderCancelledConfirmation(customerEmail, orderNumber);
            }
        }