Exemplo n.º 1
0
        public async Task HandleAsync(SendStory command)
        {
            var user = await _userRepository.GetAsync(command.UserId);

            if (user is null)
            {
                throw new UserNotFoundException(command.UserId);
            }

            if (!_storyAuthorPolicy.CanCreate(user))
            {
                throw new CannotCreateStoryException(user.Id);
            }

            var author     = Author.Create(user);
            var text       = _storyTextFactory.Create(command.Text);
            var now        = _clock.Current();
            var visibility = command.VisibleFrom.HasValue && command.VisibleTo.HasValue
                ? new Visibility(command.VisibleFrom.Value, command.VisibleTo.Value, command.Highlighted)
                : Visibility.Default(now);
            var storyId = command.StoryId <= 0 ? _idGenerator.Generate() : command.StoryId;
            var story   = Story.Create(storyId, author, command.Title, text, command.Tags, now, visibility);
            await _storyRepository.AddAsync(story);

            var domainEvents = story.Events.ToArray();
            await _domainEventDispatcher.DispatchAsync(domainEvents);

            var integrationEvents = _eventMapper.Map(domainEvents).ToArray();

            _storyRequestStorage.SetStoryId(command.Id, story.Id);
            await _messageBroker.PublishAsync(integrationEvents);
        }
Exemplo n.º 2
0
        private async Task <List <IEvent> > HandleDomainEvents(IEnumerable <IDomainEvent> events)
        {
            var integrationEvents = new List <IEvent>();

            using var scope = _serviceScopeFactory.CreateScope();
            foreach (var @event in events)
            {
                var eventType = @event.GetType();
                _logger.LogTrace($"Handling domain event: {eventType.Name}");
                var     handlerType = typeof(IDomainEventHandler <>).MakeGenericType(eventType);
                dynamic handlers    = scope.ServiceProvider.GetServices(handlerType);
                foreach (var handler in handlers)
                {
                    await handler.HandleAsync((dynamic)@event);
                }

                var integrationEvent = _eventMapper.Map(@event);
                if (integrationEvent is null)
                {
                    continue;
                }

                integrationEvents.Add(integrationEvent);
            }

            return(integrationEvents);
        }
Exemplo n.º 3
0
        public async Task HandleAsync(RateStory command)
        {
            var story = await _storyRepository.GetAsync(command.StoryId);

            if (story is null)
            {
                throw new StoryNotFoundException(command.StoryId);
            }

            var user = await _userRepository.GetAsync(command.UserId);

            if (user is null)
            {
                throw new UserNotFoundException(command.UserId);
            }

            var rating = await _storyRatingService.RateAsync(story, user, command.Rate);

            await _storyRatingRepository.SetAsync(rating);

            var domainEvents = rating.Events.ToArray();
            await _domainEventDispatcher.DispatchAsync(domainEvents);

            var integrationEvents = _eventMapper.Map(domainEvents).ToArray();
            await _messageBroker.PublishAsync(integrationEvents);
        }
        public static void DoesNotFilter(IEventMapper <IKaVESet <IDEEvent> > uut, IDEEvent @event)
        {
            var processedEvent = uut.Map(@event);

            CollectionAssert.AreEqual(new KaVEHashSet <IDEEvent> {
                @event
            }, processedEvent);
        }
        public async Task <IEnumerable <EventResponse> > GetEventsByUserId(GetUserRequest request, string timing)
        {
            if (request == null)
            {
                throw new ArgumentNullException();
            }
            var user = await _userRepository.GetByEmailAsync(request.Email);

            var result = await _eventRepository.GetEventsByUserIdAsync(user.Id, timing);

            return(result.Select(x => _eventMapper.Map(x)));
        }
Exemplo n.º 6
0
    public override void OnWsReceived(byte[] buffer, long offset, long size)
    {
        var jsonMessage = string.Empty;

        try
        {
            jsonMessage = Encoding.UTF8.GetString(buffer, (int)offset, (int)size);
            _logger.LogDebug($"Received from id: '{Id}': {jsonMessage}");

            var eventMessage = _eventMapper.Map(jsonMessage);

            _mediator.Send(eventMessage).Wait();
        }
        catch (Exception ex)
        {
            _logger.LogError($"Received invalid message with content: {jsonMessage} and exception: {ex.Message}");
        }
    }
Exemplo n.º 7
0
        public async Task <IEnumerable <EventResponse> > GetEventsAsync()
        {
            var result = await _eventRepository.GetAsync();

            return(result.Select(x => _eventMapper.Map(x)));
        }