Created
December 29, 2019 05:12
-
-
Save hadoan/0a44ed996466513db7c7ac394036057a 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
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