/// <summary> /// Add an inventory item given an item model if it doesn't exist /// Reactivates it if deactivated /// </summary> /// <param name="model"></param> /// <returns>The manipulated/created inventory entry</returns> private Inventory AddToInventory(ItemModel model) { JourListDMContainer dm = new JourListDMContainer(); var member = dm.Members.SingleOrDefault(z => z.Name == User.Identity.Name); if (member == null) return null; // If inventory already exists but is inactive, reactivate // If it exists and is active, then just return the inventory item, do nothing Inventory inv; if ((inv = member.Inventories.FirstOrDefault(z=>z.Item.Id == model.Id)) != null) { if (inv.Active == false) inv.Active = true; else return inv; } // Add an inventory listing if it doesn't exist. else { inv = new Inventory(); inv.Item = dm.Items.Single(z=>z.Id == model.Id); inv.RestockThreshold = model.RestockThreshold; inv.OnHand = model.OnHand; inv.RequiredQuantity = model.RequiredQuantity; inv.Unit = dm.Units.Single(z => z.Id == model.UnitId); inv.Member = member; dm.AddToInventories(inv); } dm.SaveChanges(); return inv; }