예제 #1
0
        public InviteMemberValidator(IMemberRepository memberRepository, IGuildRepository guildRepository)
        {
            RuleFor(x => x.MemberId)
            .NotEmpty()
            .MustAsync(async(memberId, _) => await memberRepository.ExistsWithIdAsync(memberId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.MemberId));

            RuleFor(x => x.GuildId)
            .NotEmpty()
            .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId))
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.GuildId));

            RuleFor(x => x)
            .MustAsync(async(x, _) => !await guildRepository.ExistsAsync(y =>
                                                                         y.Id.Equals(x.GuildId) &&
                                                                         y.Members.Any(m => m.Id.Equals(x.MemberId))
                                                                         ))
            .WithMessage(x => string.Format(
                             "{0} already in target {1} with key {2}", nameof(Member), nameof(Guild), x.GuildId));
        }
        public MembershipValidator(IGuildRepository guildRepository, IMemberRepository memberRepository)
        {
            RuleFor(x => x.Since)
            .NotEmpty()
            .WithErrorCode(CommonValidationMessages.ConflictCodeString)
            .WithMessage($"{nameof(Membership)} must have a {nameof(Membership.Since)} start date.");

            RuleFor(x => x.MemberId)
            .NotEmpty()
            .MustAsync(async(x, _) => await memberRepository.ExistsAsync(y => y.Id.Equals(x)))
            .WithErrorCode(CommonValidationMessages.ConflictCodeString)
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.MemberId));

            RuleFor(x => x.GuildId)
            .NotEmpty()
            .MustAsync(async(x, _) => await guildRepository.ExistsAsync(y => y.Id.Equals(x)))
            .WithErrorCode(CommonValidationMessages.ConflictCodeString)
            .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.GuildId));

            RuleFor(x => x.Member.Id).Equal(x => x.MemberId).Unless(x => x.Member is null);

            RuleFor(x => x.Guild.Id).Equal(x => x.GuildId).Unless(x => x.Guild is null);
        }