Generator Changes at 9/27/2025 8:46:36 AM
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.DeletePackage;
|
||||
public record DeletePackageCommand : IRequest<Unit>
|
||||
{
|
||||
//شناسه
|
||||
public long Id { get; init; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace CMSMicroservice.Application.PackageCQ.Commands.DeletePackage;
|
||||
public class DeletePackageCommandValidator : AbstractValidator<DeletePackageCommand>
|
||||
{
|
||||
public DeletePackageCommandValidator()
|
||||
{
|
||||
RuleFor(model => model.Id)
|
||||
.NotNull();
|
||||
}
|
||||
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
|
||||
{
|
||||
var result = await ValidateAsync(ValidationContext<DeletePackageCommand>.CreateWithOptions((DeletePackageCommand)model, x => x.IncludeProperties(propertyName)));
|
||||
if (result.IsValid)
|
||||
return Array.Empty<string>();
|
||||
return result.Errors.Select(e => e.ErrorMessage);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user