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