/// <inheritdoc/> protected override void Make(AppCommandRequest commandRequest) { string whereString = "where"; int whereIndex = commandRequest.Parameters.IndexOf(whereString, StringComparison.CurrentCultureIgnoreCase); string selectSection; IEnumerable <FileCabinetRecord> list; if (whereIndex < 0) { list = this.IsCached(commandRequest.Parameters, this.Service.GetRecords()); selectSection = commandRequest.Parameters; } else { string whereSection = commandRequest.Parameters.Substring(whereIndex + whereString.Length + 1); selectSection = commandRequest.Parameters.Substring(0, whereIndex); var filter = Parser.Parser.Parse(whereSection); list = this.IsCached(commandRequest.Parameters, this.Service.GetRecords().Where(x => filter.Execute(x))); } IEnumerable <string> fields = string.IsNullOrWhiteSpace(selectSection) ? SelectorBuilder.GetFieldsNames() : selectSection.Split(',').Select(x => x.Trim(' ')); Printer printer = new (fields); printer.Print(list); }
/// <inheritdoc/> public override void Handle(AppCommandRequest commandRequest) { if (commandRequest.Command.Equals("help", StringComparison.CurrentCultureIgnoreCase)) { if (!string.IsNullOrEmpty(commandRequest.Parameters)) { var index = Array.FindIndex(HelpData.HelpMessages, 0, HelpData.HelpMessages.Length, i => string.Equals(i[HelpData.CommandHelpIndex], commandRequest.Parameters, StringComparison.InvariantCultureIgnoreCase)); if (index >= 0) { Console.WriteLine(HelpData.HelpMessages[index][HelpData.ExplanationHelpIndex]); } else { Console.WriteLine($"There is no explanation for '{commandRequest.Parameters}' command."); } } else { Console.WriteLine("Available commands:"); foreach (var helpMessage in HelpData.HelpMessages) { Console.WriteLine("\t{0}\t- {1}", helpMessage[HelpData.CommandHelpIndex], helpMessage[HelpData.DescriptionHelpIndex]); } } Console.WriteLine(); } else { this.NextHandler?.Handle(commandRequest); } }
/// <inheritdoc/> protected override void Make(AppCommandRequest commandRequest) { string whereString = "where"; int whereIndex = commandRequest.Parameters.IndexOf(whereString, StringComparison.CurrentCultureIgnoreCase); string whereSection = commandRequest.Parameters.Substring(whereIndex + whereString.Length + 1); var filter = Parser.Parser.Parse(whereSection); var list = this.Service.GetRecords().Where(x => filter.Execute(x)).ToList(); foreach (var record in list) { this.Service.RemoveRecord(record.Id); } if (list.Count == 0) { Console.WriteLine("No records to delete."); } else if (list.Count == 1) { Console.WriteLine($"Record #{list[0].Id} is deleted."); } else { Console.WriteLine($"Records {string.Join(", ", list.Select(x => $"#{x.Id}").ToArray())} are deleted. "); } this.OnDelete?.Invoke(this, new EventArgs()); }
/// <inheritdoc/> public override void Handle(AppCommandRequest commandRequest) { if (HelpData.HelpMessages.Any(x => commandRequest.Command.Equals(x[HelpData.CommandHelpIndex], StringComparison.CurrentCultureIgnoreCase))) { this.NextHandler?.Handle(commandRequest); } else { Console.WriteLine($"There is no '{commandRequest.Command}' command. Use 'help' command."); var list = HelpData.HelpMessages.Select(x => x[HelpData.CommandHelpIndex]).Where(x => x.StartsWith(commandRequest.Command)).ToList(); if (list.Count == 1) { Console.WriteLine($"The most similar command is"); } else if (list.Count > 1) { Console.WriteLine($"The most similar commands are"); } foreach (var command in list) { Console.WriteLine($"\t{command}"); } } }
/// <inheritdoc/> protected override void Make(AppCommandRequest commandRequest) { var record = new FileCabinetRecord(); try { record = commandRequest.Parameters.GetRecordFromInsertCommandParameter(); this.Service.GetValidator().ValidateParameters(record); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine("Example: insert (fieldNameOne, fieldNameTwo, ...) values (valueOne, valueTwo, ...)"); } if (this.Service.FindRecordById(record.Id) is not null) { Console.WriteLine($"Record #{record.Id} is exist. Try once more."); } else { try { this.Service.InsertRecord(record); Console.WriteLine($"Record #{record.Id}, {record.FirstName}, {record.LastName}, {record.DateOfBirth:yyyy-MMM-dd}, {record.DigitKey}, {record.Account}, {record.Sex} is inserted."); } catch (Exception e) { Console.WriteLine(e.Message); } } this.OnInsert?.Invoke(this, new EventArgs()); }
/// <inheritdoc/> public override void Handle(AppCommandRequest commandRequest) { if (commandRequest.Command.Equals("exit", StringComparison.CurrentCultureIgnoreCase)) { this.running(false); } else { this.NextHandler?.Handle(commandRequest); } }
/// <inheritdoc/> public override void Handle(AppCommandRequest commandRequest) { if (string.IsNullOrWhiteSpace(commandRequest.Command)) { Console.WriteLine("Enter command. 'help' for exemple"); } else { this.NextHandler?.Handle(commandRequest); } }
/// <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); } }
/// <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}."); }
/// <inheritdoc/> protected override void Make(AppCommandRequest commandRequest) { string setString = "set"; string whereString = "where"; int setIndex = commandRequest.Parameters.IndexOf(setString, StringComparison.CurrentCultureIgnoreCase); int whereIndex = commandRequest.Parameters.IndexOf(whereString, StringComparison.CurrentCultureIgnoreCase); setIndex = setIndex + setString.Length + 1; string setSection = commandRequest.Parameters.Substring(setIndex, whereIndex - setIndex); var setSectionPairList = setSection.GetSetPairs(); string whereSection = commandRequest.Parameters.Substring(whereIndex + whereString.Length + 1); var filter = Parser.Parser.Parse(whereSection); var list = this.Service.GetRecords().Where(x => filter.Execute(x)).ToList(); var editRecord = setSectionPairList.GetRecordEditor(); int counter = 0; foreach (var record in list) { editRecord?.Invoke(record); this.Service.EditRecord(record); counter++; } if (counter == 0) { Console.WriteLine("No records to edit."); } else if (counter == 1) { Console.WriteLine("One record has edited."); } else { Console.WriteLine($"{counter} records have edited."); } this.OnUpdate?.Invoke(this, new EventArgs()); }
/// <inheritdoc/> protected override void Make(AppCommandRequest commandRequest) { int recordCount = this.Service.GetStat(); Console.WriteLine($"Data storage processing is completed: {this.Service.PurgeStorage()} of {recordCount} records were purged."); }
/// <inheritdoc/> protected override void Make(AppCommandRequest commandRequest) { var recordsCount = this.Service.GetStat(); Console.WriteLine($"{recordsCount} record(s). Including {this.Service.GetStatDeleted()} is ready to purging."); }
/// <inheritdoc/> public override void Handle(AppCommandRequest commandRequest) { this.NextHandler?.Handle(commandRequest); }