Esempio 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);
        }
        public async Task HandleAsync(SendStory 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 author    = Author.Create(user);
            var storyText = new StoryText(command.Text);

            _storyTextPolicy.Verify(storyText);
            var now        = _dateTimeProvider.Get();
            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 == default ? _idGenerator.Generate() : command.StoryId;
            var story   = new Story(storyId, author, command.Title, storyText, command.Tags, now, visibility);
            await _storyRepository.AddAsync(story);

            _storyRequestStorage.SetStoryId(command.Id, story.Id);
            await _messageBroker.PublishAsync(new StorySent(story.Id,
                                                            new StorySent.AuthorModel(author.Id, author.Name), story.Title, story.Tags, story.CreatedAt,
                                                            new StorySent.VisibilityModel(story.Visibility.From, story.Visibility.To,
                                                                                          story.Visibility.Highlighted)));
        }