Api/Brizco.Repository/Handlers/Complexes/DeleteComplexCommandHandler.cs

29 lines
1.1 KiB
C#

namespace Brizco.Repository.Handlers.Complexes;
public class DeleteComplexCommandHandler(IRepositoryWrapper repositoryWrapper)
: IRequestHandler<DeleteComplexCommand, bool>
{
public async Task<bool> Handle(DeleteComplexCommand request, CancellationToken cancellationToken)
{
try
{
await repositoryWrapper.BeginTransaction(cancellationToken);
var task = await repositoryWrapper.SetRepository<Complex>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
if (task == null)
throw new AppException("Task not found", ApiResultStatusCode.NotFound);
repositoryWrapper.SetRepository<Complex>()
.Delete(task);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
await repositoryWrapper.CommitAsync(cancellationToken);
return true;
}
catch (Exception)
{
await repositoryWrapper.RollBackAsync(cancellationToken);
throw;
}
}
}