Files
CMS/src/CMSMicroservice.Application/UserWalletCQ/Commands/DeleteUserWallet/DeleteUserWalletCommandHandler.cs
2025-11-12 02:24:02 +03:30

23 lines
917 B
C#

using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.UserWalletCQ.Commands.DeleteUserWallet;
public class DeleteUserWalletCommandHandler : IRequestHandler<DeleteUserWalletCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeleteUserWalletCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeleteUserWalletCommand request, CancellationToken cancellationToken)
{
var entity = await _context.UserWallets
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(UserWallet), request.Id);
entity.IsDeleted = true;
_context.UserWallets.Update(entity);
entity.AddDomainEvent(new DeleteUserWalletEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}