Beispiel #1
0
        /// <inheritdoc/>
        protected override void Make(AppCommandRequest commandRequest)
        {
            (string format, string fileName) = CommandHandleBase.SplitParam(commandRequest.Parameters);
            if (File.Exists(fileName))
            {
                Console.Write($"File is exist - rewrite {fileName}? [Y/n] ");
                char output = char.ToUpper(Console.ReadLine()[0]);
                if (output != 'Y')
                {
                    return;
                }
            }

            FileCabinetServiceSnapshot snapshot = this.Service.MakeSnapshot();

            try
            {
                using (StreamWriter writer = new (fileName))
                {
                    switch (format)
                    {
                    case "csv":
                        snapshot.SaveToCSV(new FileCabinetRecordCsvWriter(writer));
                        break;

                    case "xml":
                    {
                        using FileCabinetRecordXmlWriter fileCabinetRecordXmlWriter = new (writer);
                        snapshot.SaveToXML(fileCabinetRecordXmlWriter);
                        break;
                    }

                    default:
                        Console.WriteLine("Format is not supported.");
                        break;
                    }
                }

                Console.WriteLine($"All records are exported to file {fileName}.");
            }
            catch (IOException)
            {
                Console.WriteLine($"Export failed: can't open file {fileName}.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Beispiel #2
0
        /// <inheritdoc/>
        protected override void Make(AppCommandRequest commandRequest)
        {
            (string format, string fileName) = CommandHandleBase.SplitParam(commandRequest.Parameters);
            if (!File.Exists(fileName))
            {
                Console.WriteLine($"File \"{fileName}\"is not exist.");
                return;
            }

            FileStream fileStream;

            try
            {
                fileStream = File.OpenRead(fileName);
            }
            catch (Exception)
            {
                Console.WriteLine("Can't open file");
                return;
            }

            int count    = 0;
            var snapshot = new FileCabinetServiceSnapshot();

            switch (format)
            {
            case "csv":
                snapshot.LoadFromCSV(fileStream);
                count = this.Service.Restore(snapshot);
                break;

            case "xml":
                snapshot.LoadFromXml(fileStream);
                count = this.Service.Restore(snapshot);
                break;

            default:
                Console.WriteLine("Format is not supported.");
                break;
            }

            Console.WriteLine($"{count} records were imported from {fileName}.");
        }