Files
blazor-scm/Features/Utilities/Todo/Cqrs/DeleteTodoItemHandler.cs
T
2026-07-21 14:28:43 +07:00

30 lines
863 B
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Utilities.Todo.Cqrs;
public record DeleteTodoItemCommand(string Id) : IRequest<bool>;
public class DeleteTodoItemHandler : IRequestHandler<DeleteTodoItemCommand, bool>
{
private readonly AppDbContext _context;
public DeleteTodoItemHandler(AppDbContext context) => _context = context;
public async Task<bool> Handle(DeleteTodoItemCommand request, CancellationToken cancellationToken)
{
var entity = await _context.TodoItem
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
if (entity == null)
{
return false;
}
_context.TodoItem.Remove(entity);
await _context.SaveChangesAsync(cancellationToken);
return true;
}
}