public ICommandResult Handle(CreatePaypalSubscriptionCommand command) { // Fail Fast Validations command.Validate(); AddNotifications(command); if (command.Invalid) { return(_failCommandResult); } // Verificar se o documento já está cadastrado if (_studentRepository.DocumentExists(command.Document)) { AddNotification("Document", "Este documento já está em uso."); return(_failCommandResult); } // Verificar se o E-mail já está cadastrado if (_studentRepository.DocumentExists(command.EmailAddress)) { AddNotification("EmailAddress", "Este E-mail já está em uso."); return(_failCommandResult); } // Gerar VOs var name = new Name(command.FirstName, command.LastName); var email = new Email(command.EmailAddress); var document = new Document(command.Document, EDocumentType.CPF); var address = new Address( street: command.Street, number: command.AddressNumber, neighborhood: command.Neighborhood, city: command.City, state: command.State, country: command.Country, zipCode: command.ZipCode ); var payerDocument = new Document( command.PayerDocument, command.PayerDocumentType ); // Gerar as entidades var student = new Student(name, document, email); var subscription = new Subscription(DateTime.Now.AddMonths(1)); var payment = new PayPalPayment( email: email, transactionCode: command.TransactionCode, paidDate: command.PaidDate, expireDate: command.ExpireDate, total: command.Total, totalPaid: command.TotalPaid, payer: command.Payer, document: document, address: address ); // Relacionamentos subscription.AddPayment(payment); student.AddSubscription(subscription); // Agrupar as validações AddNotifications( name, document, email, address, student, subscription, payment ); // Verificar antes de salvar if (this.Invalid) { return(_failCommandResult); } // Salvar as informações _studentRepository.CreateSubscription(student); // Enviar E-mail de boas vindas _emailService.Send( to: student.Name.ToString(), email: student.Email.Address, subject: "Bem vindo ao balta.io.", body: "Sua assinatura foi realizada." ); // Retornar informações return(new CommandResult(true, "Assinatura realizada com sucesso.")); }
public ICommandResult Handle(CreatePaypalSubscriptionCommand command) { //Fail Fast Validations. command.Validate(); if (command.Invalid) { AddNotifications(command); return(new CommandResult(false, "Não foi possível realizar a assinatura, por favor verifique os dados")); } // Verificar se documento já está cadastrado. if (_repository.DocumentExits(command.Document)) { AddNotification("Document", "Este CPF já está em uso"); } //Verificar se e-mail já está cadastrado. if (_repository.EmailExits(command.Email)) { AddNotification("Email", "Este E-mail já está em uso"); } // Gerar os VO's var name = new Name(command.FirstName, command.LastName); var document = new Document(command.Document, EDocumentType.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); //Gerar as entidades. var student = new Student(name, document, email); var subscription = new Subscription(DateTime.Now.AddMonths(1)); var payment = new PaypalPayment( command.TransactionCode, command.PaidDate, command.ExpireDate, command.Total, command.TotalPaid, address, document, command.Payer, email); //Relacionamentos subscription.AddPayment(payment); student.AddSubscription(subscription); //Agrupar validações. AddNotifications(name, document, email, address, student, subscription, payment); //Checar as notificações if (Invalid) { return(new CommandResult(false, "Não foi possível realizar a assinatura")); } //Salvar as informações. _repository.CreateSubscription(student); //Enviar e-mail. _emailService.Send(student.Name.ToString(), student.Email.Address, "Bem vindo ao Balta.io", "Sua assinatura foi criada"); //Retornas informações. return(new CommandResult(true, "Assinatura realizada com sucesso")); }