示例#1
0
        public ContentViewModel Create(ContentRequest request)
        {
            Authorized();

            var content = new Content();

            content.Title    = request.Title;
            content.Body     = request.Body;
            content.HtmlBody = Markdown.Encode(content.Body);
            content.UserId   = user.Id;
            content.Type     = request.Type;
            var id = ContentApi.Insert(content);

            TagManager.SetTagsForContent(id, request.Tags);

            if (request.ParentId != null)
            {
                ContentApi.Relate(request.ParentId.Value, id);
            }

            return(ContentApi.Select(id).AsViewModel()
                   .WithChildren()
                   .WithChildrenCount()
                   .WithTags());
        }
示例#2
0
        public ContentViewModel Create(ContentRequest request)
        {
            Authorized();

            var content = new Content();

            content.Title    = request.Title;
            content.Body     = request.Body;
            content.HtmlBody = Markdown.Encode(content.Body);
            content.UserId   = user.Id;
            content.Type     = request.Type;
            var id = ContentApi.Insert(content);

            TagManager.SetTagsForContent(id, request.Tags);

            if (request.ParentId != null)
            {
                ContentApi.Relate(request.ParentId.Value, id);
            }

            var item = ContentApi.Select(id).AsViewModel()
                       .WithChildren()
                       .WithChildrenCount()
                       .WithUser()
                       .WithTags();

            Searcher.Instance.Index(new Searchable()
            {
                Id = id, Type = item.Type, Title = item.Title, Body = item.Body, Username = item.User.DisplayName
            });
            ContentApi.MarkAsIndexed(id);
            return(item);
        }
示例#3
0
        public ContentViewModel Update(ContentRequest request)
        {
            Authorized();

            if (request.ContentId == null)
            {
                throw new ArgumentNullException(nameof(request.ContentId));
            }

            var userId = user?.Id;

            if (userId == null)
            {
                throw new ArgumentNullException(nameof(request), "User must be set to update an answer");
            }

            TagManager.SetTagsForContent((int)request.ContentId, request.Tags);

            var htmlBody = Markdown.Encode(request.Body);

            ContentApi.Update(request.ContentId.Value, request.Title, request.Body, htmlBody, (int)userId);

            return(ContentApi.Select(request.ContentId.Value)
                   .AsViewModel()
                   .WithAll());
        }
示例#4
0
        public ContentViewModel Update(ContentRequest request)
        {
            Authorized();

            if (request.ContentId == null)
            {
                throw new ArgumentNullException(nameof(request.ContentId));
            }

            var userId = user?.Id;

            if (userId == null)
            {
                throw new ArgumentNullException(nameof(request), "User must be set to update an answer");
            }

            TagManager.SetTagsForContent((int)request.ContentId, request.Tags);

            var htmlBody = Markdown.Encode(request.Body);

            ContentApi.Update(request.ContentId.Value, request.Title, request.Body, htmlBody, (int)userId);

            var item = ContentApi.Select(request.ContentId.Value)
                       .AsViewModel()
                       .WithAll();

            Searcher.Instance.Index(new Searchable()
            {
                Id = item.Id, Type = item.Type, Title = item.Title, Body = item.Body, Username = item.User.DisplayName
            });
            ContentApi.MarkAsIndexed(item.Id);
            return(item);
        }
        public ActionResult Search(int?p, string o, string q, int?t)
        {
            if (p == null)
            {
                p = 1;
            }

            if (q == null)
            {
                return(SearchOld(p.Value, o, q, t));
            }

            var results = Searcher.Instance.Search(q, 100, 0)
                          .Select(r => r.Id)
                          .Select(i => ContentApi.Select(i).AsViewModel().WithTags().WithUser())
                          .ToList();

            var searchRequest = new SearchRequest();

            searchRequest.Text    = q;
            searchRequest.Page    = p;
            searchRequest.OrderBy = o;
            searchRequest.Type    = "question";

            var resultPage = new SearchResultViewModel()
            {
                Results = results, Request = searchRequest
            };

            resultPage.ResultsCount = results.Count;
            resultPage.MaxPages     = Math.Min(5, (int)Math.Floor((double)resultPage.ResultsCount / 10));
            resultPage.Results      = results.Skip((p.Value - 1) * 10).Take(10).ToList();
            return(View("Search", resultPage));
        }
        public IActionResult Similar(int id)
        {
            var content = ContentApi.Select(id);
            var ids     = Searcher.Instance.Search(content.Title, 100, 0);
            var results = ids.Where(i => i.Id != id).Select(i => ContentApi.Select(i.Id)).Where(c => c.Type == "question").Select(c => c.AsViewModel().WithTags().WithUser()).Take(3).ToList();

            return(View(results));
        }
示例#7
0
        public IActionResult Show(int id, string response = null)
        {
            var content = ContentApi.Select(id);

            ViewData["Title"] = content.Title;
            return(View(new ShowContentModel()
            {
                Content = content, Response = response
            }));
        }
        public IActionResult Ask(int?id)
        {
            if (id == null)
            {
                return(View(new ContentRequest {
                    Type = "question", AvailableTags = TagApi.SelectSuggestions()
                }));
            }

            var c = ContentApi.Select(id.Value);

            ViewData["Title"] = c.Title;
            return(View(c.AsRequest()));
        }
示例#9
0
        public IActionResult Ask(int?id)
        {
            if (id == null)
            {
                return(View(new Content()
                {
                    UserId = 60, Type = "question"
                }));
            }

            var c = ContentApi.Select(id.Value);

            ViewData["Title"] = c.Title;
            return(View(c));
        }
示例#10
0
        public IActionResult Similar(int id)
        {
            var        content = ContentApi.Select(id);
            WebRequest request = WebRequest.Create($"http://localhost:63683/api/Values?search={content.Title}&max=50&minScore=0");

            request.Method = "GET";
            var response = request.GetResponseAsync().Result;
            IEnumerable <int> ids;

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var s = reader.ReadToEnd().Replace("[", "").Replace("]", "").Split(',');
                ids = s.Where(str => !string.IsNullOrEmpty(str)).Select(i => int.Parse(i)).ToList();
            }
            var results = ids.Where(i => i != id).Select(i => ContentApi.Select(i)).Where(c => c.Type == "question").Select(c => c.AsViewModel().WithTags().WithUser()).Take(3).ToList();

            return(View(results));
        }
示例#11
0
        public IActionResult Compare(int id)
        {
            var content    = ContentApi.Select(id);
            var oldContent = ContentApi.Select(id);
            var changeSets = ContentHistoryApi.SelectByContentId(id, DateTime.Now.AddDays(-1), DateTime.Now).OrderBy(c => c.Changed);

            foreach (var property in oldContent.GetType().GetProperties())
            {
                if (changeSets.Any(c => c.ChangedField == property.Name))
                {
                    var oldVal = changeSets.First(c => c.ChangedField == property.Name).OldValue;
                    property.SetValue(oldContent, oldVal);
                }
            }

            oldContent.HtmlBody = Site.Markdown.Encode(oldContent.Body);

            return(View(new Comparison {
                Old = oldContent, New = content, ChangeSets = changeSets.Select(cs => new ContentHistoryViewModel(cs)).ToList()
            }));
        }
示例#12
0
        public IActionResult Search(int p, string o, string q, int t)
        {
            if (q == null)
            {
                return(SearchOld(p, o, q, t));
            }

            WebRequest request = WebRequest.Create($"http://localhost:63683/api/Values?search={q}&max=100");

            request.Method = "GET";
            var response = request.GetResponseAsync().Result;
            IEnumerable <int> ids;

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                ids = reader.ReadToEnd()
                      .Replace("[", "").Replace("]", "")
                      .Split(',')
                      .Where(s => !string.IsNullOrEmpty(s))
                      .Select(id => int.Parse(id));
            }
            var results = ids.Select(i => ContentApi.Select(i).AsViewModel().WithTags().WithUser()).ToList();

            var searchRequest = new SearchRequest();

            searchRequest.Text    = q;
            searchRequest.Page    = p;
            searchRequest.OrderBy = o;
            searchRequest.Type    = "question";

            var resultPage = new SearchResultViewModel()
            {
                Results = results, Request = searchRequest
            };

            resultPage.ResultsCount = results.Count;
            resultPage.MaxPages     = Math.Min(5, (int)Math.Floor((double)resultPage.ResultsCount / 10));
            resultPage.Results      = results.Skip((p - 1) * 10).Take(10).ToList();
            return(View("Search", resultPage));
        }
示例#13
0
        public IActionResult Edit(int id)
        {
            var content = ContentApi.Select(id);

            return(View(content));
        }
示例#14
0
 public ContentViewModel Get(int contentId)
 {
     return(ContentApi.Select(contentId)
            .AsViewModel()
            .WithAll());
 }