Exemple #1
0
        public async Task <ValidationResult> Handle(AddPhoneCandidateCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                return(message.ValidationResult);
            }

            var candidate = await GetCandidateById(message.CandidateId);

            if (candidate == null)
            {
                AddError("Candidato invalido. Verique o Id.");
                return(ValidationResult);
            }

            if (HasMaximumNumberPhonesByCandidate(candidate))
            {
                AddError("Numero máximo de telefones cadastrados");
                return(ValidationResult);
            }
            var phone = new Phone(message.PhoneId, message.CandidateId, message.PhoneNumber, message.IsDefault, message.PhoneType);

            if (candidate.HasThisPhoneNumberRegistered(phone))
            {
                AddError("Você ja cadastrou esse número.");
                return(ValidationResult);
            }
            if (await HasOtherCandidateThisPhone(phone))
            {
                AddError("Já existe uma pessoa com esse telefone cadastrado");
                return(ValidationResult);
            }

            candidate.AddPhone(phone);

            _candidateRepository.UpdatePhones(candidate.Phones.ToList());
            await _candidateRepository.AddPhone(phone);

            candidate.AddEvent(new AddPhoneCandidateEvent(phone.Id, phone.CandidateId, phone.PhoneNumber, phone.Default, phone.PhoneType));
            return(await SaveChanges(_candidateRepository.UnitOfWork));
        }