Skip to content

Instantly share code, notes, and snippets.

@hadoan
Created December 29, 2019 05:03
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/58dd4ef69283fb7429c4d38336302d4c to your computer and use it in GitHub Desktop.
Save hadoan/58dd4ef69283fb7429c4d38336302d4c to your computer and use it in GitHub Desktop.
public class AddTodoCommand : IRequest<Guid>
{
public string Name { get; set; }
}
public class AddTodoCommandHandler : IRequestHandler<AddTodoCommand, Guid>
{
private readonly TodoDbContext _context;
public AddTodoCommandHandler(TodoDbContext context)
{
this._context = context;
}
public async Task<Guid> Handle(AddTodoCommand request, CancellationToken cancellationToken)
{
var todo = new Todo { Name = request.Name };
_context.Todoes.Add(todo);
var summary = await _context.TodoSummaries.Where(x => x.Date == DateTime.Today).FirstOrDefaultAsync();
if (summary == null)
{
summary = new TodoSummary { Date = DateTime.Today, AddedCount = 1 };
_context.TodoSummaries.Add(summary);
}
else
{
summary.AddedCount += 1;
_context.TodoSummaries.Update(summary);
}
await _context.SaveChangesAsync();
return todo.Id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment