public void DeleteInventoryById(int Id) { List <Inventory> inventories = new List <Inventory>(); JSONReadWrite readWrite = new JSONReadWrite(); inventories = JsonConvert.DeserializeObject <List <Inventory> >(readWrite.Read("Inventory.json", "data")); int index = inventories.FindIndex(x => x.Id == Id); inventories.RemoveAt(index); string jSONString = JsonConvert.SerializeObject(inventories); readWrite.Write("Inventory.json", "data", jSONString); }
public Inventory AddUpdateInventory(Inventory Model) { List <Inventory> inventory = new List <Inventory>(); JSONReadWrite readWrite = new JSONReadWrite(); inventory = JsonConvert.DeserializeObject <List <Inventory> >(readWrite.Read("Inventory.json", "data")); Inventory data = inventory.FirstOrDefault(x => x.Id == Model.Id); if (data == null) { inventory.Add(Model); } else { int index = inventory.FindIndex(x => x.Id == Model.Id); inventory[index] = Model; } string jSONString = JsonConvert.SerializeObject(data); readWrite.Write("Inventory.json", "data", jSONString); return(data); }