Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions BusyList/Commands/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
8 changes: 7 additions & 1 deletion BusyList/Handlers/DoneHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
50 changes: 50 additions & 0 deletions BusyList/Handlers/EditHandler.cs
Original file line number Diff line number Diff line change
@@ -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<EditCommand>
{
private readonly ITaskRepository _taskRepository;

public EditHandler(ITaskRepository taskRepository)
{
_taskRepository = taskRepository;
}

public void Run(EditCommand command)
{
TaskItem taskItem = _taskRepository.GetTaskById(command.Id);

if (taskItem != null)
Comment thread
unbekanntunity marked this conversation as resolved.
{
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");
}
}
}
}
14 changes: 14 additions & 0 deletions BusyList/Parsing/CommandGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public static class CommandGrammar
private static readonly Parser<string> _keywordAdd =
Parse.IgnoreCase("add").Text();

private static readonly Parser<string> _keywordEdit =
Parse.IgnoreCase("edit").Text();

private static readonly Parser<string> _keywordDelete =
Parse.IgnoreCase("delete")
.Or(Parse.IgnoreCase("del"))
Expand Down Expand Up @@ -78,10 +81,21 @@ from _ in Parse.WhiteSpace
from keyword in _keywordDone
select new DoneCommand(id);

private static readonly Parser<Command> _editCommand =
from id in _number
from _ in Parse.WhiteSpace
from keyword in _keywordEdit
from __ in Parse.WhiteSpace
from property in Parse.LetterOrDigit.AtLeastOnce().Text()
Comment thread
unbekanntunity marked this conversation as resolved.
from ___ in Parse.WhiteSpace
from value in Parse.LetterOrDigit.AtLeastOnce().Text()
select new EditCommand(id, property, value);

public static readonly Parser<Command> Source =
_deleteCommand
.Or(_nextCommand)
.Or(_doneCommand)
.Or(_editCommand)
.Or(_readCommand)
.Or(_addCommandWithPriority)
.Or(_addCommand)
Expand Down
4 changes: 4 additions & 0 deletions BusyList/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ private static IServiceCollection ConfigureServices()
services.AddTransient<IHandler<DoneCommand>, DoneHandler>();
services.AddTransient<IHandler<NextCommand>, NextHandler>();
services.AddTransient<IHandler<ReadCommand>, ReadHandler>();
services.AddTransient<IHandler<EditCommand>, EditHandler>();

return services;
}
Expand All @@ -88,6 +89,9 @@ private static void HandleCommand(ServiceProvider provider, Command command)
case ReadCommand read:
provider.GetRequiredService<IHandler<ReadCommand>>().Run(read);
break;
case EditCommand edit:
provider.GetRequiredService<IHandler<EditCommand>>().Run(edit);
break;
default:
throw new Exception($"Unknown command type {command.GetType().FullName} sent to HandleCommand!");
}
Expand Down
6 changes: 2 additions & 4 deletions BusyList/TaskItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ public TaskItem(int id, string description, PriorityEnum priority = PriorityEnum
}
public int Id { get; }

public string Description { get; }

public TaskStatus TaskStatus { get; set; }

public string Description { get; set; }
public TaskStatus Status { get; set; }
Comment thread
unbekanntunity marked this conversation as resolved.
public PriorityEnum Priority { get; set; }
Comment thread
unbekanntunity marked this conversation as resolved.

public string Print()
Expand Down
2 changes: 1 addition & 1 deletion tests/BusyList.Tests/Handlers/DoneHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void Run_ShouldSetTaskStatusToDone_WhenAValidIdIsPassed()
_taskRepository.Setup(_ => _.GetTaskById(command.Id)).Returns(currentTask);

_taskRepository.Setup(_ => _.UpdateTask(It.IsAny<TaskItem>())).Callback<TaskItem>(task =>
task.TaskStatus.Should().Be(TaskStatus.Done)
task.Status.Should().Be(TaskStatus.Done)
);

_subject.Run(command);
Expand Down