public ICommandResult Handler(CreateBoletoSubscriptionCommand command)
        {
            //Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Não foi possível realizar sua assinatura"));
            }

            //Verifica se o documento já esta cadastrado!
            if (studentyRepository.DocumentExists(command.Document))
            {
                AddNotification("Document", "Este CPF já está em uso");
            }

            if (studentyRepository.EmailExists(command.Email))
            {
                AddNotification("Email", "Este email já está em uso");
            }

            //Entities e V.O.
            var name         = new Name(command.FirstName, command.LastName);
            var email        = new Email(command.Email);
            var address      = new Address(command.Street, command.Number, command.Neighborhood, command.City, command.State, command.Contry, command.ZipCode);
            var document     = new Document(command.Document, EDocumentType.CPF);
            var student      = new Student(name, document, email);
            var subscription = new Subscription(DateTime.Now.AddMonths(1));

            var payment = new BoletoPayment(command.BarCode,
                                            command.BoletoNumber,
                                            command.PaidDate,
                                            command.ExpireDate,
                                            command.Total,
                                            command.TotalPaid,
                                            command.Payer, new Document(command.PayerDocument, command.PayerDocumentType),
                                            address, email);

            //Relacionamentos
            subscription.AddPayment(payment);
            student.AddSubscription(subscription);

            //Agrupa as validações
            AddNotifications(name, document, address, student, subscription, payment);

            //Valida
            if (Invalid)
            {
                return(new CommandResult(false, "Não foi possível realizar sua assinatura"));
            }

            //Salva as informacoes
            studentyRepository.CreateSubscription(student);

            //Envia e-mail de boas vindas
            emailService.Send(student.Name.ToString(), student.Email.Address, "Bem vindo", "Sua assinatura foi criada!");

            //retorna as informações
            return(new CommandResult(true, "Assinatura realizada com sucesso"));
        }
Ejemplo n.º 2
0
        public ICommandResult Handle(CreateBoletoSubscriptionCommand command)
        {
            command.Validate();

            if (command.Invalid)
            {
                AddNotifications(command);
                return(new CommandResult(false, "Não foi possivel realizar sua assinatura."));
            }

            if (_repository.DocumentExists(command.Document))
            {
                AddNotification("Document", "Este CPF já está em uso.");
            }

            if (_repository.EmailExists(command.Email))
            {
                AddNotification("Document", "Este Email já está em uso.");
            }

            var name     = new Name(command.FirstName, command.LastName);
            var document = new Document(command.Document, EnumDocumentType.CPF);
            var email    = new Email(command.Email);
            var address  = new Address(command.Street, command.Number, command.Neighborhood, command.City, command.State, command.Country, command.ZipCode);

            var student      = new Student(name, document, email);
            var subscription = new Subscription(DateTime.Now.AddMonths(1));
            var payment      = new BoletoPayment(command.BarCode, command.BoletoNumber, command.PaidDate, command.ExpireDate, command.Total, command.PaidTotal, command.Payer, new Document(command.PayerDocument, command.PayerDocumentType), address, email);

            subscription.AddPayment(payment);
            student.AddSubscription(subscription);

            AddNotifications(name, document, email, address, student, subscription, payment);

            _repository.CreateSubscription(student);

            _emailService.Send(student.Name.ToString(), student.Email.Address, "Bem vindo na plataforma de cursos.", "Sua assinatura foi criada.");

            return(new CommandResult(true, "Assinatura realizada com sucesso."));
        }