Ejemplo n.º 1
0
        public NewPostAndSuggestions GetSuggestionsAfterAccept(string threadId, ThreadPost acceptedPost, string askingUser)
        {
            if (!hasAccessToThread(threadId, askingUser))
            {
                throw new Exception("You have no access to this thread!");
            }


            var thread = threadRepository.Find(threadId);
            var dbId   = ObjectId.GenerateNewId().ToString();

            thread.ThreadPosts.Add(dbId);
            threadRepository.Update(thread);

            var storedThreadPosts        = postsRepository.Querry(p => thread.ThreadPosts.Contains(p.Id)).ToList();
            var acceptedStoredThreadPost = new StoredThreadPost
            {
                Id             = dbId,
                ThreadId       = threadId,
                PostId         = acceptedPost.Id,
                ConnectedPosts = acceptedPost.ConnectedPosts,
                ThreadIndex    = storedThreadPosts.Count() > 0 ? storedThreadPosts.MaxBy(sTP => sTP.ThreadIndex).First().ThreadIndex + 1 : 0
            };

            storedThreadPosts.Add(acceptedStoredThreadPost);
            postsRepository.Create(acceptedStoredThreadPost);

            var recommendedQuestions = elasticSuggestionHelper.GetRecommendedQuestions(storedThreadPosts.OrderByDescending(sTP => sTP.ThreadIndex)
                                                                                       .Select(sTP => sTP.PostId)
                                                                                       .ToList(),
                                                                                       thread.LastSearched,
                                                                                       thread.TagList.ToList());
            var threadQuestions = questionsElasticRepository.GetAllByIds(storedThreadPosts.Select(sTP => sTP.PostId)
                                                                         .Concat(recommendedQuestions.Select(recQ => recQ.Id))
                                                                         .ToList());

            return(new NewPostAndSuggestions
            {
                NewPost = new ThreadPost
                {
                    Id = acceptedStoredThreadPost.Id,
                    ThreadIndex = acceptedStoredThreadPost.ThreadIndex,
                    Title = acceptedPost.Title,
                    Body = acceptedPost.Body,
                    ConnectedPosts = acceptedStoredThreadPost.ConnectedPosts
                },
                Suggestions = elasticSuggestionHelper.ParseQuestionsToThreadPosts(recommendedQuestions, storedThreadPosts.ToList())
            });
        }
Ejemplo n.º 2
0
        public SingleThread GetSingleThread(string id, string askingUser)
        {
            if (!hasAccessToThread(id, askingUser))
            {
                throw new Exception("You have no access to this thread!");
            }

            var thread               = threadRepository.Find(id);
            var storedThreadPosts    = postsRepository.Querry(p => thread.ThreadPosts.Contains(p.Id));
            var recommendedQuestions = elasticSuggestionHelper.GetRecommendedQuestions(storedThreadPosts.OrderByDescending(sTP => sTP.ThreadIndex)
                                                                                       .Select(sTP => sTP.PostId)
                                                                                       .ToList(),
                                                                                       thread.LastSearched,
                                                                                       thread.TagList.ToList());
            var threadQuestions = questionsElasticRepository.GetAllByIds(storedThreadPosts.Select(sTP => sTP.PostId)
                                                                         .Concat(recommendedQuestions.Select(recQ => recQ.Id))
                                                                         .ToList());


            if (threadQuestions.Count() != storedThreadPosts.Count() + recommendedQuestions.Count || threadQuestions.Count() == 0)
            {
                throw new Exception("Cannot get the relevant posts!");
            }

            return(new SingleThread
            {
                Thread = thread,
                Posts = storedThreadPosts.Select(sTP =>
                {
                    var bqPost = threadQuestions.Where(tQ => sTP.PostId == tQ.Id).First();
                    return new ThreadPost
                    {
                        Id = sTP.Id,
                        Title = bqPost.Title,
                        Body = bqPost.Body,
                        ThreadIndex = sTP.ThreadIndex,
                        ConnectedPosts = sTP.ConnectedPosts
                    };
                }).OrderBy(post => post.ThreadIndex).ToList(),
                Suggestions = elasticSuggestionHelper.ParseQuestionsToThreadPosts(recommendedQuestions, storedThreadPosts.ToList())
            });
        }