Created
September 26, 2016 09:09
-
-
Save AndyButland/8cf0cd5217e287424d6ecd599c23b02d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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