public ViewThreadViewModel Thread(ViewThreadRequest request)
        {
            _taskQueue.ExecuteLater(new RecordThreadViewTask(request));

            var thread = _session.Load<DiscussionThread>(DiscussionThread.Key(request.UriId));
            var replies = _session.Query<Post>().Where(x => x.ThreadId == thread.Id).ToList();

            var userKeys = new List<string> { UserAccount.Key(thread.AuthorUsername) };
            userKeys.AddRange(replies.Select(x => UserAccount.Key(x.Username)).Distinct().ToList());
            var users = _session.Load<UserAccount>(userKeys).ToList();

            var viewModel = _mapper.Map<DiscussionThread, ViewThreadViewModel>(thread);
            foreach (var reply in replies)
            {
                var user = users.First(x => x.Username == reply.Username);
                var replyModel = new ViewThreadReplyViewModel
                                     {
                                         DisplayBody = reply.DisplayText,
                                         Username = reply.Username,
                                         UserProfilePictureUrl = user.ProfilePictureUrl
                                     };
                viewModel.Replies.Add(replyModel);
            }

            return viewModel;
        }
            public void CreatesNewThreadAndSavesIt()
            {
                var endpoint = TestableNewThreadEndpoint.Build();
                var command = new NewThreadCommand { Tags = "test,tags", TagInput = "hogs baseball", Author = new UserAccount() };
                var request = new ViewThreadRequest();

                endpoint.Mapper
                    .Setup(x => x.Map<DiscussionThread, ViewThreadRequest>(It.IsAny<DiscussionThread>()))
                    .Returns(request);

                var continuation = endpoint.NewThread(command);

                endpoint.Session.Verify(x => x.Store(It.IsAny<DiscussionThread>()));
                continuation.AssertWasRedirectedTo<ViewThreadRequest>();
            }
        public void RecordsThreadViewIfNotViewedFromUserAndIPBefore()
        {
            var thread = new DiscussionThread("title", "body", new[] {"tags"}, DefaultUser);
            thread.ViewCount.ShouldEqual(0);
            Session.Store(thread);
            Session.SaveChanges();

            var request = new ViewThreadRequest
                              {
                                  IPAddress = "myIP",
                                  UriId = thread.UriId,
                                  UserAccount = DefaultUser
                              };
            var task = new RecordThreadViewTask(request);
            task.Initialize(Session, DocumentStore);
            task.Execute();
            Session.SaveChanges();

            var views = Session.Query<ThreadView>().Where(x => x.IPAddress == request.IPAddress && x.Username == DefaultUser.Username && x.DiscussionThreadId == thread.Id).ToList();
            views.Count().ShouldEqual(1);
            Session.Load<DiscussionThread>(thread.Id).ViewCount.ShouldEqual(1);
        }
 public RecordThreadViewTask(ViewThreadRequest request)
 {
     _request = request;
 }