Created
December 29, 2019 05:03
-
-
Save hadoan/58dd4ef69283fb7429c4d38336302d4c 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 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