//UPDATE public void UpdateOrder(OrderDetails ord) { LineItemManager linemgr = new LineItemManager(); using (var context = DbContextFactory.Create()) //no 'NEW' b/c of static class { try { //Locate order var query = context.OrderDetails.FirstOrDefault(a => a.Id == ord.Id); Console.WriteLine(query.Id); //Update values //If Id matches... if (query.Id == ord.Id) { Console.WriteLine("Query id matches passed value..."); query.OrderStatus = ord.OrderStatus; query.Shipmethod.Id = ord.Shipmethod.Id; //Then Save changes to order context.Entry(query).State = EntityState.Modified; context.SaveChanges(); if (ord.Lineitems != null) { Console.WriteLine("UPDATED ORDER HAS LINES "); //Update each line item foreach (LineItem l in ord.Lineitems) { //if new line item, create it -- will contain order ID if (l.Id == 0) { linemgr.CreateLineItem(l); } else { //Update linemgr.UpdateLineItem(l); } }// end foreach ql @ line (in query) } Console.WriteLine($"Order ID# {ord.Id} updated successfully."); }//end id match } catch (Exception) { Console.WriteLine("\t** Unable to update Order."); } } }//end update
}//end update //CREATE //This should take CUSTOMER ID, SHIPPING DETAILS, ARRAY OF LINE ITEMS & CREATE ORDER public OrderDetails CreateOrder(NewOrderDetails orderdetails) { LineItemManager linemgr = new LineItemManager(); using (var context = DbContextFactory.Create()) //no 'NEW' b/c of static class { var cust = context.Customer.Include(a => a.Addresses).FirstOrDefault(c => c.Id == orderdetails.CustomerId); var shipmethod = context.ShippingMethod.FirstOrDefault(s => s.Id == orderdetails.ShippingMethodId); var status = context.OrderDetails.FirstOrDefault(s => Convert.ToInt32(s.OrderStatus) == orderdetails.Status); var product = context.Product; var lines = new List<LineItem>(); foreach(NewLine l in orderdetails.LineItems) { var prod = product.FirstOrDefault(t => t.Id == l.ProductId); LineItem newLine = new LineItem { Id = 0, ProductDetails = prod, Quantity = l.Quantity, OrderDetails = null }; lines.Add(newLine); } OrderDetails newOrder = new OrderDetails { CustomerDetails = cust, Shipmethod = shipmethod, Lineitems = lines, OrderStatus = status.OrderStatus, OrderDate = orderdetails.OrderDate }; try { //Add order context.OrderDetails.Add(newOrder); context.SaveChanges(); Console.WriteLine($"Order {newOrder.Id} added...."); return newOrder; } catch (Exception) { Console.WriteLine("Unable to add order...."); return null; } } }