63 lines
1.5 KiB
C#
63 lines
1.5 KiB
C#
using Application.Common.Repositories;
|
|
using Domain.Entities;
|
|
using FluentValidation;
|
|
using MediatR;
|
|
|
|
namespace Application.Features.CustomerManager.Commands;
|
|
|
|
public class DeleteCustomerResult
|
|
{
|
|
public Customer? Data { get; set; }
|
|
}
|
|
|
|
public class DeleteCustomerRequest : IRequest<DeleteCustomerResult>
|
|
{
|
|
public string? Id { get; init; }
|
|
public string? DeletedById { get; init; }
|
|
}
|
|
|
|
public class DeleteCustomerValidator : AbstractValidator<DeleteCustomerRequest>
|
|
{
|
|
public DeleteCustomerValidator()
|
|
{
|
|
RuleFor(x => x.Id).NotEmpty();
|
|
}
|
|
}
|
|
|
|
public class DeleteCustomerHandler : IRequestHandler<DeleteCustomerRequest, DeleteCustomerResult>
|
|
{
|
|
private readonly ICommandRepository<Customer> _repository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public DeleteCustomerHandler(
|
|
ICommandRepository<Customer> repository,
|
|
IUnitOfWork unitOfWork
|
|
)
|
|
{
|
|
_repository = repository;
|
|
_unitOfWork = unitOfWork;
|
|
}
|
|
|
|
public async Task<DeleteCustomerResult> Handle(DeleteCustomerRequest request, CancellationToken cancellationToken)
|
|
{
|
|
|
|
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
|
|
|
|
if (entity == null)
|
|
{
|
|
throw new Exception($"Entity not found: {request.Id}");
|
|
}
|
|
|
|
entity.UpdatedById = request.DeletedById;
|
|
|
|
_repository.Delete(entity);
|
|
await _unitOfWork.SaveAsync(cancellationToken);
|
|
|
|
return new DeleteCustomerResult
|
|
{
|
|
Data = entity
|
|
};
|
|
}
|
|
}
|
|
|