From 249af4576f3dcfe069ec0a570e4e0fc50bfa9d8a Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Mon, 24 May 2021 21:04:26 +0200 Subject: [PATCH 1/7] Add edit command --- BusyList/Commands/Commands.cs | 1 + BusyList/Handlers/DoneHandler.cs | 6 ++++++ BusyList/Handlers/EditHandler.cs | 26 ++++++++++++++++++++++++++ BusyList/Parsing/CommandGrammar.cs | 14 ++++++++++++++ BusyList/Program.cs | 4 ++++ 5 files changed, 51 insertions(+) create mode 100644 BusyList/Handlers/EditHandler.cs diff --git a/BusyList/Commands/Commands.cs b/BusyList/Commands/Commands.cs index f62c16e..5c20050 100644 --- a/BusyList/Commands/Commands.cs +++ b/BusyList/Commands/Commands.cs @@ -7,4 +7,5 @@ public record NextCommand() : Command; public record AddCommand(string Description) : 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..738a773 100644 --- a/BusyList/Handlers/DoneHandler.cs +++ b/BusyList/Handlers/DoneHandler.cs @@ -16,6 +16,12 @@ public void Run(DoneCommand command) { TaskItem taskItem = _taskRepository.GetTaskById(command.Id); + if(taskItem == null) + { + Console.WriteLine($"The task with the id {command.Id} does not exist"); + return; + } + taskItem.TaskStatus = TaskStatus.Done; _taskRepository.UpdateTask(taskItem); diff --git a/BusyList/Handlers/EditHandler.cs b/BusyList/Handlers/EditHandler.cs new file mode 100644 index 0000000..8a5def8 --- /dev/null +++ b/BusyList/Handlers/EditHandler.cs @@ -0,0 +1,26 @@ +using BusyList.Commands; +using System.Reflection; + +namespace BusyList.Handlers +{ + 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); + + PropertyInfo propertyInfo = taskItem.GetType().GetProperty(command.property); + + propertyInfo.SetValue(taskItem, command.value); + + _taskRepository.UpdateTask(taskItem); + } + } +} diff --git a/BusyList/Parsing/CommandGrammar.cs b/BusyList/Parsing/CommandGrammar.cs index dc8ed99..4ea0e86 100644 --- a/BusyList/Parsing/CommandGrammar.cs +++ b/BusyList/Parsing/CommandGrammar.cs @@ -9,6 +9,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")) @@ -49,10 +52,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.AnyChar.AtLeastOnce().Text() + from ___ in Parse.WhiteSpace + from value in Parse.AnyChar.AtLeastOnce().Text() + select new EditCommand(id, property, value); + public static readonly Parser Source = _deleteCommand .Or(_nextCommand) .Or(_doneCommand) + .Or(_editCommand) .Or(_readCommand) .Or(_addCommand) .End(); 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!"); } From 92149b172bfcf587021eaf3ca8f1b0d6d804017a Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Mon, 24 May 2021 21:56:38 +0200 Subject: [PATCH 2/7] Implement edit command --- BusyList/Handlers/DoneHandler.cs | 2 +- BusyList/Handlers/EditHandler.cs | 15 ++++++++++++--- BusyList/Handlers/NextHandler.cs | 2 +- BusyList/Parsing/CommandGrammar.cs | 4 ++-- BusyList/TaskItem.cs | 8 ++++---- tests/BusyList.Tests/Handlers/DoneHandlerTests.cs | 2 +- 6 files changed, 21 insertions(+), 12 deletions(-) diff --git a/BusyList/Handlers/DoneHandler.cs b/BusyList/Handlers/DoneHandler.cs index 738a773..481437b 100644 --- a/BusyList/Handlers/DoneHandler.cs +++ b/BusyList/Handlers/DoneHandler.cs @@ -22,7 +22,7 @@ public void Run(DoneCommand command) return; } - taskItem.TaskStatus = TaskStatus.Done; + taskItem.Status = TaskStatus.Done; _taskRepository.UpdateTask(taskItem); diff --git a/BusyList/Handlers/EditHandler.cs b/BusyList/Handlers/EditHandler.cs index 8a5def8..d761405 100644 --- a/BusyList/Handlers/EditHandler.cs +++ b/BusyList/Handlers/EditHandler.cs @@ -1,4 +1,5 @@ using BusyList.Commands; +using System; using System.Reflection; namespace BusyList.Handlers @@ -16,11 +17,19 @@ public void Run(EditCommand command) { TaskItem taskItem = _taskRepository.GetTaskById(command.Id); - PropertyInfo propertyInfo = taskItem.GetType().GetProperty(command.property); + if (taskItem != null) + { + PropertyInfo propertyInfo = taskItem.GetType().GetProperty(command.property); - propertyInfo.SetValue(taskItem, command.value); + if (propertyInfo != null) + { + propertyInfo.SetValue(taskItem, command.value); - _taskRepository.UpdateTask(taskItem); + _taskRepository.UpdateTask(taskItem); + + Console.WriteLine("Task updated"); + } + } } } } diff --git a/BusyList/Handlers/NextHandler.cs b/BusyList/Handlers/NextHandler.cs index f07f881..2c68907 100644 --- a/BusyList/Handlers/NextHandler.cs +++ b/BusyList/Handlers/NextHandler.cs @@ -25,7 +25,7 @@ public void Run(NextCommand command) { Console.WriteLine($"Task id: {item.Id}"); Console.WriteLine($"Description: {item.Description}"); - Console.WriteLine($"Status: {item.TaskStatus}"); + Console.WriteLine($"Status: {item.Status}"); Console.WriteLine(SEPERATOR); } } diff --git a/BusyList/Parsing/CommandGrammar.cs b/BusyList/Parsing/CommandGrammar.cs index 4ea0e86..fbeda51 100644 --- a/BusyList/Parsing/CommandGrammar.cs +++ b/BusyList/Parsing/CommandGrammar.cs @@ -57,9 +57,9 @@ from id in _number from _ in Parse.WhiteSpace from keyword in _keywordEdit from __ in Parse.WhiteSpace - from property in Parse.AnyChar.AtLeastOnce().Text() + from property in Parse.LetterOrDigit.AtLeastOnce().Text() from ___ in Parse.WhiteSpace - from value in Parse.AnyChar.AtLeastOnce().Text() + from value in Parse.LetterOrDigit.AtLeastOnce().Text() select new EditCommand(id, property, value); public static readonly Parser Source = diff --git a/BusyList/TaskItem.cs b/BusyList/TaskItem.cs index e8f20f7..c0b25db 100644 --- a/BusyList/TaskItem.cs +++ b/BusyList/TaskItem.cs @@ -8,13 +8,13 @@ public TaskItem(int id, string description, TaskStatus status = TaskStatus.NotSt { Id = id; Description = description; - TaskStatus = status; + Status = status; } public int Id { get; } - public string Description { get; } + public string Description { get; set; } - public TaskStatus TaskStatus { get; set; } + public TaskStatus Status { get; set; } public string Print() { @@ -22,7 +22,7 @@ public string Print() sb.AppendLine($"Id {Id}"); sb.AppendLine($"Description {Description}"); - 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 b8bda84..eab071a 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); From 0e0ddb2e613fa54b63b31a0c545d0c48284c3181 Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Tue, 25 May 2021 18:05:26 +0200 Subject: [PATCH 3/7] Add some printings --- BusyList/Handlers/EditHandler.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/BusyList/Handlers/EditHandler.cs b/BusyList/Handlers/EditHandler.cs index d761405..452acdc 100644 --- a/BusyList/Handlers/EditHandler.cs +++ b/BusyList/Handlers/EditHandler.cs @@ -29,6 +29,14 @@ public void Run(EditCommand command) 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"); } } } From 5895343816638cae5db76b182ef243e669b9acf5 Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Fri, 28 May 2021 16:44:23 +0200 Subject: [PATCH 4/7] Add hint --- BusyList/Handlers/EditHandler.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BusyList/Handlers/EditHandler.cs b/BusyList/Handlers/EditHandler.cs index 452acdc..61f89d3 100644 --- a/BusyList/Handlers/EditHandler.cs +++ b/BusyList/Handlers/EditHandler.cs @@ -4,6 +4,8 @@ namespace BusyList.Handlers { + // There are some problems. + // Only work for string properties e.g description. public class EditHandler : IHandler { private readonly ITaskRepository _taskRepository; From 8738d67a779fdec98cd3f491076fb761340760c1 Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Tue, 6 Jul 2021 17:41:42 +0200 Subject: [PATCH 5/7] Edit edit command --- BusyList/Commands/Commands.cs | 2 +- BusyList/Handlers/EditHandler.cs | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/BusyList/Commands/Commands.cs b/BusyList/Commands/Commands.cs index 5c20050..d756ebb 100644 --- a/BusyList/Commands/Commands.cs +++ b/BusyList/Commands/Commands.cs @@ -7,5 +7,5 @@ public record NextCommand() : Command; public record AddCommand(string Description) : Command; public record DeleteCommand(int Id) : Command; public record DoneCommand(int Id) : Command; - public record EditCommand(int Id, string property, string value) : Command; + public record EditCommand(int Id, string Property, string Value) : Command; } diff --git a/BusyList/Handlers/EditHandler.cs b/BusyList/Handlers/EditHandler.cs index 61f89d3..85636df 100644 --- a/BusyList/Handlers/EditHandler.cs +++ b/BusyList/Handlers/EditHandler.cs @@ -21,11 +21,16 @@ public void Run(EditCommand command) if (taskItem != null) { - PropertyInfo propertyInfo = taskItem.GetType().GetProperty(command.property); + 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); + propertyInfo.SetValue(taskItem, command.Value); _taskRepository.UpdateTask(taskItem); @@ -33,7 +38,7 @@ public void Run(EditCommand command) } else { - Console.WriteLine($"The property with the name {command.property} does not exist"); + Console.WriteLine($"The property with the name {command.Property} does not exist"); } } else From 1a60800c08b2eb8c4697bd9182fc538dd7c929f1 Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Wed, 7 Jul 2021 08:55:35 +0200 Subject: [PATCH 6/7] Chnage Taskstatus to status --- BusyList/TaskItem.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BusyList/TaskItem.cs b/BusyList/TaskItem.cs index 267f84c..53fffcf 100644 --- a/BusyList/TaskItem.cs +++ b/BusyList/TaskItem.cs @@ -14,7 +14,7 @@ public TaskItem(int id, string description, PriorityEnum priority = PriorityEnum public int Id { get; } public string Description { get; set; } - public TaskStatus TaskStatus { get; set; } + public TaskStatus Status { get; set; } public PriorityEnum Priority { get; set; } public string Print() From 07b7e72a5dcb6fd28c2de70577a7de287710266b Mon Sep 17 00:00:00 2001 From: unbekanntunity Date: Mon, 6 Sep 2021 15:24:58 +0200 Subject: [PATCH 7/7] Rename the other taskstatus to status --- BusyList/TaskItem.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BusyList/TaskItem.cs b/BusyList/TaskItem.cs index 53fffcf..f19fd77 100644 --- a/BusyList/TaskItem.cs +++ b/BusyList/TaskItem.cs @@ -8,7 +8,7 @@ public TaskItem(int id, string description, PriorityEnum priority = PriorityEnum { Id = id; Description = description; - TaskStatus = status; + Status = status; Priority = priority; } public int Id { get; } @@ -24,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(); }