public void DeleteData(DataConfig.Lists sheetName = DataConfig.Lists.Main, int row = -1) { if (row == -1) { throw new CustomException(ErrorCodes.Codes.NotExistingData); } List <List <object> > data = ReadData(sheetName); data.RemoveAt(row); WriteData(data); }
public List <List <object> > ReadData(DataConfig.Lists sheetName = DataConfig.Lists.Main) { List <List <object> > info = new List <List <object> >(); if (sheetName == DataConfig.Lists.Prices) { csvPath = Path.Combine(assemblyPath, "shadec-prices.csv"); separator = ";"; } try { using (TextFieldParser parser = new TextFieldParser(csvPath)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(separator); while (!parser.EndOfData) { List <object> row = new List <object>(); try { row.AddRange( (parser.ReadFields() ?? throw new CustomException(ErrorCodes.Codes.DataReading)) .Cast <object>()); } catch (NullReferenceException e) { throw new CustomException(ErrorCodes.Codes.DataReading); } info.Add(row); } } } catch (Exception ex) { throw new CustomException(ErrorCodes.Codes.DataFileNotFound); } return(info); }
public void UpdateData(List <object> dataNew, List <object> dataOld, DataConfig.Lists sheetName = DataConfig.Lists.Main) { List <List <object> > data = ReadData(sheetName); int oldDataIndex = data.FindIndex(el => el[0].ToString() == dataOld[0].ToString()); if (oldDataIndex != -1) { data[oldDataIndex] = dataNew; File.WriteAllLines(csvPath, PrepareData(data)); } else { throw new CustomException(ErrorCodes.Codes.NotExistingData); } }
public List <List <object> > ReadData(DataConfig.Lists sheetName = DataConfig.Lists.Main) { List <List <object> > info = new List <List <object> >(); if (sheetName == DataConfig.Lists.Prices) { csvPath = Path.Combine(assemblyPath, "shadec-prices.csv"); separator = ","; } try { using (TextFieldParser parser = new TextFieldParser(csvPath)) { parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(separator); while (!parser.EndOfData) { List <object> row = new List <object>(); try { foreach (string field in parser.ReadFields()) { row.Add(field); } } catch (Exception) { throw new CustomException(ErrorCodes.Codes.DataReading); } info.Add(row); } } } catch (Exception) { throw new CustomException(ErrorCodes.Codes.DataReading); } return(info); }
public void WriteData(List <List <object> > data, DataConfig.Lists sheetName = DataConfig.Lists.Main) { if (sheetName == DataConfig.Lists.Prices) { csvPath = Path.Combine(assemblyPath, "shadec-prices.csv"); separator = ";"; } List <string> allLines = PrepareData(data); try { File.AppendAllLines(csvPath, allLines); } catch (FileNotFoundException e) { throw new CustomException(ErrorCodes.Codes.DataFileNotFound); } catch (Exception) { throw new CustomException(ErrorCodes.Codes.DataWriting); } }