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