public async Task GivenAnId_WhenGetByIdAsync_ThenReturnAnUser()
        {
            var consultant = ConsultantFactory.AddedConsultant(_database.DbContext);

            var result = await _repository.GetByIdAsync(consultant.Id);

            result.Should().NotBeNull();
            result.Should().BeOfType <Consultant>();
            result.Id.Should().Be(consultant.Id);
            result.Username.Should().Be(consultant.Username);
            result.Email.Should().Be(consultant.Email);
            result.Location.Should().Be(consultant.Location);
            result.Availability.Should().Be(consultant.Availability);
            result.NumberOfTickets.Should().Be(consultant.NumberOfTickets);
            result.Skill.Should().Be(consultant.Skill);
        }
        public async Task <Consultant> Handle(GetConsultantByIdQuery request, CancellationToken cancellationToken)
        {
            var consultant = await _repository.GetByIdAsync(request.Id);

            if (consultant == null)
            {
                throw new ArgumentException("Consultant Not Found!", nameof(request));
            }
            HttpRequestEvaluationsApi _request = new HttpRequestEvaluationsApi();
            var averageScore = _request.GetAverageScore(consultant.Id);

            consultant.Score = averageScore;
            return(consultant.HidePassword());
        }
Esempio n. 3
0
        public UpdateConsultantByIdCommandValidator(IConsultantRepository repository)
        {
            _repository = repository;
            const string idRegex = "^[a-f\\d]{24}$";

            RuleFor(consultant => consultant.Id)
            .Cascade(CascadeMode.Stop)
            .NotEmpty().WithMessage(ValidationErrors.EmptyId)
            .Length(24).WithMessage(ValidationErrors.InvalidIdLength)
            .Matches(idRegex).WithMessage(ValidationErrors.InvalidIdStructure)
            .MustAsync(async(id, cancellation) =>
            {
                var consultant = await _repository.GetByIdAsync(id);
                return(consultant != null);
            }).WithMessage(ValidationErrors.InexistentId);

            When(consultant => !string.IsNullOrEmpty(consultant.Username), () =>
            {
                RuleFor(consultant => consultant.Username)
                .Length(2, 25).WithMessage(ValidationErrors.ShortUsername);
            });

            When(consultant => !string.IsNullOrEmpty(consultant.Email), () =>
            {
                RuleFor(consultant => consultant.Email)
                .Cascade(CascadeMode.Stop)
                .EmailAddress().WithMessage(ValidationErrors.InvalidEmail)
                .MustAsync(async(email, cancellation) =>
                {
                    var exists = await _repository.CheckUniqueEmail(email);
                    return(!exists);
                }).WithMessage(ValidationErrors.NotUniqueEmail);
            });

            When(consultant => !string.IsNullOrEmpty(consultant.Password), () =>
            {
                RuleFor(consultant => consultant.Password)
                .Cascade(CascadeMode.Stop)
                .MinimumLength(4).WithMessage(ValidationErrors.ShortPassword)
                .Must(p => p.Any(char.IsDigit)).WithMessage(ValidationErrors.NoDigitPassword);
            });

            When(consultant => !string.IsNullOrEmpty(consultant.Location), () =>
            {
                RuleFor(consultant => consultant.Location)
                .Cascade(CascadeMode.Stop)
                .MinimumLength(2).WithMessage(ValidationErrors.ShortLocation);
            });

            When(consultant => !string.IsNullOrEmpty(consultant.Skill), () =>
            {
                RuleFor(consultant => consultant.Skill)
                .Cascade(CascadeMode.Stop)
                .IsEnumName(typeof(ConsultantSkills), caseSensitive: false).WithMessage(ValidationErrors.InvalidSkill);
            });

            When(consultant => !string.IsNullOrEmpty(consultant.Availability), () =>
            {
                RuleFor(consultant => consultant.Availability)
                .Cascade(CascadeMode.Stop)
                .IsEnumName(typeof(ConsultantAvailability), caseSensitive: false).WithMessage(ValidationErrors.InvalidAvailability);
            });

            When(consultant => consultant.NumberOfTickets != 0, () =>
            {
                RuleFor(consultant => consultant.NumberOfTickets)
                .GreaterThan(1).WithMessage(ValidationErrors.InvalidNumberOfTickets);
            });
        }