/// <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(); }
/// <summary> /// //execute order in db and update passed Store /// </summary> /// <param name="storeChoice"></param> /// <param name="appOrder"></param> /// <returns></returns> public Library.Store FillOrderDb(Library.Store storeChoice, Library.Order appOrder) { using var context = new P0Context(_dbContextOptions); List <bool> successList = storeChoice.FillOrder(appOrder); int successListIndex = 0; //go ahead and grab everything var dbStore = context.Stores .Include(s => s.Items) .ThenInclude(i => i.Product) .Include(s => s.Orders) .ThenInclude(o => o.Customer) .First(s => s.Id == storeChoice.Id); //get store inventory var dbItems = context.Items.Where(s => s.StoreId == storeChoice.Id); //find all items in inventory matching product.Name appOrderId then decrement quantity and update appStore foreach (var product in appOrder.Selections) { //find first item in dbItems that has the same name, and is an inventory var dbInvItem = dbItems.FirstOrDefault(i => i.Product.Name == product.Name); //update the db when fillOrder is successful if (successList.ElementAt(successListIndex)) // == appOrder.OrderId && { dbInvItem.Quantity -= product.Quantity; context.Update(dbInvItem); //storeChoice.Inventory.First(x=>x.Name == product.Name).Quantity = (int)dbInvItem.Quantity; } successListIndex++; } context.SaveChanges(); return(storeChoice); }