示例#1
0
        public async Task Handle(UserCreatedDomainEvent notification, CancellationToken cancellationToken)
        {
            var integrationEvent = UserCreatedIntegrationEvent.FromUserCreatedDomainEvent(notification);

            _logger.LogCritical("Handled");
            _eventBus.Publish(integrationEvent);
        }
示例#2
0
        public async Task HandleAsync_WhenUserCreatedIntegrationEventIsValid_ShouldBeCompletedTask()
        {
            // Arrange
            var mockLogger = new MockLogger <UserCreatedIntegrationEventHandler>();

            TestMock.AccountService.Setup(accountRepository => accountRepository.AccountExistsAsync(It.IsAny <UserId>())).ReturnsAsync(false).Verifiable();

            TestMock.AccountService.Setup(accountRepository => accountRepository.CreateAccountAsync(It.IsAny <UserId>()))
            .ReturnsAsync(new DomainValidationResult <IAccount>())
            .Verifiable();

            var handler = new UserCreatedIntegrationEventHandler(TestMock.CashierAppSettingsOptions.Object, TestMock.AccountService.Object, mockLogger.Object);

            var integrationEvent = new UserCreatedIntegrationEvent
            {
                UserId = new UserId(),
                Email  = new EmailDto
                {
                    Address = "*****@*****.**"
                },
                Country = EnumCountryIsoCode.CA,
                Ip      = "10.10.10.10"
            };

            // Act
            await handler.HandleAsync(integrationEvent);

            // Assert
            TestMock.AccountService.Verify(accountRepository => accountRepository.AccountExistsAsync(It.IsAny <UserId>()), Times.Once);
            TestMock.AccountService.Verify(accountRepository => accountRepository.CreateAccountAsync(It.IsAny <UserId>()), Times.Once);
        }
示例#3
0
        public async Task <IActionResult> Register([FromBody] RegisterUserCommand command)
        {
            var userCreatedEvent = new UserCreatedIntegrationEvent(command.Name, command.Email, command.Password);

            await _eventBus.PublishAsync(userCreatedEvent);

            return(Ok(new { EventId = userCreatedEvent.Id }));
        }
示例#4
0
        private async Task HandleIntegrationEvent(object sender, BasicDeliverEventArgs args)
        {
            UserCreatedIntegrationEvent userCreatedIntegrationEvent = JsonConvert.DeserializeObject <UserCreatedIntegrationEvent>(Encoding.UTF8.GetString(args.Body.ToArray()));

            using IServiceScope scope = serviceProvider.CreateScope();

            await scope.ServiceProvider.GetRequiredService <IMediator>().Send(new CreateUserCommand(userCreatedIntegrationEvent.UserId));

            channel !.BasicAck(deliveryTag: args.DeliveryTag, multiple: false);
        }
示例#5
0
        public async Task Handle(UserCreatedEvent notification, CancellationToken cancellationToken)
        {
            var user     = notification.User;
            var password = notification.Password;

            var userCreatedIntegrationEvent = new UserCreatedIntegrationEvent(user.Email, password);
            var outboxMessageRepository     = _userDbContext.OutboxMessageRepository;
            await outboxMessageRepository.AddAsync(userCreatedIntegrationEvent, cancellationToken);

            await _userDbContext.SaveAsync(cancellationToken);
        }
示例#6
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName = model?.Email,
                    Email    = model?.Email,
                    Name     = model?.Name,
                    Surname  = model?.Surname
                };
                var result = await _userManager.CreateAsync(user, model?.Password).ConfigureAwait(false);

                if (result.Errors.Any())
                {
                    AddErrors(result);
                    // If we got this far, something failed, redisplay form
                    return(View(model));
                }

                //Inform Web.Api through service bus.
                var @event = new UserCreatedIntegrationEvent(user.Id, user.Email, user.Name, user.Surname, user.Email);
                try
                {
                    _eventBus.Publish(@event);
                }
                catch (Exception e)
                {
                    _logger.LogError(
                        "An error occured while publishing integration event: {IntegrationEventId} from {AppName}",
                        @event.Id, Program.AppName);
                    throw;
                }
            }

            if (returnUrl != null)
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    return(Redirect(returnUrl));
                }
                else
                if (ModelState.IsValid)
                {
                    return(RedirectToAction("login", "account", new { returnUrl }));
                }
                else
                {
                    return(View(model));
                }
            }
            return(RedirectToAction("index", "home"));
        }
        public static async Task PublishUserCreatedIntegrationEventAsync(this IServiceBusPublisher publisher, User user, string ip)
        {
            var integrationEvent = new UserCreatedIntegrationEvent
            {
                UserId = user.Id.ConvertTo <UserId>(),
                Email  = new EmailDto
                {
                    Address  = user.Email,
                    Verified = user.EmailConfirmed
                },
                Country = user.Country.ToEnum <EnumCountryIsoCode>(),
                Ip      = ip,
                Dob     = new DobDto
                {
                    Day   = user.Dob.Day,
                    Month = user.Dob.Month,
                    Year  = user.Dob.Year
                }
            };

            await publisher.PublishAsync(integrationEvent);
        }
        public async Task HandleAsync_WhenUserCreatedIntegrationEventIsValid_ShouldBeCompletedTask()
        {
            // Arrange
            var mockLogger = new MockLogger <UserCreatedIntegrationEventHandler>();

            TestMock.UserService.Setup(userService => userService.UserExistsAsync(It.IsAny <UserId>())).ReturnsAsync(false).Verifiable();

            TestMock.UserService.Setup(userService => userService.CreateUserAsync(It.IsAny <UserId>(), It.IsAny <string>()))
            .ReturnsAsync(new DomainValidationResult <User>())
            .Verifiable();

            TestMock.UserService.Setup(userService => userService.SendEmailAsync(It.IsAny <UserId>(), It.IsAny <string>(), It.IsAny <object>()))
            .Returns(Task.CompletedTask)
            .Verifiable();

            var handler = new UserCreatedIntegrationEventHandler(TestMock.UserService.Object, mockLogger.Object, TestMock.SendgridOptions.Object);

            var integrationEvent = new UserCreatedIntegrationEvent
            {
                UserId = new UserId(),
                Email  = new EmailDto
                {
                    Address  = "*****@*****.**",
                    Verified = true
                },
                Country = EnumCountryIsoCode.CA,
                Ip      = "10.10.10.10"
            };

            // Act
            await handler.HandleAsync(integrationEvent);

            // Assert
            TestMock.UserService.Verify(userService => userService.UserExistsAsync(It.IsAny <UserId>()), Times.Once);
            TestMock.UserService.Verify(userService => userService.CreateUserAsync(It.IsAny <UserId>(), It.IsAny <string>()), Times.Once);
            TestMock.UserService.Verify(userService => userService.SendEmailAsync(It.IsAny <UserId>(), It.IsAny <string>(), It.IsAny <object>()), Times.Once);
            mockLogger.Verify(Times.Never());
        }
示例#9
0
        public async Task <bool> Handle(CreateUserCommand message, CancellationToken cancellationToken)
        {
            //添加一个事件到一个 新篮子里面
            var userCreatedIntegrationEvent = new UserCreatedIntegrationEvent(message.UserId);
            //添加然后保存事件
            //这里应该就回去找他的事件处理程序
            await _userIntegrationEventService.AddAndSaveEventAsync(userCreatedIntegrationEvent);

            ///  添加 / 更新 用户聚合根
            //  DDD 模式注释:通过顺序聚合根添加子实体和值对象
            //   方法和构造函数,以便验证、不变量和业务逻辑
            ///  确保在整个聚合中保持一致性

            UserInformation userinfo = null;

            _logger.LogInformation("----- Creating Order - Order: {@Order}", userinfo);

            //添加
            _userRespository.Add(userinfo);

            return(await _userRespository.UnitOfWork
                   .SaveEntitiesAsync(cancellationToken));
        }
示例#10
0
 protected void When(UserCreatedIntegrationEvent @event)
 {
     Id = @event.AggregateRootId;
 }
示例#11
0
 public void Start(string username, string email)
 {
     Raise(UserCreatedIntegrationEvent.Create(this, username, email));
 }