Files
CMS/src/CMSMicroservice.Application/PackageCQ/Commands/DeletePackage/DeletePackageCommandHandler.cs
2025-09-27 08:46:36 +03:30

23 lines
887 B
C#

using CMSMicroservice.Domain.Events;
namespace CMSMicroservice.Application.PackageCQ.Commands.DeletePackage;
public class DeletePackageCommandHandler : IRequestHandler<DeletePackageCommand, Unit>
{
private readonly IApplicationDbContext _context;
public DeletePackageCommandHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<Unit> Handle(DeletePackageCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Packages
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Package), request.Id);
entity.IsDeleted = true;
_context.Packages.Update(entity);
entity.AddDomainEvent(new DeletePackageEvent(entity));
await _context.SaveChangesAsync(cancellationToken);
return Unit.Value;
}
}