Skip to content
Open
Show file tree
Hide file tree
Changes from 11 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
2 changes: 0 additions & 2 deletions src/EasyList/DataModels/Todo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ public class Todo
public string Label { get; set; }
public string? Description { get; set; }
public TodoPriority Priority { get; set; } = TodoPriority.Low;

private DateTimeOffset _createdDate = DateTimeOffset.UtcNow;
public DateTimeOffset CreatedDate => _createdDate.ToLocalTime();

private DateTimeOffset? _dueDate;
public DateTimeOffset? DueDate
{
Expand Down
1 change: 1 addition & 0 deletions src/EasyList/Enums/TodoMenuOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public enum TODOMENU
View,
MarkAsDone,
ListAll,
Update,
Quit
}
}
10 changes: 10 additions & 0 deletions src/EasyList/Enums/TodoUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace EasyList
{
public enum TodoUpdate
{
Label,
Description,
Priority,
DueDate
}
}
1 change: 1 addition & 0 deletions src/EasyList/Interfaces/ITodoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ internal interface ITodoService
void DisplayAllTodo(TodoOrder todoOrder);
Todo? GetTodoByID(int id);
void MarkTodoAsDone(Todo todo);
void UpdateTodo(Todo todo, TodoUpdate command);
}
}
1 change: 0 additions & 1 deletion src/EasyList/ParseAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static Dictionary<string, string> Parse(string[] args)
}

string label = GetData("add", args, positions) ?? string.Empty;

string description = GetData("-d", args, positions) ?? string.Empty;
string dueDate = GetData("-t", args, positions) ?? string.Empty;
string priority = GetData("-p", args, positions) ?? "${TodoPriority.Low}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did this get committed? Was the code not debugged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, Maybe Expecting only a silly string change I didn't check after this commit.

Expand Down
7 changes: 6 additions & 1 deletion src/EasyList/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
namespace EasyList
using EasyList.Factories;
using EasyList.Interfaces;

namespace EasyList
{
class Program
{
public static ITodoService _todoService = Factory.CreateTodoServiceDB();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename to something like TodoService and make it a readonly property.


public static void Main(string[] args)
{
if(args.Length > 1)
Expand Down
109 changes: 36 additions & 73 deletions src/EasyList/TodoMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,9 @@ namespace EasyList
{
public class TodoMenu
{
private static IEnumerable<string> ValidateAdd(Dictionary<string, string> input)
{
if (string.IsNullOrWhiteSpace(input["label"]))
{
yield return "Label cannot be empty";
}
if (DateTimeOffset.TryParse(input["duedate"], out DateTimeOffset tempDate))
{
if (tempDate < DateTime.UtcNow)
{
yield return "Due date cannot be in the past";
}
}
}

private static bool Validate(TODOMENU command, Dictionary<string, string> input)
{
var errors = command switch {
TODOMENU.Add => ValidateAdd(input),
// register the rest of the options that need to be validated here
_ => throw new InvalidOperationException($"No validator exists for {command}"),
};

if (errors.Any())
{
Console.WriteLine("The input has the following errors:");
foreach (var error in errors)
{
Console.WriteLine("\t"+error);
}

return false;
}

return true;
}

public static void Run()
{
ITodoService _todoService = Factory.CreateTodoServiceDB();

while(true)
{
var action = Prompt.Select<TODOMENU>("Welcome to EasyList!");
Expand All @@ -61,7 +24,9 @@ public static void Run()
var inputAdd = Prompt.Input<string>("Enter TODO ");
var parsedAdd = ParseAdd.Parse(inputAdd?.Split() ?? Array.Empty<string>());

if (Validate(action, parsedAdd))
Validate.Add(parsedAdd);

if (!Validate.TodoErrors())
{
var newTodo = new Todo()
{
Expand All @@ -70,64 +35,62 @@ public static void Run()
DueDate = DateTimeOffset.TryParse(parsedAdd["duedate"], out DateTimeOffset tempDate) ? tempDate : null,
Priority = Enum.Parse<TodoPriority>(parsedAdd["priority"])
};
_todoService.AddTodo(newTodo);
Program._todoService.AddTodo(newTodo);
}

break;

case TODOMENU.Delete:
var inputDelete = Prompt.Input<string>("Enter TODO ID(s) ");
foreach (var item in inputDelete.Split())
var inputDelete = Prompt.Input<string>("Enter TODO ID(s) ").Split();

var validDeleteList = Validate.MultipleIds(inputDelete.ToIntIds());

if (!Validate.TodoErrors())
{
var todoItem = _todoService.GetTodoByID(int.Parse(item));
if(todoItem != null)
{
Console.WriteLine($"Deleted: {todoItem.Label}");
_todoService.DeleteTodo(todoItem);
}
else
foreach (var todoDelete in validDeleteList)
{
Console.WriteLine($"Todo Id: {item} Not Found.");
Console.WriteLine("Try Again.");
}

Program._todoService.DeleteTodo(todoDelete);
}
}
break;

case TODOMENU.View:
var inputView = Prompt.Input<int>("Enter TODO ID ");
var todo = _todoService.GetTodoByID(inputView);
if(todo != null)
{
_todoService.DisplayTodo(todo);
}
else

var todoView = Validate.Id(inputView);

if(!Validate.TodoErrors())
{
Console.WriteLine($"Todo Id: {inputView} Not Found.");
Program._todoService.DisplayTodo(todoView!);
}
break;

case TODOMENU.MarkAsDone:
var inputDone = Prompt.Input<string>("Enter TODO ID(s) ");
foreach (var item in inputDone.Split())
var inputDone = Prompt.Input<string>("Enter TODO ID(s) ").Split();

var validTodoList = Validate.MultipleIds(inputDone.ToIntIds());

if (!Validate.TodoErrors())
{
var todoItem = _todoService.GetTodoByID(int.Parse(item));
if(todoItem != null)
{
Console.WriteLine($"Completed:{todoItem.Label}.");
_todoService.MarkTodoAsDone(todoItem);
}
else
foreach (var todoDelete in validTodoList)
{
Console.WriteLine($"Todo Id: {item} Not Found.");
Console.WriteLine("Try Again.");
Program._todoService.MarkTodoAsDone(todoDelete);
}
}
break;
case TODOMENU.Update:
var inputUpdateAction = Prompt.Select<TodoUpdate>("Select Update Action");
var inputUpdate = Prompt.Input<int>("Enter the ID to Update");

var validTodo = Validate.Id(inputUpdate);

if (!Validate.TodoErrors())
{
Program._todoService.UpdateTodo(validTodo!, inputUpdateAction);
}
break;
case TODOMENU.ListAll:
var inputList = Prompt.Select<TodoOrder>("Select List Order: ", defaultValue: TodoOrder.CreateDate);
_todoService.DisplayAllTodo(inputList);
Program._todoService.DisplayAllTodo(inputList);
break;
case TODOMENU.Quit:
Console.WriteLine("Exiting...");
Expand Down
56 changes: 55 additions & 1 deletion src/EasyList/TodoServiceDB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EasyList.Enums;
using EasyList.Factories;
using EasyList.Interfaces;
using Sharprompt;

namespace EasyList
{
Expand Down Expand Up @@ -50,6 +51,59 @@ public void DisplayAllTodo(TodoOrder todoOrder)
.Write(Format.MarkDown);
}


public void UpdateTodo(Todo todo, TodoUpdate command)
{
switch (command)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If statement would be better here too.

{
case TodoUpdate.Label:
{
Console.WriteLine("Enter the New Label: ");
string? newLabel = Console.ReadLine();
if (Validate.Label(newLabel))
{
if (!Validate.TodoErrors())
{
todo.Label = newLabel!;
}
}
_todoRepository.UpdateTodo(todo);
break;
}

case TodoUpdate.Description:
{
Console.WriteLine("Enter the New Description: ");
string? newDescription = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newDescription))
{
todo.Description = newDescription;
}
_todoRepository.UpdateTodo(todo);
break;
}

case TodoUpdate.Priority:
{
todo.Priority = Prompt.Select<TodoPriority>("Select new Priority."); ;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service should not interact with the user, thats a job for the menu

_todoRepository.UpdateTodo(todo);
break;
}

case TodoUpdate.DueDate:
{
Console.WriteLine("Enter the New Due Date: ");
string? newDueDate = Console.ReadLine();
if(Validate.DueDate(newDueDate))
{
if (!Validate.TodoErrors())
{
todo.DueDate = DateTimeOffset.Parse(newDueDate!);
}
}
_todoRepository.UpdateTodo(todo);
break;
}
}
}
}
}
53 changes: 52 additions & 1 deletion src/EasyList/TodoServiceInMemory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using EasyList.Enums;
using EasyList.Factories;
using EasyList.Interfaces;
using Sharprompt;

namespace EasyList
{
Expand Down Expand Up @@ -48,6 +49,56 @@ public void DisplayAllTodo(TodoOrder todoOrder)
.Write(Format.MarkDown);
}


public void UpdateTodo(Todo todo, TodoUpdate command)
{
switch (command)
{
case TodoUpdate.Label:
{
Console.WriteLine("Enter the New Label: ");
string? newLabel = Console.ReadLine();
if (Validate.Label(newLabel))
{
if (!Validate.TodoErrors())
{
todo.Label = newLabel!;
}
}
break;
}

case TodoUpdate.Description:
{
Console.WriteLine("Enter the New Description: ");
string? newDescription = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(newDescription))
{
todo.Description = newDescription;
}
break;
}

case TodoUpdate.Priority:
{
var newPriority = Prompt.Select<TodoPriority>("Select new Priority.");
todo.Priority = newPriority;
break;
}

case TodoUpdate.DueDate:
{
Console.WriteLine("Enter the New Due Date: ");
string? newDueDate = Console.ReadLine();
if (Validate.DueDate(newDueDate))
{
if (!Validate.TodoErrors())
{
todo.DueDate = DateTimeOffset.Parse(newDueDate!);
}
}
break;
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/EasyList/Utility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;

public static class Utility
{
public static IEnumerable<int> ToIntIds(this string[] input)
{
foreach (var item in input)
{
if (int.TryParse(item, out int num))
{
yield return num;
}
}
}
}
Loading