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