Created
November 22, 2020 08:12
-
-
Save sunilkumarmedium/6183a0ede703f9acc5b049ae9b2257c6 to your computer and use it in GitHub Desktop.
CreateUserCommandValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CleanArchitectureApp.Application.Features.Users.Commands | |
{ | |
public class CreateUserCommandValidator : AbstractValidator<CreateUserCommand> | |
{ | |
private readonly IUserRepositoryAsync userRepository; | |
public CreateUserCommandValidator(IUserRepositoryAsync userRepository) | |
{ | |
this.userRepository = userRepository; | |
RuleFor(p => p.UserName) | |
.NotEmpty().WithMessage("{PropertyName} is required.") | |
.NotNull() | |
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters.") | |
.MustAsync(IsUniqueUserName).WithMessage("{PropertyName} already exists."); | |
RuleFor(p => p.Password) | |
.NotEmpty().WithMessage("{PropertyName} is required.") | |
.NotNull() | |
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters."); | |
RuleFor(p => p.FirstName) | |
.NotEmpty().WithMessage("{PropertyName} is required.") | |
.NotNull() | |
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters."); | |
RuleFor(p => p.UserEmail) | |
.NotEmpty().WithMessage("{PropertyName} is required.") | |
.NotNull() | |
.MaximumLength(50).WithMessage("{PropertyName} must not exceed 50 characters."); | |
RuleFor(p => p.CreatedBy) | |
.NotEmpty().WithMessage("{PropertyName} is required.") | |
.NotNull() | |
.WithMessage("{PropertyName} must not exceed 50 characters."); | |
} | |
private async Task<bool> IsUniqueUserName(string username, CancellationToken cancellationToken) | |
{ | |
var userObject = (await userRepository.FindByCondition(x => x.UserName.ToLower() == username.ToLower()).ConfigureAwait(false)).AsQueryable().FirstOrDefault(); | |
if (userObject != null) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment