/// <summary> /// Enter a new order into db /// </summary> /// <param name="appOrder"></param> public void CreateOrder(Library.Order appOrder) { using var context = new P0Context(_dbContextOptions); //map library order to db var dbOrder = new Order() { Store = context.Stores.First(s => s.Id == appOrder.TargetStore.Id), Customer = context.StoreCustomers.First(c => c.Id == appOrder.Orderer.Id), Time = appOrder.Time }; //map all items in the order to db foreach (Library.Product selection in appOrder.Selections) { //create a new item, Store = null unless item is part of an inventory var dbItem = new Item() { Product = context.Products.First(p => p.Name == selection.Name), Quantity = selection.Quantity, Store = null, //Store = context.Stores.First(s => s.Id == appOrder.TargetStore.Id), Order = dbOrder }; context.Add(dbItem); } context.Add(dbOrder); context.SaveChanges(); }
/// <summary> /// Enter a new customer into db, and app /// </summary> /// <param name="customerName"></param> public void CreateCustomer(string customerName, List <Library.Customer> customers) { //add to app Library.Customer newCustomer = new Library.Customer(customerName); customers.Add(newCustomer); //add to db using var context = new P0Context(_dbContextOptions); newCustomer.Id = context.StoreCustomers.OrderBy(x => x.Id).Last().Id + 1; var dbCustomer = new StoreCustomer() { Name = customerName }; context.Add(dbCustomer); context.SaveChanges(); }