Ejemplo n.º 1
0
        public async Task <List <CommentViewModel> > GetAllCommentsAsync(int id)
        {
            //Get Comments
            List <Comment> comments = await _context.Comment.Where(c => c.ArticleID == id).ToListAsync();

            List <CommentViewModel> cvms = new List <CommentViewModel>();

            //Build up structure of comments
            foreach (var c in comments)
            {
                if (c.ParentID == null)
                {
                    var cvm = new CommentViewModel
                    {
                        CommentID = c.CommentID,
                        Content   = LF_MarkdownParser.ParseWithoutStyle(c.Content),
                        Author    = await _userController.GetNickNameAsync(c.Author),
                        AddTime   = c.AddTime.ToLocalTime(),
                        Childs    = await GetChildCommentsAsync(comments, c.CommentID),
                        Avatar    = await _userController.GetAvatarAsync(c.Author)
                    };
                    cvms.Add(cvm);
                }
            }

            return(cvms);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Archives()
        {
            List <Article> articles = new List <Article>();

            try
            {
                articles = await _context.Article
                           .Where(a => !a.Title.Contains("「LONEFIRE」") && a.Status == ArticleStatus.Approved)
                           .OrderByDescending(a => a.AddTime).ToListAsync();
            }
            catch (Exception)
            {
                _toaster.ToastError("读取文章列表失败");
            }
            foreach (var a in articles)
            {
                a.Author = await _userController.GetNickNameAsync(a.Author);

                if (a.Content != null)
                {
                    a.Content = LF_MarkdownParser.ParseAsPlainText(a.Content);
                    a.Content = a.Content.Substring(0, Math.Min(a.Content.Length, 100));
                }
            }
            return(View(articles));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Index(int page = 1)
        {
            PaginatedList <Article> articles = new PaginatedList <Article>();

            try
            {
                IQueryable <Article> articleIQ = _context.Article
                                                 .Where(a => !a.Title.Contains("「LONEFIRE」") && a.Status == ArticleStatus.Approved)
                                                 .OrderByDescending(a => a.AddTime);

                articles = await PaginatedList <Article> .CreateAsync(articleIQ.AsNoTracking(), page, 6);
            }
            catch (Exception)
            {
                _toaster.ToastError("读取文章列表失败");
            }
            foreach (var a in articles)
            {
                a.Author = await _userController.GetNickNameAsync(a.Author);

                if (a.Content != null)
                {
                    a.Content = LF_MarkdownParser.ParseAsPlainText(a.Content);
                    a.Content = a.Content.Substring(0, Math.Min(a.Content.Length, 100));
                }
            }
            ViewData["AboutMe"] = await _context.Article.FirstOrDefaultAsync(m => m.Title == "「LONEFIRE」首页关于");

            ViewData["Friends"] = await _context.Article.FirstOrDefaultAsync(m => m.Title == "「LONEFIRE」首页友链");

            ViewData["Tags"] = await _context.Tag.OrderByDescending(t => t.TagCount).Take(6).ToListAsync();

            return(View(articles));
        }
Ejemplo n.º 4
0
        //Recursive child comment fetcher
        public async Task <List <CommentViewModel> > GetChildCommentsAsync(List <Comment> comments, int cid)
        {
            List <Comment>          child_comments = comments.Where(c => c.ParentID == cid).ToList();
            List <CommentViewModel> cvms           = new List <CommentViewModel>();

            foreach (var c in child_comments)
            {
                var cvm = new CommentViewModel
                {
                    CommentID = c.CommentID,
                    Content   = LF_MarkdownParser.ParseWithoutStyle(c.Content),
                    Author    = await _userController.GetNickNameAsync(c.Author),
                    AddTime   = c.AddTime.ToLocalTime(),
                    Childs    = await GetChildCommentsAsync(comments, c.CommentID),
                    Avatar    = await _userController.GetAvatarAsync(c.Author)
                };
                cvms.Add(cvm);
            }

            return(cvms);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> ArticleView(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var article = await _context.Article
                          .SingleOrDefaultAsync(m => m.ArticleID == id);

            if (article == null)
            {
                return(NotFound());
            }

            //Get Comments
            ViewData["Comments"] = await _commentController.GetAllCommentsAsync(article.ArticleID);

            article.ViewCount++;
            await _context.SaveChangesAsync();

            article.Content = LF_MarkdownParser.Parse(article.Content, ImageUploadPath + article.Title + '/');

            article.Author = await _userController.GetNickNameAsync(article.Author);

            ViewData["HeaderImg"] = ImageUploadPath + article.Title + '/' + article.HeaderImg;

            try
            {
                var articles = await _context.Article
                               .Where(a => !a.Title.Contains("「LONEFIRE」") && a.Status == ArticleStatus.Approved)
                               .OrderByDescending(a => a.AddTime)
                               .Select(a => new { a.ArticleID, a.Title, a.Author, a.Tag, a.AddTime, a.Status })
                               .ToListAsync();

                int idx = articles.FindIndex(a => a.ArticleID == id);

                if (articles.Count > 1)
                {
                    if (idx == 0)
                    {
                        ViewData["Next"] = articles[idx + 1];
                    }
                    else if (idx == articles.Count - 1)
                    {
                        ViewData["Prev"] = articles[idx - 1];
                    }
                    else
                    {
                        ViewData["Prev"] = articles[idx - 1];
                        ViewData["Next"] = articles[idx + 1];
                    }
                }
            }
            catch (Exception)
            {
                _toaster.ToastError("读取文章列表失败");
            }

            ViewData["Related"] = await GetRelatedArticles(article);

            return(View(article));
        }