public void SignUpThousandCoursesInFiveSecondsTest()
        {
            var message = new SignUpIntegrationEvent("RabbitMq Overview", "*****@*****.**", new System.DateTime(1990, 12, 16), new System.Guid("57D92BF1-7AD6-4ACC-901C-8A54ACB39E76"));

            _messageBus.Setup(p => p.PublishAsync(message)).Returns(Task.FromResult(true));

            var cancellationToken   = new System.Threading.CancellationToken();
            var signUpCourseRequest = new SignUpCourseRequest()
            {
                Name        = "Torres",
                Email       = "*****@*****.**",
                DateOfBirth = new System.DateTime(1990, 12, 16),
                CourseId    = new System.Guid("57D92BF1-7AD6-4ACC-901C-8A54ACB39E76")
            };

            var handler = new API.Commands.Handlers.SignUpCourseHandler(_messageBus.Object);

            var lastResponse = new API.Commands.Responses.SignUpCourseResponse();

            for (int i = 0; i < 1000; i++)
            {
                lastResponse = handler.Handle(signUpCourseRequest, cancellationToken).Result;
            }

            Assert.IsTrue(lastResponse.ValidationResult.IsValid == true);
        }
        public void SubscribeCourseTest()
        {
            var message = new SignUpIntegrationEvent("RabbitMq Overview", "*****@*****.**", new System.DateTime(1990, 12, 16), new System.Guid("57D92BF1-7AD6-4ACC-901C-8A54ACB39E76"));

            _serviceProvider
            .Setup(x => x.GetService(typeof(ICourseRepository)))
            .Returns(_courseRepository);

            var serviceScope = new Moq.Mock <Microsoft.Extensions.DependencyInjection.IServiceScope>();

            serviceScope.Setup(x => x.ServiceProvider).Returns(_serviceProvider.Object);

            var serviceScopeFactory = new Moq.Mock <Microsoft.Extensions.DependencyInjection.IServiceScopeFactory>();

            serviceScopeFactory
            .Setup(x => x.CreateScope())
            .Returns(serviceScope.Object);

            _serviceProvider
            .Setup(x => x.GetService(typeof(Microsoft.Extensions.DependencyInjection.IServiceScopeFactory)))
            .Returns(serviceScopeFactory.Object);

            var handler  = new API.Handlers.CourseSignUpHandler(_serviceProvider.Object, _messageBus.Object);
            var response = handler.SignUpCourse(message);

            Assert.IsTrue(response.IsCompletedSuccessfully == true);
        }
Exemplo n.º 3
0
        public async Task <SignUpCourseResponse> Handle(SignUpCourseRequest request, CancellationToken cancellationToken)
        {
            var result = new SignUpCourseResponse();

            try
            {
                // Validation
                if (!request.IsValid())
                {
                    result.ValidationResult = request.ValidationResult;
                    return(await Task.FromResult(result));
                }

                //Send event to a service Bus
                var message = new SignUpIntegrationEvent(request.Name, request.Email, request.DateOfBirth, request.CourseId);
                await _bus.PublishAsync <SignUpIntegrationEvent>(message);
            }
            catch (Exception ex)
            {
                _log.Error($"", JsonConvert.SerializeObject(ex));
                result.ValidationResult.Errors.Add(new ValidationFailure(string.Empty, "An error has occurred please try again."));
            }

            return(await Task.FromResult(result));
        }
        private SignUpCourse MapSignUpCourse(SignUpIntegrationEvent request)
        {
            var mapped = new SignUpCourse();

            mapped.Id          = Guid.NewGuid();
            mapped.Name        = request.Name;
            mapped.Email       = request.Email;
            mapped.DateOfBirth = request.DateOfBirth;
            mapped.CourseId    = request.CourseId;

            return(mapped);
        }
        public async Task SignUpCourse(SignUpIntegrationEvent request)
        {
            try
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    var courseRepository = scope.ServiceProvider.GetRequiredService <ICourseRepository>();
                    var signUpRepository = scope.ServiceProvider.GetRequiredService <ISignUpRepository>();

                    //verify
                    bool isAvailable = await VerifyAvailableRoomAsync(courseRepository, signUpRepository, request.CourseId);

                    bool notAppliedYet = await VerifyStudentNotAppliedYet(signUpRepository, request.CourseId, request.Email);

                    // Map signUpCourse
                    var signUpCourse = MapSignUpCourse(request);

                    if (isAvailable && notAppliedYet)
                    {
                        //add course to context repository
                        signUpRepository.Create(signUpCourse);

                        //add sign up course to context repository
                        var validationResult = await PersistData(signUpRepository.UnitOfWork);

                        //send a confirmation email
                        SendEmail(signUpCourse, true);
                    }
                    else
                    {
                        //send a decline email if not applyed yet
                        if (notAppliedYet)
                        {
                            SendEmail(signUpCourse, false);
                        }
                    }

                    await Task.FromResult(true);
                }
            }
            catch (Exception ex)
            {
                _log.Error($"", JsonConvert.SerializeObject(ex));
                //NextStep
                //In erro, retry X times.
            }
        }