Example #1
0
 public SearchController(SearchCommand searchCommand,
                         SearchQueryCommand searchQueryCommand,
                         ISession session)
 {
     _searchCommand = searchCommand;
     _searchQueryCommand = new SearchQueryCommand(session);
 }
        public async Task <ActionResult <ICollection <PostDTO> > > GetQuestionsByKeyword([FromQuery] SearchQueryCommand query)
        {
            try
            {
                ICollection <Post> questions;
                if (query.Keywords != null)
                {
                    questions = await _postRepository.FindBy(new SearchByKeyword(query.Keywords, search => search
                                                                                 .Include(p => p.Replies)
                                                                                 .ThenInclude(p => p.Author)
                                                                                 .ThenInclude(p => p.Comments)
                                                                                 .ThenInclude(c => c.Author)
                                                                                 .ThenInclude(p => p.Votes)
                                                                                 .Include(p => p.Author)
                                                                                 .Include(p => p.Comments)
                                                                                 .ThenInclude(c => c.Author)
                                                                                 .Include(p => p.PostTags)
                                                                                 .ThenInclude(pt => pt.Tag)
                                                                                 .Include(p => p.Votes)
                                                                                 ));
                }
                else
                {
                    questions = (await this.GetQuestionList()).ToList();
                }


                if (questions == null)
                {
                    return(NotFound());
                }
                else
                {
                    ICollection <PostDTO> questDTO = questions.ToDTO();
                    questDTO = OrderedQuestions(questDTO, query.Filter);
                    return(Ok(questDTO));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));

                throw;
            }
        }