public bool Create(string path, EnergyConsumptionModel em) { bool isCreated = true; if (!File.Exists(path)) { isCreated = false; } listOfObjects = GetAll(path); if (listOfObjects == null) { listOfObjects = new List <EnergyConsumptionModel>(); } listOfObjects.Add(em); string json = JsonConvert.SerializeObject(listOfObjects, Formatting.Indented); File.WriteAllText(path, json); Console.WriteLine("Creating is done successfully"); return(isCreated); }
public bool Update(string path, EnergyConsumptionModel em) { bool isUpdated = false; listOfObjects = GetAll(path); if (listOfObjects == null) { return(false); } for (int i = 0; i < listOfObjects.Count; i++) { if (listOfObjects[i].identificator == em.identificator) { listOfObjects[i] = em; isUpdated = true; string json = JsonConvert.SerializeObject(listOfObjects, Formatting.Indented); File.WriteAllText(path, json); Console.WriteLine("Updating is done successfully."); } else { Console.WriteLine("Couldn't update this object"); isUpdated = false; } } return(isUpdated); }