示例#1
0
        public async Task <ValidationResult> Handle(AddJobTitleInterestedCandidateCommand messsage, CancellationToken cancellationToken)
        {
            if (!messsage.IsValid())
            {
                return(messsage.ValidationResult);
            }
            var candidate = await GetCandidate(messsage.CandidateId);

            if (candidate == null)
            {
                AddError(messsage.MessageType, "Candidato invalido.");
                return(ValidationResult);
            }
            if (candidate.JobTitleInteresteds.Count > _settings.AppConfig.MaximumNumberJobTitleInterestedByCandidate)
            {
                AddError(messsage.MessageType, "Número maximo de cargos de interesse cadastrado.");
                return(ValidationResult);
            }
            var jobTitle = await _candidateRepository.FindJobTitleById(messsage.JobTitleId);

            if (jobTitle == null)
            {
                AddError("O Cargo de interesse é inválido.");
                return(ValidationResult);
            }

            var jobTitleInterested = new JobTitleInterested(messsage.CandidateId, messsage.JobTitleId, messsage.IsDefault);

            if (candidate.HasThisJobTitleInterestedRegistered(jobTitleInterested))
            {
                AddError(messsage.MessageType, "Esse cargo de interesse já está cadastrado.");
                return(ValidationResult);
            }

            candidate.AddJobTitleInterested(jobTitleInterested);

            _candidateRepository.UpdateJobTitleInterested(candidate.JobTitleInteresteds.ToList());
            await _candidateRepository.AddJobTitleInterested(jobTitleInterested);

            return(await SaveChanges(_candidateRepository.UnitOfWork));
        }