Exemplo n.º 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);
            }
        }
Exemplo n.º 2
0
        private void Export(string parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            string[] commands = parameters.Split(' ');
            if (commands.Length != 2)
            {
                throw new ArgumentException("Wrong command format. Example: export csv d:/records.csv", nameof(parameters));
            }

            string       format      = commands[FormatPosition];
            string       path        = commands[PathPosition];
            const string yesAnswer   = "y";
            const string csvFileType = "csv";
            const string xmlFileType = "xml";

            if (File.Exists(path))
            {
                write($"File is exist - rewrite {path}? [Y/n] ");
                if (char.TryParse(Console.ReadLine(), out char answer))
                {
                    if (answer.ToString(CultureInfo.InvariantCulture).Equals(yesAnswer, StringComparison.InvariantCultureIgnoreCase))
                    {
                        File.Delete(path);
                        Write(path);
                    }
                    else
                    {
                        return;
                    }
                }
            }
            else
            {
                try
                {
                    Write(path);
                }
                catch (FileLoadException ex)
                {
                    throw new ArgumentException($"Export failed: can't open file {path}.", ex.Message);
                }
            }

            void Write(string p)
            {
                using (StreamWriter streamWriter = new StreamWriter(p))
                {
                    FileCabinetServiceSnapshot snapshot = this.Service.MakeSnapshot();

                    if (format == csvFileType)
                    {
                        snapshot.SaveToCSV(streamWriter);
                    }
                    else if (commands[FormatPosition] == xmlFileType)
                    {
                        snapshot.SaveToXML(streamWriter);
                    }
                    else
                    {
                        write($"There is no {commands[FormatPosition]} command.");
                        return;
                    }

                    write($"All records are exported to file {p}.");
                }
            }
        }