Files
social-media/src/Application/TodoItems/Commands/UpdateTodoItem/UpdateTodoItem.cs
2024-03-30 20:59:17 -04:00

31 lines
818 B
C#

using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.TodoItems.Commands.UpdateTodoItem;
public record UpdateTodoItemCommand : IRequest
{
public int Id { get; init; }
public string? Title { get; init; }
public bool Done { get; init; }
}
public class UpdateTodoItemCommandHandler(
IApplicationDbContext context)
: IRequestHandler<UpdateTodoItemCommand>
{
public async Task Handle(UpdateTodoItemCommand request, CancellationToken cancellationToken)
{
var entity = await context.TodoItems
.FindAsync(new object[] { request.Id }, cancellationToken);
Guard.Against.NotFound(request.Id, entity);
entity.Title = request.Title;
entity.Done = request.Done;
await context.SaveChangesAsync(cancellationToken);
}
}