diff --git a/src/CMSMicroservice.Application/UserCQ/Commands/UpdateUser/UpdateUserCommandHandler.cs b/src/CMSMicroservice.Application/UserCQ/Commands/UpdateUser/UpdateUserCommandHandler.cs index 468e1c3..1013c8c 100644 --- a/src/CMSMicroservice.Application/UserCQ/Commands/UpdateUser/UpdateUserCommandHandler.cs +++ b/src/CMSMicroservice.Application/UserCQ/Commands/UpdateUser/UpdateUserCommandHandler.cs @@ -1,5 +1,7 @@ using CMSMicroservice.Domain.Events; + namespace CMSMicroservice.Application.UserCQ.Commands.UpdateUser; + public class UpdateUserCommandHandler : IRequestHandler { private readonly IApplicationDbContext _context; @@ -11,16 +13,19 @@ public class UpdateUserCommandHandler : IRequestHandler public async Task Handle(UpdateUserCommand request, CancellationToken cancellationToken) { - var entity = await _context.Users - .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(User), request.Id); - if (!string.IsNullOrWhiteSpace(request.NationalCode) && entity.NationalCode == request.NationalCode) + if (!string.IsNullOrWhiteSpace(request.NationalCode) && + _context.Users.Any(a => a.NationalCode == request.NationalCode)) { throw new Exception("کد ملی تکراری است"); } + + var entity = await _context.Users + .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? + throw new NotFoundException(nameof(User), request.Id); request.Adapt(entity); _context.Users.Update(entity); entity.AddDomainEvent(new UpdateUserEvent(entity)); await _context.SaveChangesAsync(cancellationToken); return Unit.Value; } -} +} \ No newline at end of file