public AddBlogPostCommandValidator(IUserService userService, IAuthorValidator authorValidator, AppDbContext dbContext) { RuleFor(b => b.Subject).NotEmpty().MaximumLength(100); RuleFor(b => b.ContentIntro).NotEmpty().MaximumLength(120); RuleFor(b => b.Content).NotEmpty().MaximumLength(50000); RuleFor(b => b.Tags).Custom((tags, context) => { if (tags == null) { return; } var invalidTags = tags.Where(tag => string.IsNullOrWhiteSpace(tag)); foreach (var _ in invalidTags) { context.AddFailure(nameof(tags), "Empty tag not allowed."); } }); RuleFor(b => b.CategoryId).NotEmpty().CustomAsync(async(categoryId, context, cancellationToken) => { if (!await dbContext.Categories.AnyAsync(c => c.Id == categoryId)) { context.AddFailure("Category doesn't exit."); } }); RuleFor(b => b).CustomAsync(async(command, context, cancellationToken) => { if (!await authorValidator.Exists(userService)) { context.AddFailure("No author registered."); } }); }
public static async Task <Author> Create(User user, AuthorProfile authorProfile, IAuthorValidator authorValidator) { if (user == null) { throw new ArgumentNullException(nameof(user)); } if (authorProfile == null) { throw new ArgumentNullException(nameof(authorProfile)); } if (authorValidator == null) { throw new ArgumentNullException(nameof(authorValidator)); } if (await authorValidator.Exists(user)) { throw new InvalidOperationException("Only one author can be created per user."); } return(new Author(user, authorProfile)); }