public UpdateGuildValidator(IGuildRepository guildRepository, IMemberRepository memberRepository) { RuleFor(x => x.Id).NotEmpty(); RuleFor(x => x.Name).NotEmpty(); RuleFor(x => x.LeaderId).NotEmpty(); When(x => x.Name != string.Empty && x.Id != Guid.Empty && x.LeaderId != Guid.Empty, () => { RuleFor(x => x) .MustAsync((x, ct) => guildRepository.ExistsWithIdAsync(x.Id, ct)) .WithMessage(x => $"Record not found for guild with given id {x.Id}.") .WithName("Id") .WithErrorCode(nameof(HttpStatusCode.NotFound)) .MustAsync((x, ct) => memberRepository.ExistsWithIdAsync(x.LeaderId, ct)) .WithMessage(x => $"Record not found for member with given id {x.LeaderId}.") .WithName("LeaderId") .WithErrorCode(nameof(HttpStatusCode.NotFound)) .MustAsync((x, ct) => guildRepository.CanChangeNameAsync(x.Id, x.Name, ct)) .WithMessage(x => $"Record already exists for guild with given name {x.Name}.") .WithName("Name") .WithErrorCode(nameof(HttpStatusCode.Conflict)) .MustAsync((x, ct) => memberRepository.IsGuildMemberAsync(x.LeaderId, x.Id, ct)) .WithMessage("Member chosen for Guild Master must be member of target Guild.") .WithName("LeaderId") .WithErrorCode(nameof(HttpStatusCode.UnprocessableEntity)); }); }
public AcceptInviteValidator(IInviteRepository inviteRepository, IMemberRepository memberRepository, IGuildRepository guildRepository) { RuleFor(x => x.Id) .NotEmpty() .MustAsync(async(id, _) => await inviteRepository.ExistsWithIdAsync(id)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Invite), x.Invite.Id)); RuleFor(x => x.Invite) .NotEmpty().NotEqual(new NullInvite()) .WithMessage("Invite was null or empty."); RuleFor(x => x.Invite.Status).IsInEnum().Equal(InviteStatuses.Pending); RuleFor(x => x.Invite.MemberId) .NotEmpty() .MustAsync(async(memberId, _) => await memberRepository.ExistsWithIdAsync(memberId)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.Invite.MemberId)); RuleFor(x => x.Invite.GuildId) .NotEmpty() .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.Invite.GuildId)); }
public UpdateGuildValidator(IGuildRepository guildRepository, IMemberRepository memberRepository) { RuleFor(x => x.Id) .NotEmpty() .MustAsync(async(id, cancellationToken) => await guildRepository.ExistsWithIdAsync(id, cancellationToken)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.Id)); RuleFor(x => x.Name) .NotEmpty() .MustAsync(async(name, cancellationToken) => !await memberRepository.ExistsWithNameAsync(name, cancellationToken)) .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Guild), x.Name)) .UnlessAsync(async(x, cancellationToken) => { var member = await memberRepository.GetByNameAsync(x.Name, true, cancellationToken); return(x.Id.Equals(member.Id)); }); RuleFor(x => x.MasterId) .NotEmpty() .MustAsync(async(masterId, cancellationToken) => await memberRepository.ExistsWithIdAsync(masterId, cancellationToken)) .WithMessage("Member chosen for Guild Master not found."); RuleFor(x => x) .MustAsync(async(x, cancellationToken) => (await memberRepository.GetByIdAsync(x.MasterId, true, cancellationToken)) .GuildId.Equals(x.Id)) .WithMessage("Member chosen for Guild Master must be a Member of target Guild."); }
public MemberValidator(IGuildRepository guildRepository) { RuleFor(x => x.Data.Name).NotEmpty(); RuleFor(x => x.Data) .Must(x => x.Memberships.All(m => m.MemberId.Equals(x.Id))) .WithMessage(x => $"Not all {nameof(Membership)}s with '{nameof(Membership.MemberId)}' matching '{x.Data.Id}'."); RuleFor(x => x.Data.GuildId).NotEmpty().Unless(x => x.Data.Guild is null); RuleFor(x => x.Data.GuildId) .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId.Value)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.Data.GuildId.Value)) .When(x => x.Data.GuildId.HasValue); RuleFor(x => x.Data.Guild.Id) .Equal(x => x.Data.GuildId.Value) .WithMessage(x => $"{nameof(Member.GuildId)} and {nameof(Guild)}.{nameof(Member.Guild.Id)} not matching.") .Unless(x => x.Data.Guild is null); RuleFor(x => x) .Must(x => x.Data.Memberships.Any(ms => ms.MemberId == x.Data.Id && ms.GuildId == x.Data.GuildId && ms.Until == null && !ms.Disabled)) .WithMessage(x => $"{nameof(Member)} missing active {nameof(Membership)} for {nameof(Member.GuildId)} '{x.Data.GuildId}'.") .When(x => x.Data.Memberships.Any() && x.Data.GuildId.HasValue && !x.Data.GuildId.Equals(Guid.Empty)); }
public UpdateMemberValidator(IMemberRepository memberRepository, IGuildRepository guildRepository) { RuleFor(x => x.Id) .NotEmpty() .MustAsync(async(id, _) => await memberRepository.ExistsWithIdAsync(id)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Member), x.Id)); RuleFor(x => x.Name) .NotEmpty() .MustAsync(async(name, _) => !await memberRepository.ExistsWithNameAsync(name)) .WithMessage(x => CommonValidationMessages.ForConflictWithKey(nameof(Member), x.Name)) .Unless(x => x.Id.Equals(memberRepository.Query().SingleOrDefault(y => y.Name.Equals(x.Name)).Id)); RuleFor(x => x.GuildId) .MustAsync(async(guildId, _) => await guildRepository.ExistsWithIdAsync(guildId)) .WithMessage(x => CommonValidationMessages.ForRecordNotFound(nameof(Guild), x.GuildId)) .When(x => x.GuildId != Guid.Empty && x.GuildId != null); }
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 InviteMemberValidator(IMemberRepository memberRepository, IGuildRepository guildRepository) { RuleFor(x => x.MemberId).NotEmpty(); RuleFor(x => x.GuildId).NotEmpty(); When(x => x.MemberId != Guid.Empty && x.GuildId != Guid.Empty, () => { RuleFor(x => x) .MustAsync((x, ct) => memberRepository.ExistsWithIdAsync(x.MemberId, ct)) .WithMessage(x => $"Record not found for invited member with given id {x.MemberId}.") .WithName(x => nameof(x.MemberId)) .WithErrorCode(nameof(HttpStatusCode.NotFound)) .MustAsync((x, ct) => guildRepository.ExistsWithIdAsync(x.GuildId, ct)) .WithMessage(x => $"Record not found for inviting guild with given id {x.GuildId}.") .WithName(x => nameof(x.GuildId)) .WithErrorCode(nameof(HttpStatusCode.NotFound)) .MustAsync(async(x, ct) => !await memberRepository.IsGuildMemberAsync(x.MemberId, x.GuildId, ct)) .WithMessage("Member is already in target guild.") .WithName(x => nameof(x.MemberId)) .WithErrorCode(nameof(HttpStatusCode.Conflict)); }); }