public SinglePost GetPost(int postId)
        //try to get a particular post, q or a
        //returns null if post not found
        //use SinglePost.Id for annotations
        //use SinglePost.QuestionId to get the thread the post belongs to
        {
            SinglePost returnPost = new SinglePost();

            var type = GetPostType(postId);

            if (type == "questions") //then its a question
            {
                var q = GetQuestion(postId);
                returnPost.Body       = q.Body;
                returnPost.Id         = postId;
                returnPost.QuestionId = q.Id;
                returnPost.Title      = q.Title;
                return(returnPost);
            }
            else if (type == "answers") //then its an answer
            {
                var a = GetAnswer(postId);
                returnPost.Body       = a.Body;
                returnPost.Id         = postId;
                returnPost.QuestionId = GetAnswer(postId).Parentid;               //get parent q of answer
                returnPost.Title      = GetQuestion(returnPost.QuestionId).Title; //get title of parent q
                return(returnPost);
            }
            else
            {
                return(null); //else its unknown!
            }
        }
Esempio n. 2
0
        public IList <Posts> Search(int userid, string searchstring, int?searchtypecode, PagingAttributes pagingAttributes)
        {
            ////// for performing searches with appsearch on the db
            ///
            // do actual search using appsearch in db and build results

            //need db context and searchtype lookuptable
            using var db = new DatabaseContext();
            Modules.SearchTypeLookupTable st = new Modules.SearchTypeLookupTable();

            ////get params for db.func
            ///
            //build searchstring
            var search = new NpgsqlParameter("search", NpgsqlTypes.NpgsqlDbType.Text)
            {
                Value = BuildSearchString(searchstring, false)
            };

            //lookup searchtype string
            var searchtype = new NpgsqlParameter("searchtype", NpgsqlTypes.NpgsqlDbType.Text);

            if (searchtypecode >= 0 && searchtypecode <= 3)
            {
                searchtype.Value = st.searchType[searchtypecode.Value];
            }
            else
            {
                searchtype.Value = st.searchType[3];
            }

            //userid
            var appuserid = new NpgsqlParameter("appuserid", NpgsqlTypes.NpgsqlDbType.Integer)
            {
                Value = userid
            };

            //if internal call is specified, stored function appsearch won't add to searches/searchhistory
            var internalcall = new NpgsqlParameter("internalcall", NpgsqlTypes.NpgsqlDbType.Boolean)
            {
                Value = true
            };

            //count all matches
            var matchcount = db.Search
                             .FromSqlRaw("select appsearch(@appuserid, @searchtype, @search, @internalcall)", appuserid, searchtype, search, internalcall)
                             .Count();

            System.Console.WriteLine($"{matchcount} results.");

            int page = _sharedService.GetPagination(matchcount, pagingAttributes);

            System.Console.WriteLine($"{page} page trying to get.");

            //get subset of results according to pagesize etc
            var resultlist = db.Search
                             .FromSqlRaw("SELECT * from appsearch(@appuserid, @searchtype, @search)", appuserid, searchtype, search)
                             .Skip(page * pagingAttributes.PageSize)
                             .Take(pagingAttributes.PageSize)
                             .ToList();

            //build and map results to posts
            var resultposts = new List <Posts>();

            foreach (Search s in resultlist)
            {
                Posts      p  = new Posts();
                SinglePost sp = new SinglePost();
                sp = _sharedService.GetPost(s.postid);

                p.Parentid = sp.QuestionId;
                p.Id       = sp.Id;
                var endpos = 100;
                if (sp.Body.Length < 100)
                {
                    endpos = sp.Body.Length;
                }
                p.Body = sp.Body.Substring(0, endpos);

                p.Title        = _sharedService.GetQuestion(p.Parentid).Title;
                p.Totalresults = matchcount;
                p.Rank         = s.rank;
                resultposts.Add(p);
            }
            return(resultposts);
        }