Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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) : 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
45 changes: 45 additions & 0 deletions BusyList/Handlers/EditHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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.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");
}
}
}
}
2 changes: 1 addition & 1 deletion BusyList/Handlers/NextHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
14 changes: 14 additions & 0 deletions BusyList/Parsing/CommandGrammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 @@ -49,10 +52,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(_addCommand)
.End();
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
8 changes: 4 additions & 4 deletions BusyList/TaskItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ 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; }
Comment thread
unbekanntunity marked this conversation as resolved.

public string Print()
{
var sb = new StringBuilder();

sb.AppendLine($"Id {Id}");
sb.AppendLine($"Description {Description}");
sb.AppendLine($"Status {TaskStatus}");
sb.AppendLine($"Status {Status}");

return sb.ToString();
}
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