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