protected override async Task <int> OnExecuteAsync(CommandLineApplication app) { List <Models.Neuron> neurons; if (string.IsNullOrEmpty(Group)) { neurons = await this.braindumpService.GetAllAsync(); } else { if (groupParser.TryParse(Group, out var groupName)) { neurons = await this.braindumpService.GetNeuronsByGroupAsync(groupName); } else { throw new ArgumentException(groupParser.ErrorMessage, nameof(Group)); } } foreach (var n in neurons) { console.WriteLine(n.Information); if (n.Groups.Count > 0) { console.WriteLine($" Groups: {string.Join(", ", n.Groups)}"); } if (n.Reminders.Count > 0) { // display the reminders in ascending order n.Reminders.Sort(); var reminderTimeSpans = new string[n.Reminders.Count]; for (int i = 0; i < n.Reminders.Count; i++) { var timeSpan = n.Reminders[i] - DateTimeOffset.Now; reminderTimeSpans[i] = FormatTimeSpan(timeSpan); } console.WriteLine($" Reminders: {string.Join(", ", reminderTimeSpans)}"); } } console.WriteTable(neurons, n => new { n.Id, n.Information, Groups = string.Join(", ", n.Groups), Reminders = string.Join(", ", n.Reminders.Select(r => FormatTimeSpan(r - DateTimeOffset.Now))) }); return(await base.OnExecuteAsync(app)); }
protected override async Task <int> OnExecuteAsync(CommandLineApplication app) { var groupNames = new List <string>(); var invalidGroups = new List <string>(); foreach (var g in Groups) { if (groupParser.TryParse(g, out var groupName)) { groupNames.Add(groupName); } else { invalidGroups.Add(g); } } if (invalidGroups.Any()) { throw new ArgumentException($"Could not parse these {invalidGroups.Count} group(s): {string.Join(", ", invalidGroups)}."); } var now = DateTimeOffset.Now; var reminderTimes = new List <DateTimeOffset>(); var invalidReminders = new List <string>(); foreach (var r in Reminders) { if (reminderParser.TryParse(r, out var time)) { reminderTimes.Add(now + time); } else { invalidReminders.Add(r); } } if (invalidReminders.Any()) { console.WriteLine( $"Could not parse these {invalidReminders.Count} reminder(s): {string.Join(", ", invalidReminders)}."); } // Message is not null because it is [Required] await this.braindumpService.AddNeuronAsync(Message !, Groups.ToList(), reminderTimes); return(await base.OnExecuteAsync(app)); }