Example #1
0
        public Task <bool> Handle(RegisterCitizenCommand message, CancellationToken cancellationToken)
        {
            var citizen = new Citizen(message.Id, message.Name, message.NickName, message.Document, message.Email, message.Gender, message.DateRegister);

            if (!CitizenIsValid(citizen))
            {
                return(Task.FromResult(false));
            }

            var citizenRegistred = _citizenRepository.Find(c => c.Document == citizen.Document || c.Email == citizen.Email);

            if (citizenRegistred.Any())
            {
                _mediator.PublishEvent(new DomainNotification(message.MessageType, "CPF ou e-mail já utilizados"));
            }

            _citizenRepository.Add(citizen);

            if (Commit())
            {
                _mediator.PublishEvent(new CitizenRegisteredEvent(citizen.Id, citizen.Name, citizen.NickName, citizen.Document, citizen.Email, citizen.Gender, citizen.DateRegister));
            }

            return(Task.FromResult(true));
        }