Skip to content

Commit

Permalink
Add filters to list/remove items completes before a given date
Browse files Browse the repository at this point in the history
  • Loading branch information
Regenhardt committed Feb 22, 2024
1 parent 142c560 commit b6df851
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
15 changes: 12 additions & 3 deletions src/Todo.CLI/Commands/ListCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ namespace Todo.CLI.Commands;

public class ListCommand : Command
{
private static readonly Option<bool> GetAllOption = new(["-a", "--all"], "Lists all to do items including the completed ones.");
private static readonly Option<bool> NoStatusOption = new(["--no-status"], "Suppresses the bullet indicating whether the item is completed or not.");
private static readonly Option<bool> GetAllOption =
new(["-a", "--all"], "Lists all to do items including the completed ones.");

private static readonly Option<bool> NoStatusOption = new(["--no-status"],
"Suppresses the bullet indicating whether the item is completed or not.");

private static readonly Option<DateTime?> OlderThanOption =
new(["--older-than"], "Only items completed before this date.");

private static readonly Argument<string> ListNameArgument = new("list-name", "Only list tasks of this To-Do list.")
{
Arity = ArgumentArity.ZeroOrOne
};


public ListCommand(IServiceProvider serviceProvider) : base("list")
{
Description = "Retrieves a list of the to do items across all To-Do lists.";

Add(GetAllOption);
Add(NoStatusOption);
Add(OlderThanOption);
Add(ListNameArgument);

this.SetHandler(ListCommandHandler.Create(serviceProvider), GetAllOption, NoStatusOption, ListNameArgument);
this.SetHandler(ListCommandHandler.Create(serviceProvider), GetAllOption, NoStatusOption, OlderThanOption, ListNameArgument);
}
}
4 changes: 3 additions & 1 deletion src/Todo.CLI/Commands/RemoveCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ namespace Todo.CLI.Commands;
public class RemoveCommand : Command
{
private static readonly Option<string> ListOpt = new(["--list", "-l"], "The name of the list to remove the item from.");
private static readonly Option<DateTime?> OlderThanOpt = new(new[] { "--older-than" }, "Only items completed before this date.");
public RemoveCommand(IServiceProvider serviceProvider) : base("remove", "Deletes a to do item.")
{
Add(ListOpt);
this.SetHandler(RemoveCommandHandler.Create(serviceProvider), ListOpt);
Add(OlderThanOpt);
this.SetHandler(RemoveCommandHandler.Create(serviceProvider), ListOpt, OlderThanOpt);
}
}
23 changes: 14 additions & 9 deletions src/Todo.CLI/Handlers/ListCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace Todo.CLI.Handlers;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Core.Model;
Expand All @@ -13,12 +12,12 @@ public class ListCommandHandler
private const char TodoBullet = '-';
private const char CompletedBullet = '\u2713'; // Sqrt - check mark

public static Func<bool, bool, string, Task> Create(IServiceProvider serviceProvider)
public static Func<bool, bool, DateTime?, string, Task> Create(IServiceProvider serviceProvider)
{
return (all, noStatus, listName) => Execute(serviceProvider, all, noStatus, listName);
return (all, noStatus, olderThan, listName) => Execute(serviceProvider, all, noStatus, olderThan, listName);
}

private static async Task Execute(IServiceProvider sp, bool all, bool noStatus, string listName)
private static async Task Execute(IServiceProvider sp, bool all, bool noStatus, DateTime? olderThan, string listName)
{
if (!string.IsNullOrWhiteSpace(listName))
{
Expand All @@ -29,15 +28,21 @@ private static async Task Execute(IServiceProvider sp, bool all, bool noStatus,
else
{
var itemRepo = sp.GetRequiredService<ITodoItemRepository>();
list.Tasks = (await itemRepo.ListByListIdAsync(list.Id, all)).ToList();
Render(list);
var tasksCall = await itemRepo.ListByListIdAsync(list.Id, all);
if(olderThan.HasValue)
tasksCall = tasksCall.Where(item => item.IsCompleted && item.Completed < olderThan);
list.Tasks = tasksCall.ToList();
Render(list, noStatus);
}

return;
}

var taskRepo = sp.GetRequiredService<ITodoItemRepository>();
await foreach (var item in taskRepo.EnumerateAllAsync(all))
var tasks = taskRepo.EnumerateAllAsync(all).ToBlockingEnumerable();
if (olderThan.HasValue)
tasks = tasks.Where(item => item.IsCompleted && item.Completed < olderThan);
foreach (var item in tasks)
{
if (!noStatus)
{
Expand All @@ -49,10 +54,10 @@ private static async Task Execute(IServiceProvider sp, bool all, bool noStatus,
}
}

private static void Render(TodoList list)
private static void Render(TodoList list, bool noStatus)
{
Console.WriteLine($"{list.Name} ({list.Count}):");
foreach (var item in list.Tasks) Render(item);
foreach (var item in list.Tasks) Render(item, noStatus);
}

private static void Render(TodoItem item, bool noStatus)
Expand Down
13 changes: 11 additions & 2 deletions src/Todo.CLI/Handlers/RemoveCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class RemoveCommandHandler
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 Func<string, Task<int>> Create(IServiceProvider serviceProvider)
public static Func<string, DateTime?, Task<int>> Create(IServiceProvider serviceProvider)
{
return async listName =>
return async (listName, olderThan) =>
{
var todoItemRepository = serviceProvider.GetRequiredService<ITodoItemRepository>();

Expand All @@ -26,6 +26,15 @@ public static Func<string, Task<int>> Create(IServiceProvider serviceProvider)
? await todoItemRepository.ListAllAsync(includeCompleted: true)
: await todoItemRepository.ListByListNameAsync(listName, includeCompleted: true)).ToList();

if (olderThan.HasValue)
{
items = items.Where(item =>
item.IsCompleted && item.Completed.HasValue &&
item.Completed.Value < olderThan.Value
)
.ToList();
}

// Ask user which item to delete
var message = PromptMessage
+ Environment.NewLine
Expand Down

0 comments on commit b6df851

Please sign in to comment.