public ICommandResult Handle(CreateBoletoSubscriptionCommand command) { //Fail Fast Validation command.Validate(); if (command.Invalid) { AddNotifications(command); return(new CommandResult(false, "Não foi possível realizar sua assinatura")); } //Verificar se Documento já está cadastrado if (_repository.DocumentExists(command.PayerDocument)) { AddNotification("Document", "Este CPF já está em uso"); } //Verificar se E-mail já está cadastrados if (_repository.EmailExists(command.PayerEmail)) { AddNotification("Email", "Este E-mail já está em uso"); } //Gerar os values Objects (VOs) var name = new Name(command.FirstName, command.LastName); var document = new Document(command.PayerDocument, EDocumentType.CPF); var email = new Email(command.PayerEmail); var address = new Address(command.Stret, command.Number, command.Neighborhood, command.City, command.State, command.Country, command.zipCode); //Gerar as entidades var student = new Entities.Student(name, email, document); var subscription = new Entities.Subscription(DateTime.Now.AddMonths(1)); var payment = new Entities.BoletoPayment(command.BarCode, command.BoletoNumber, command.Number, command.PaidDate, command.ExpireDate, command.Total, command.TotalPaid, command.Payer, new Document(command.Payer, command.PayerDocumentType), address, email); // Adiciona os Relacionamentos subscription.AddPayment(payment); student.AddSubscription(subscription); // Agrupa as validações AddNotifications(name, document, address, student, subscription, payment); //Checar as infos if (Invalid) { new CommandResult(false, "Assinatura realizada com suesso"); } // Salvar as validações _repository.CreateSubscription(student); //Enviar E-mail de boas vindas _emailService.Send(student.Name.ToString(), student.Email.Address, "Bem vindo ao Beludo", "Sua assinatura foi criada"); //Retornar informações return(new CommandResult(true, "Assinatura realizada com sucesso")); }
public async Task HandleAsync(SubscribeCommand command) { var subscription = await _repository.GetOneAsync(a => a.UserId == command.UserId && a.BookId == command.BookId); if (subscription != null) { throw new ValidateException("You are already subscribed"); } subscription = new Entities.Subscription(command.UserId, command.BookId, command.BookName); await _repository.InsertAsync(subscription); await _uow.SaveAsync(); }