/// <summary> /// Restores the records from the snapshot. /// </summary> /// <param name="snapshot">A snapshot to restore.</param> /// <returns>Number of imported records.</returns> public int Restore(IFileCabinetServiceSnapshot snapshot) { if (snapshot == null) { throw new ArgumentNullException($"Snapshot is invalid."); } int recordsImported = 0; foreach (var record in snapshot.Records) { string exceptionMessage = this.validator.Validate(record); if (exceptionMessage == null) { if (this.ids.Contains(record.Id)) { this.EditRecord(record); recordsImported++; } else { this.AddRecord(record); recordsImported++; } } else { Console.WriteLine("#" + record.Id + " record is invalid."); } } return(recordsImported); }
/// <summary> /// Adds timing count for Restore method. /// </summary> /// <param name="snapshot">A snapshot of records to restore.</param> /// <returns>Number of imported records.</returns> public int Restore(IFileCabinetServiceSnapshot snapshot) { this.watch = Stopwatch.StartNew(); int imported = this.service.Restore(snapshot); this.watch.Stop(); Console.WriteLine($"Restore method execution duration is " + this.watch.ElapsedTicks + " ticks."); return(imported); }
/// <summary> /// Adds logs about Restore method in the textfile. /// </summary> /// <param name="snapshot">Snapshot to restore by.</param> /// <returns>Number of imported records.</returns> public int Restore(IFileCabinetServiceSnapshot snapshot) { string toWrite = DateTime.Now.ToString("MM/dd/yyyy hh:mm", CultureInfo.InvariantCulture) + " Calling Restore()\n"; this.writer.Write(toWrite); int importedRecords = this.service.Restore(snapshot); toWrite = DateTime.Now.ToString("MM/dd/yyyy hh:mm", CultureInfo.InvariantCulture) + " Restore() imported " + importedRecords + "records\n"; this.writer.Write(toWrite); return(importedRecords); }
/// <summary> /// Exports the records to csv or xml format. /// </summary> /// <param name="parameters">Format to write in and path to write to.</param> private void Export(string parameters) { string[] args = parameters.Split(); if (args == null || args.Length < 2) { Console.WriteLine("Incorrect parameters."); return; } string format = args[0].ToLower(); string path = args[1]; if (File.Exists(path)) { Console.WriteLine("File is exist - rewrite " + path + "? [Y/n]"); if (Console.ReadLine().ToLower() != "y") { return; } } using (StreamWriter writer = new StreamWriter(path)) { IFileCabinetServiceSnapshot snapshot = this.Service.MakeSnapshot(); bool isSucceed = false; switch (format) { case "csv": isSucceed = snapshot.SaveToCsv(writer); break; case "xml": isSucceed = snapshot.SaveToXml(writer); break; default: Console.WriteLine("Incorrect format: can be xml or csv."); return; } if (!isSucceed) { Console.WriteLine("Export failed: can't open file " + path); } else { Console.WriteLine("All records are exported to file " + path); } } }