public IHttpActionResult CreateInventory(NewInventoryViewModel NewInventory) { var InventoryName = _Context.Inventories.Where(a => NewInventory.Name.Contains(a.Name)); if (InventoryName == null) { return(BadRequest()); } var Inventory = new Inventory { Name = NewInventory.Name }; _Context.Inventories.Add(Inventory); var Products = _Context.Products.Where(p => NewInventory.ProductIds.Contains(p.Id)).ToList(); int i = 0; foreach (var Product in Products) { var quantity = NewInventory.Quantity[i]; Product.QuantityInStock -= quantity; if (Product.QuantityInStock < 0) { return(BadRequest()); } var InventoryProducts = new InventoryProducts { Inventory = Inventory, InventoryName = NewInventory.Name, Product = Product }; _Context.InventoryProducts.Add(InventoryProducts); i += 1; } _Context.SaveChanges(); return(Ok()); }
public async Task <IActionResult> NewInventory(NewInventoryViewModel Inventory) { var user = await GetCurrentUserAsync(); Inventory.OwnerUserId = user.UserId.Value; var result = await _inventories.CreateInventory(_inventories.Transform(Inventory)); if (result != null) { return(RedirectToAction(nameof(Inventory), new { Id = Inventory.Id })); } else { Inventory.MyProfile = user; return(View(Inventory)); } }
public BaseObject Transform(NewInventoryViewModel inventory) { throw new NotImplementedException(); }