public bool UpdateRenewModel(RenewableModel model)
 {
     try
     {
         return(DeleteRenewModel(model) && AddRenewModel(model));
     }
     catch
     {
         return(false);
     }
 }
 public bool DeleteRenewModel(RenewableModel model)
 {
     try
     {
         File.Delete(_path + model.Id + ".json");
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        private void SaveRenew()
        {
            var renew = new RenewableModel();

            renew.Id         = _selectedRenew != Guid.Empty ? _selectedRenew : Guid.NewGuid();
            renew.Name       = textRenewName.Text;
            renew.AppliesTo  = textRenewAppliesTo.Text;
            renew.ExpiryDate = dateTimePickerExp.Value;
            renew.Remarks    = richTextBoxRemarks.Text;

            if (string.IsNullOrWhiteSpace(renew.Name) || string.IsNullOrWhiteSpace(renew.AppliesTo) ||
                !textRenewAppliesTo.AutoCompleteCustomSource.Contains(renew.AppliesTo.Trim()))
            {
                MessageBox.Show("Something is wrong!", "Error");
                return;
            }
            _renewables.Add(renew);
            LoadRenewablesDataGrid(_renewables.GetList());
        }
        public bool AddRenewModel(RenewableModel model)
        {
            try
            {
                if (!Directory.Exists(_path))
                {
                    Directory.CreateDirectory(_path);
                }

                using (StreamWriter sw = new StreamWriter(_path + model.Id + ".json"))
                {
                    string sm = JsonConvert.SerializeObject(model);
                    sw.Write(sm);
                    sw.Flush();
                    sw.Close();
                }
                return(true);
            }
            catch
            {
                return(false);
            }
        }