Add validation to prevent duplicate national codes in user updates
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user