Make DeleteRecActionsCommand robust and async-safe
Enhanced the DeleteRecActionsCommandHandler to include a check for the existence of records before deletion, throwing a NotFoundException if none are found. Made the Handle method asynchronous and added necessary namespace imports for exception handling and database operations. Added a comment to suggest updating the DeleteAsync method in the Core library to return the number of deleted records.
This commit is contained in:
parent
1cb9ce18b4
commit
771eb80b9e
@ -1,5 +1,7 @@
|
|||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
|
using DigitalData.Core.Exceptions;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using ReC.Domain.Entities;
|
using ReC.Domain.Entities;
|
||||||
|
|
||||||
namespace ReC.Application.RecActions.Commands;
|
namespace ReC.Application.RecActions.Commands;
|
||||||
@ -11,8 +13,12 @@ public class DeleteRecActionsCommand : IRequest
|
|||||||
|
|
||||||
public class DeleteRecActionsCommandHandler(IRepository<RecAction> repo) : IRequestHandler<DeleteRecActionsCommand>
|
public class DeleteRecActionsCommandHandler(IRepository<RecAction> repo) : IRequestHandler<DeleteRecActionsCommand>
|
||||||
{
|
{
|
||||||
public Task Handle(DeleteRecActionsCommand request, CancellationToken cancel)
|
public async Task Handle(DeleteRecActionsCommand request, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
return repo.DeleteAsync(act => act.ProfileId == request.ProfileId, cancel);
|
// TODO: update DeleteAsync (in Core) to return number of deleted records
|
||||||
|
if (!await repo.Where(act => act.ProfileId == request.ProfileId).AnyAsync(cancel))
|
||||||
|
throw new NotFoundException();
|
||||||
|
|
||||||
|
await repo.DeleteAsync(act => act.ProfileId == request.ProfileId, cancel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user