27 lines
931 B
C#
27 lines
931 B
C#
using Indotalent.Infrastructure.Database;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Indotalent.Features.Payroll.Deduction.Cqrs;
|
|
|
|
public record DeleteDeductionByIdRequest(string Id);
|
|
|
|
public record DeleteDeductionByIdCommand(DeleteDeductionByIdRequest Data) : IRequest<bool>;
|
|
|
|
public class DeleteDeductionByIdHandler : IRequestHandler<DeleteDeductionByIdCommand, bool>
|
|
{
|
|
private readonly AppDbContext _context;
|
|
|
|
public DeleteDeductionByIdHandler(AppDbContext context) => _context = context;
|
|
|
|
public async Task<bool> Handle(DeleteDeductionByIdCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var entity = await _context.Deduction
|
|
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
|
|
|
if (entity == null) return false;
|
|
|
|
_context.Deduction.Remove(entity);
|
|
return await _context.SaveChangesAsync(cancellationToken) > 0;
|
|
}
|
|
} |