Example #1
0
        public async Task HandleAsync(RateStory command)
        {
            var user = await _userRepository.GetAsync(command.UserId);

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

            if (user.Locked)
            {
                throw new UserLockedException(command.UserId);
            }

            var story = await _storyRepository.GetAsync(command.StoryId);

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

            await _storyRatingRepository.SetAsync(new StoryRating(new StoryRatingId(command.StoryId, command.UserId),
                                                                  command.Rate));

            var totalRating = await _storyRatingRepository.GetTotalRatingAsync(story.Id);

            await _messageBroker.PublishAsync(new StoryRated(command.StoryId, command.UserId,
                                                             command.Rate, totalRating));
        }
Example #2
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);
        }