Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remove-completed command, to remove completed items from a give… #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Todo.CLI/Commands/RemoveCompletedCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.CommandLine;
using Todo.CLI.Handlers;

namespace Todo.CLI.Commands;

public class RemoveCompletedCommand : Command
{
private static readonly Argument<string> ListNameArgument = new("list-name", "The name of the list from where to remove completed items.")
{
Arity = ArgumentArity.ExactlyOne
};


public RemoveCompletedCommand(IServiceProvider serviceProvider) : base("remove-completed")
{
Description = "Removes completed items from a list.";

Add(ListNameArgument);

this.SetHandler(RemoveCompletedCommandHandler.Create(serviceProvider), ListNameArgument);
}
}
1 change: 1 addition & 0 deletions src/Todo.CLI/Commands/TodoCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public TodoCommand(IServiceProvider serviceProvider)
Add(new ListCommand(serviceProvider));
Add(new CompleteCommand(serviceProvider));
Add(new RemoveCommand(serviceProvider));
Add(new RemoveCompletedCommand(serviceProvider));
}
}
57 changes: 57 additions & 0 deletions src/Todo.CLI/Handlers/RemoveCompletedCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;

namespace Todo.CLI.Handlers;

using System;
using System.Linq;
using System.Threading.Tasks;
using Core.Model;
using Core.Repository;
using Microsoft.Extensions.DependencyInjection;

public class RemoveCompletedCommandHandler
{
public static Func<string, Task> Create(IServiceProvider serviceProvider)
{
return (listName) => Execute(serviceProvider, listName);
}

private static async Task Execute(IServiceProvider sp, string listName)
{
if (!string.IsNullOrWhiteSpace(listName))
{
var listRepo = sp.GetRequiredService<ITodoListRepository>();
var list = await listRepo.GetByNameAsync(listName);
if (list?.Id is null)
Console.WriteLine($"No list found with the name '{listName}'.");
else
{
var itemRepo = sp.GetRequiredService<ITodoItemRepository>();


list.Tasks = (await itemRepo.ListByListIdAsync(list.Id, true)).ToList();
while(list.Tasks.Any(t => t.IsCompleted)){
DeleteItems(itemRepo, list.Tasks.Where(t => t.IsCompleted));
list.Tasks = (await itemRepo.ListByListIdAsync(list.Id, true)).ToList();
}
}

return;
}

Console.WriteLine("No list name provided.");
}


private static void DeleteItems(ITodoItemRepository todoItemRepository, IEnumerable<TodoItem> selectedItems)
{

foreach (var item in selectedItems)
{
Console.WriteLine($"Attempt to delete `{item.Subject}`");
todoItemRepository.DeleteAsync(item).GetAwaiter().GetResult();
}

Console.Clear();
}
}