Пример #1
0
        public ActionResult GetAllAnnotationsOfUser([FromQuery] PagingAttributes pagingAttributes)
        {
            (int userId, bool useridok) = GetAuthUserId();
            if (!useridok)
            {
                return(Unauthorized());
            }

            int count;
            var listOfAnnotations = _annotationService.GetAllAnnotationsOfUser(userId, pagingAttributes, out count);

            if (count == 0)
            {
                return(NotFound());
            }
            foreach (PostAnnotationsDto item in listOfAnnotations)
            {
                var postDataForAnnot = _sharedService.GetPost(item.PostId);
                item.PostId     = postDataForAnnot.Id;
                item.QuestionId = postDataForAnnot.QuestionId;
                item.Title      = postDataForAnnot.Title;
                item.PostBody   = postDataForAnnot.Body;
                item.PostUrl    = SetPostUrl(postDataForAnnot.Id, postDataForAnnot.QuestionId);
            }

            return(Ok(CreateResult(listOfAnnotations, pagingAttributes, count)));
        }
        private HistoryDTO CreateHistoryResultDto(History hist)
        {
            var post = _sharedService.GetPost(hist.Postid);
            var dto  = new HistoryDTO
            {
                Title     = post.Title,
                Body      = post.Body,
                Date      = hist.Date,
                ThreadUrl = Url.Link(
                    nameof(QuestionsController.GetThread),
                    new { questionId = hist.Postid }
                    )
            };

            return(dto);
        }
Пример #3
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);
        }
        //example http://localhost:5001/api/questions/thread/19
        //get the whole thread of question+asnswers
        public ActionResult GetThread(int questionId, int?postId)
        {
            (int userId, bool useridok) = GetAuthUserId();

            var checkthatpost = _sharedService.GetPostType(questionId);

            if (checkthatpost == "answers")
            {
                questionId = _sharedService.GetPost(questionId).QuestionId;
                if (postId != null)
                {
                    postId = questionId;
                }
            }
            else if (checkthatpost == null)
            {
                return(NotFound());
            }

            var t = _sharedService.GetThread(questionId);

            if (t != null && useridok) // then we got a thread!
            {
                ///call to add browse history here
                History browsehist = new History
                {
                    Userid = userId
                };
                if (postId != null)
                {
                    browsehist.Postid = (int)postId;
                }
                else
                {
                    browsehist.Postid = questionId;
                }
                _historyService.Add(browsehist);

                //createthreaddto
                List <PostsThreadDto> thread = new List <PostsThreadDto>();
                foreach (Posts p in t)
                {
                    PostsThreadDto pt = new PostsThreadDto
                    {
                        Id       = p.Id,
                        Parentid = p.Parentid,
                        Title    = p.Title,
                        Body     = p.Body
                    };
                    PagingAttributes           pagingAttributes = new PagingAttributes();
                    List <SimpleAnnotationDto> tempanno         = new List <SimpleAnnotationDto>();
                    tempanno              = _annotationService.GetUserAnnotationsMadeOnAPost(userId, p.Id, pagingAttributes);
                    pt.Annotations        = tempanno;
                    pt.createBookmarkLink = Url.Link(nameof(BookmarkController.AddBookmark), new { postId = p.Id });
                    AnnotationsDto anno = new AnnotationsDto
                    {
                        Body   = "form_or_similar_would_be_here_to_POST_a_new_annotation",
                        PostId = p.Id
                    };
                    pt.createAnnotationLink = Url.Link(nameof(AnnotationsController.AddAnnotation), anno);
                    // i know its supposed to be a form/post. just thought it'd be neat to have a link mockup.
                    thread.Add(pt);
                }
                return(Ok(thread));
            }
            else
            {
                return(NotFound());
            }
        }