예제 #1
0
        protected override async Task Handle(ProcessEventCommand<UserSubscribed> request, CancellationToken cancellationToken)
        {
            try
            {
                ReadModel.BeginTransaction();

                UserSubscribed subscription = request.Event;

                User user = new User(
                    subscription.AggregateId,
                    subscription.UserName,
                    subscription.FirstName,
                    subscription.LastName,
                    subscription.Email,
                    subscription.Date
                );

                await ReadModel.UserRepository.Save(user);

                ReadModel.Commit();
            }
            catch
            {
                ReadModel.Rollback();
            }
        }
        public void Handle_RollbacksTransaction_WhenErrorOccur()
        {
            //Arrange
            UserSubscribed @event = new UserSubscribed(
                Guid.NewGuid(),
                fixture.Create <string>(),
                fixture.Create <string>(),
                fixture.Create <string>(),
                fixture.Create <string>()
                );

            ProcessEventCommand <UserSubscribed> command = new ProcessEventCommand <UserSubscribed>(
                @event
                );

            CancellationToken token = tokenSource.Token;

            repository.Save(Arg.Any <User>()).Returns(u => Task.Run(() => throw new Exception()));

            //Act
            Task handling = eventProcessor.Handle(command, token);

            handling.Wait();

            //Assert
            unitOfWork.Received(1).BeginTransaction();
            unitOfWork.Received(1).Rollback();
        }
        public void Handle_SendsUserToReadModelRepository()
        {
            //Arrange
            UserSubscribed @event = new UserSubscribed(
                Guid.NewGuid(),
                fixture.Create <string>(),
                fixture.Create <string>(),
                fixture.Create <string>(),
                fixture.Create <string>()
                );

            ProcessEventCommand <UserSubscribed> command = new ProcessEventCommand <UserSubscribed>(
                @event
                );

            CancellationToken token = tokenSource.Token;

            //Act
            Task handling = eventProcessor.Handle(command, token);

            handling.Wait();

            //Assert
            unitOfWork.Received(1).BeginTransaction();
            unitOfWork.Received(1).Commit();

            repository.Received(1).Save(Arg.Is <User>(
                                            u => u.UserName == @event.UserName &&
                                            u.FirstName == @event.FirstName &&
                                            u.LastName == @event.LastName &&
                                            u.Email == @event.Email
                                            ));
        }
        public async Task Handle(JoinedCircleEvent message, IMessageHandlerContext context)
        {
            using (LogContext.PushProperty("IntegrationEventContext", $"{message.Id}-{Program.AppName}"))
            {
                _logger.LogInformation("----- Handling JoinedCircleEvent: {IntegrationEventId} at {AppName} - ({@IntegrationEvent})", message.Id, Program.AppName, message);

                var fromUser = await _userRepository.GetByIdAsync(message.JoinedUserId);

                #region 给圈主发通知
                var createEventCommand = new CreateEventCommand
                {
                    FromUserId  = message.JoinedUserId,
                    ToUserId    = message.CircleOwnerId,
                    CircleId    = message.CircleId,
                    CircleName  = message.CircleName,
                    EventType   = Domain.AggregatesModel.EventAggregate.EventType.JoinCircle,
                    PushMessage = $"{fromUser.Nickname}加入{message.CircleName}"
                };

                await _mediator.Send(createEventCommand);

                #endregion

                #region 给申请入圈用户发通知
                var joinCircleAcceptedCommand = new CreateEventCommand
                {
                    FromUserId  = message.CircleOwnerId,
                    ToUserId    = message.JoinedUserId,
                    CircleId    = message.CircleId,
                    CircleName  = message.CircleName,
                    EventType   = Domain.AggregatesModel.EventAggregate.EventType.ApplyJoinCircleAccepted,
                    PushMessage = $"申请通过,已加入{message.CircleName}"
                };

                await _mediator.Send(joinCircleAcceptedCommand);

                #endregion

                #region 将申请入圈事件标记为已处理
                var processEventCommand = new ProcessEventCommand
                {
                    FromUserId = message.JoinedUserId,
                    ToUserId   = message.CircleOwnerId,
                    EventType  = Domain.AggregatesModel.EventAggregate.EventType.ApplyJoinCircle
                };

                await _mediator.Send(processEventCommand);

                #endregion
            }
        }
예제 #5
0
 protected override async Task Handle(ProcessEventCommand <PasswordChanged> request, CancellationToken cancellationToken)
 {
     await Task.CompletedTask;
 }