Skip to content

Instantly share code, notes, and snippets.

@hadoan
Created December 29, 2019 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadoan/0a44ed996466513db7c7ac394036057a to your computer and use it in GitHub Desktop.
Save hadoan/0a44ed996466513db7c7ac394036057a to your computer and use it in GitHub Desktop.
public class CompleteTodoCommand : IRequest<bool>
{
public Guid Id { get; set; }
}
public class CompleteTodoCommandHadler : IRequestHandler<CompleteTodoCommand, bool>
{
private readonly TodoDbContext _context;
public CompleteTodoCommandHadler(TodoDbContext context)
{
this._context = context;
}
public async Task<bool> Handle(CompleteTodoCommand request, CancellationToken cancellationToken)
{
var todo = await _context.Todoes.FindAsync(request.Id);
todo.Completed = true;
_context.Todoes.Update(todo);
var summary = await _context.TodoSummaries.Where(x => x.Date == DateTime.Today).FirstOrDefaultAsync();
if (summary == null)
{
summary = new TodoSummary { Date = DateTime.Today, CompletedCount = 1 };
_context.TodoSummaries.Add(summary);
}
else
{
summary.CompletedCount += 1;
_context.TodoSummaries.Update(summary);
}
await _context.SaveChangesAsync();
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment