Add validation to prevent duplicate national codes in user updates

This commit is contained in:
masoodafar-web
2025-11-17 01:33:35 +03:30
parent 5e860e5f36
commit 41fd6e1424

View File

@@ -13,6 +13,10 @@ public class UpdateUserCommandHandler : IRequestHandler<UpdateUserCommand, Unit>
{
var entity = await _context.Users
.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(User), request.Id);
if (entity.NationalCode == request.NationalCode)
{
throw new Exception("کد ملی تکراری است");
}
request.Adapt(entity);
_context.Users.Update(entity);
entity.AddDomainEvent(new UpdateUserEvent(entity));