/// <summary> /// Updates file /// </summary> /// <param name="product">The product to be updated</param> public void GBRUpdate(GBRProduct product) { try { SaveToFile(product, true); } catch (Exception ex) { throw new Exception("Errors during update:\n" + ex.Message); } }
/// <summary> /// Remove the product from file /// </summary> /// <param name="product">Remove from file</param> public void GBRDelete(GBRProduct product) { try { DeleteAndSaveFile(product); } catch (Exception ex) { throw new Exception("Errors during delete:\n" + ex.Message); } }
/// <summary> /// Delete the supplied product and save file. /// </summary> /// <param name="product">The product to be delete.</param> private void DeleteAndSaveFile(GBRProduct product) { // get all List <GBRProduct> list = GBRGetProducts(); // prevent duplicate list.RemoveAll(p => p.ProductName.ToLower().Equals(product.ProductName.ToLower())); using (writer = new StreamWriter(FILE_NAME, false)) { list.ForEach(p => writer.WriteLine(p.ToString())); } }
/// <summary> /// Attempts to write on file /// </summary> /// <exception cref="Exception">With all errors in the message.</exception> public void GBRAdd(GBRProduct product) { product.isInsert = true; try { product.SaveToFile(product); } catch (Exception ex) { throw new Exception("Errors during insertion:\n" + ex.Message); } }
/// <summary> /// Save this instance to file /// </summary> private void SaveToFile(GBRProduct product, bool isUpdate = false) { Edit(); // try to validate the product // get all List <GBRProduct> list = GBRGetProducts(); if (isUpdate) { // prevent duplicate list.RemoveAll(p => p.ProductName.ToLower().Equals(product.ProductName.ToLower())); } list.Add(product); using (writer = new StreamWriter(FILE_NAME, false)) { list.ForEach(p => writer.WriteLine(p.ToString())); } }