public ActionResult Edit(int id, lib.Location formLocation) { try { lib.Location libLocation = lib.Location.GetById(formLocation.Id); Dictionary <int, int> formInventory = formLocation.Inventory; lib.Location.Locations.Remove(formLocation); foreach (var ingredient in formInventory) { libLocation.UpdateInventory(ingredient.Key, ingredient.Value - libLocation.Inventory[ingredient.Key]); } db.Location dbLocation; List <db.InventoryJunction> inventoryList; dbLocation = db.Mapper.Map(libLocation, out inventoryList); locationRepo.Update(dbLocation); foreach (var inventory in inventoryList) { locationRepo.inventoryRepo.Update(inventory); } locationRepo.SaveChanges(); return(RedirectToAction(nameof(Index))); } catch { return(View(formLocation)); } }
public static lib.Location Map(db.Location dbLocation) { lib.Location libLocation = new lib.Location { Id = dbLocation.Id, Name = dbLocation.Name }; // Populate the inventory List <db.InventoryJunction> inventories = inventoryRepo.GetAllInventoryJunctions() .Where(i => i.LocationId == libLocation.Id).ToList(); foreach (var inventory in inventories) { libLocation.UpdateInventory(ingredientRepo.GetById(inventory.IngredientId).Name, inventory.Count); } return(libLocation); }