diff --git a/BusyList/Commands/Commands.cs b/BusyList/Commands/Commands.cs index caccea7..3797f4a 100644 --- a/BusyList/Commands/Commands.cs +++ b/BusyList/Commands/Commands.cs @@ -7,4 +7,5 @@ public record NextCommand() : Command; public record AddCommand(string Description, PriorityEnum Priority = PriorityEnum.Normal) : Command; public record DeleteCommand(int Id) : Command; public record DoneCommand(int Id) : Command; + public record EditCommand(int Id, string Property, string Value) : Command; } diff --git a/BusyList/Handlers/DoneHandler.cs b/BusyList/Handlers/DoneHandler.cs index 3828f32..481437b 100644 --- a/BusyList/Handlers/DoneHandler.cs +++ b/BusyList/Handlers/DoneHandler.cs @@ -16,7 +16,13 @@ public void Run(DoneCommand command) { TaskItem taskItem = _taskRepository.GetTaskById(command.Id); - taskItem.TaskStatus = TaskStatus.Done; + if(taskItem == null) + { + Console.WriteLine($"The task with the id {command.Id} does not exist"); + return; + } + + taskItem.Status = TaskStatus.Done; _taskRepository.UpdateTask(taskItem); diff --git a/BusyList/Handlers/EditHandler.cs b/BusyList/Handlers/EditHandler.cs new file mode 100644 index 0000000..85636df --- /dev/null +++ b/BusyList/Handlers/EditHandler.cs @@ -0,0 +1,50 @@ +using BusyList.Commands; +using System; +using System.Reflection; + +namespace BusyList.Handlers +{ + // There are some problems. + // Only work for string properties e.g description. + public class EditHandler : IHandler + { + private readonly ITaskRepository _taskRepository; + + public EditHandler(ITaskRepository taskRepository) + { + _taskRepository = taskRepository; + } + + public void Run(EditCommand command) + { + TaskItem taskItem = _taskRepository.GetTaskById(command.Id); + + if (taskItem != null) + { + PropertyInfo propertyInfo = taskItem.GetType().GetProperty(command.Property); + + if(propertyInfo == null) + { + propertyInfo = taskItem.GetType().GetProperty(command.Property.ToUpperInvariant()); + } + + if (propertyInfo != null) + { + propertyInfo.SetValue(taskItem, command.Value); + + _taskRepository.UpdateTask(taskItem); + + Console.WriteLine("Task updated"); + } + else + { + Console.WriteLine($"The property with the name {command.Property} does not exist"); + } + } + else + { + Console.WriteLine($"The task with the id {command.Id} does not exist"); + } + } + } +} diff --git a/BusyList/Parsing/CommandGrammar.cs b/BusyList/Parsing/CommandGrammar.cs index 76a8293..a9d5ae7 100644 --- a/BusyList/Parsing/CommandGrammar.cs +++ b/BusyList/Parsing/CommandGrammar.cs @@ -8,6 +8,9 @@ public static class CommandGrammar private static readonly Parser _keywordAdd = Parse.IgnoreCase("add").Text(); + private static readonly Parser _keywordEdit = + Parse.IgnoreCase("edit").Text(); + private static readonly Parser _keywordDelete = Parse.IgnoreCase("delete") .Or(Parse.IgnoreCase("del")) @@ -78,10 +81,21 @@ from _ in Parse.WhiteSpace from keyword in _keywordDone select new DoneCommand(id); + private static readonly Parser _editCommand = + from id in _number + from _ in Parse.WhiteSpace + from keyword in _keywordEdit + from __ in Parse.WhiteSpace + from property in Parse.LetterOrDigit.AtLeastOnce().Text() + from ___ in Parse.WhiteSpace + from value in Parse.LetterOrDigit.AtLeastOnce().Text() + select new EditCommand(id, property, value); + public static readonly Parser Source = _deleteCommand .Or(_nextCommand) .Or(_doneCommand) + .Or(_editCommand) .Or(_readCommand) .Or(_addCommandWithPriority) .Or(_addCommand) diff --git a/BusyList/Program.cs b/BusyList/Program.cs index 966bdcb..6b90aa9 100644 --- a/BusyList/Program.cs +++ b/BusyList/Program.cs @@ -65,6 +65,7 @@ private static IServiceCollection ConfigureServices() services.AddTransient, DoneHandler>(); services.AddTransient, NextHandler>(); services.AddTransient, ReadHandler>(); + services.AddTransient, EditHandler>(); return services; } @@ -88,6 +89,9 @@ private static void HandleCommand(ServiceProvider provider, Command command) case ReadCommand read: provider.GetRequiredService>().Run(read); break; + case EditCommand edit: + provider.GetRequiredService>().Run(edit); + break; default: throw new Exception($"Unknown command type {command.GetType().FullName} sent to HandleCommand!"); } diff --git a/BusyList/TaskItem.cs b/BusyList/TaskItem.cs index 9fba288..f19fd77 100644 --- a/BusyList/TaskItem.cs +++ b/BusyList/TaskItem.cs @@ -8,15 +8,13 @@ public TaskItem(int id, string description, PriorityEnum priority = PriorityEnum { Id = id; Description = description; - TaskStatus = status; + Status = status; Priority = priority; } public int Id { get; } - public string Description { get; } - - public TaskStatus TaskStatus { get; set; } - + public string Description { get; set; } + public TaskStatus Status { get; set; } public PriorityEnum Priority { get; set; } public string Print() @@ -26,7 +24,7 @@ public string Print() sb.AppendLine($"Id {Id}"); sb.AppendLine($"Description {Description}"); sb.AppendLine($"Priority {Priority}"); - sb.AppendLine($"Status {TaskStatus}"); + sb.AppendLine($"Status {Status}"); return sb.ToString(); } diff --git a/tests/BusyList.Tests/Handlers/DoneHandlerTests.cs b/tests/BusyList.Tests/Handlers/DoneHandlerTests.cs index 8674865..7a1ba5a 100644 --- a/tests/BusyList.Tests/Handlers/DoneHandlerTests.cs +++ b/tests/BusyList.Tests/Handlers/DoneHandlerTests.cs @@ -25,7 +25,7 @@ public void Run_ShouldSetTaskStatusToDone_WhenAValidIdIsPassed() _taskRepository.Setup(_ => _.GetTaskById(command.Id)).Returns(currentTask); _taskRepository.Setup(_ => _.UpdateTask(It.IsAny())).Callback(task => - task.TaskStatus.Should().Be(TaskStatus.Done) + task.Status.Should().Be(TaskStatus.Done) ); _subject.Run(command);