Skip to content

Commit

Permalink
Add multiple selection support for complete command
Browse files Browse the repository at this point in the history
  • Loading branch information
mehmetseckin committed Nov 24, 2019
1 parent 0cf0537 commit fd1983e
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Todo.CLI/Handlers/CompleteCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using InquirerCS;
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Todo.Core;
Expand All @@ -12,33 +14,45 @@ namespace Todo.CLI.Handlers
{
public class CompleteCommandHandler
{
private const string PromptMessage = "Which item(s) would you like to delete?";
private const string UIHelpMessage = "Use arrow keys to navigate between options. [SPACEBAR] to mark the options, and [ENTER] to confirm your input.";

public static ICommandHandler Create(IServiceProvider serviceProvider)
{
return CommandHandler.Create<string>(async (itemId) =>
{
var todoItemRepository = (ITodoItemRepository)serviceProvider.GetService(typeof(ITodoItemRepository));

if (!string.IsNullOrEmpty(itemId))
{
await CompleteItem(todoItemRepository, new TodoItem { Id = itemId });
var item = new TodoItem { Id = itemId };
await todoItemRepository.CompleteAsync(item);
}
else
{
// Retrieve items that are not completed
var items = await todoItemRepository.ListAsync(listAll: false);

// Ask user which item to complete
var selectedItem = Question
.List("Which item would you like to complete?", items)
// Ask user which items to complete
var message = PromptMessage
+ Environment.NewLine
+ Environment.NewLine
+ UIHelpMessage;

var selectedItems = Question
.Checkbox(message, items)
.Prompt();
await CompleteItem(todoItemRepository, selectedItem);

CompleteItems(todoItemRepository, selectedItems);
}

Console.Clear();
});
}

private static async Task CompleteItem(ITodoItemRepository todoItemRepository, TodoItem selectedItem)
private static void CompleteItems(ITodoItemRepository todoItemRepository, IEnumerable<TodoItem> selectedItems)
{
await todoItemRepository.CompleteAsync(selectedItem);
Console.Clear();
Task.WaitAll(selectedItems.Select(item => todoItemRepository.CompleteAsync(item)).ToArray());
}
}
}

0 comments on commit fd1983e

Please sign in to comment.