public async Task <Product[]> UpdateProductAsync(Product updatedProduct, bool returnAllProds, bool returnActiveProds)
 {
     using (DataDbContext context = new DataDbContext())
     {
         var existingProduct = context.Products.SingleOrDefault(x => x.Id == updatedProduct.Id);
         if (existingProduct != null)
         {
             //existingProduct = updatedProduct;
             context.Entry(existingProduct).CurrentValues.SetValues(updatedProduct);
             context.SaveChanges();
         }
         if (returnAllProds)
         {
             return(await GetProductsAsync(false));
         }
         else if (returnActiveProds)
         {
             return(await GetProductsAsync(true));
         }
         else
         {
             return(null);
         }
     }     //using
 }
 public async Task <BusinessRules> UpdateBusinessRulesAsync(BusinessRules updatedBusinessRules, bool returnNewRules)
 {
     using (DataDbContext context = new DataDbContext())
     {
         var existingBusinessRules = context.BusinessRules.FirstOrDefault();
         if (existingBusinessRules == null)
         {
             context.BusinessRules.Add(updatedBusinessRules);
             context.SaveChanges();
         }
         else
         {
             //existingBusinessRules = updatedBusinessRules;
             context.Entry(existingBusinessRules).CurrentValues.SetValues(updatedBusinessRules);
             context.SaveChanges();
         }
         if (returnNewRules)
         {
             return(await GetBusinessRulesAsync());
         }
         else
         {
             return(null);
         }
     }     //using
 }
        public async Task <ViewOrder[]> UpdateOrderAsync(ViewOrder updatedViewOrder, bool returnNewList)
        {
            using (DataDbContext context = new DataDbContext())
            {
                var existingOrder = context.Orders.SingleOrDefault(x => x.Id == updatedViewOrder.Id);
                if (existingOrder != null)
                {
                    Order updatedOrder = new Order
                    {
                        Id            = updatedViewOrder.Id,
                        OnDate        = updatedViewOrder.OnDate,
                        CustomerId    = updatedViewOrder.CustomerId,
                        ShipFirstName = updatedViewOrder.ShipFirstName,
                        ShipLastName  = updatedViewOrder.ShipLastName,
                        ShipStreet    = updatedViewOrder.ShipStreet,
                        ShipCity      = updatedViewOrder.ShipCity,
                        ShipState     = updatedViewOrder.ShipState,
                        ShipZip       = updatedViewOrder.ShipZip,
                        Campaign      = updatedViewOrder.Campaign,
                        Subtotal      = updatedViewOrder.Subtotal,
                        Tax           = updatedViewOrder.Tax,
                        ShipHand      = updatedViewOrder.ShipHand,
                        Total         = updatedViewOrder.Total,
                        Deferred      = updatedViewOrder.Deferred,
                        Done          = updatedViewOrder.Done
                    };
                    //existingOrder = updatedOrder;
                    context.Entry(existingOrder).CurrentValues.SetValues(updatedViewOrder);

                    //order items: out with the old
                    OrderItem[] existingOrderItems = context.OrderItems.Where(x => x.OrderId == updatedViewOrder.Id).ToArray();
                    context.OrderItems.RemoveRange(existingOrderItems);

                    //and in with the new
                    List <OrderItem> updatedOrderItems = new List <OrderItem>();
                    foreach (ViewOrderItem item in updatedViewOrder.Items)
                    {
                        updatedOrderItems.Add(new OrderItem
                        {
                            OrderId   = updatedViewOrder.Id,
                            ProductId = item.ProductId,
                            UnitPrice = item.UnitPrice,
                            Quantity  = item.Quantity,
                            LineTotal = item.LineTotal
                        });
                    }
                    context.OrderItems.AddRange(updatedOrderItems);
                    context.SaveChanges();
                }
                if (returnNewList)
                {
                    return(await GetOrdersAsync());
                }
                else
                {
                    return(null);
                }
            }     //using
        }
 public async Task <RetailContact[]> UpdateRetailContactAsync(RetailContact updatedContact, bool returnNewList)
 {
     using (DataDbContext context = new DataDbContext())
     {
         var existingContact = context.RetailContacts.SingleOrDefault(x => x.Id == updatedContact.Id);
         if (existingContact != null)
         {
             //existingContact = updatedContact;
             context.Entry(existingContact).CurrentValues.SetValues(updatedContact);
             context.SaveChanges();
         }
         if (returnNewList)
         {
             return(await GetRetailContactsAsync());
         }
         else
         {
             return(null);
         }
     }     //using
 }
 public async Task <TaxRate[]> UpdateTaxRateAsync(TaxRate updatedRate, bool returnNewList)
 {
     using (DataDbContext context = new DataDbContext())
     {
         var existingRate = context.TaxRates.SingleOrDefault(x => x.State == updatedRate.State);
         if (existingRate != null)
         {
             //existingRate = updatedRate;
             context.Entry(existingRate).CurrentValues.SetValues(updatedRate);
             context.SaveChanges();
         }
         if (returnNewList)
         {
             return(await GetTaxRatesAsync());
         }
         else
         {
             return(null);
         }
     }     //using
 }