Skip to content

Instantly share code, notes, and snippets.

@AndyButland
Created September 26, 2016 09:09
Show Gist options
  • Save AndyButland/8cf0cd5217e287424d6ecd599c23b02d to your computer and use it in GitHub Desktop.
Save AndyButland/8cf0cd5217e287424d6ecd599c23b02d to your computer and use it in GitHub Desktop.
using System.Threading.Tasks;
using MediatR;
public class TaskCompleteCommand : IAsyncRequest<CommandResult>
{
public int Id { get; set; }
}
public class TaskCompleteCommandHandler : CommandHandlerBase,
IAsyncRequestHandler<TaskCompleteCommand, CommandResult>
{
public TaskCompleteCommandHandler(ToDoContext context)
: base(context)
{
}
public async Task<CommandResult> Handle(
TaskCompleteCommand command)
{
var task = await GetTask(command.Id);
task.MarkComplete();
await Context.SaveChangesAsync();
return CommandResult.SuccessResult();
}
private async Task<Models.Task> GetTask(int id)
{
var task = await Context.Tasks
.SingleOrDefaultAsync(x => x.Id == id);
if (task == null)
{
throw new NullReferenceException($"Task with id: {id} not found.");
}
return task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment