示例#1
0
 public int GetTotalPublishedArticleCount()
 {
     using (var db = new KbVaultContext())
     {
         return(db.PublishedArticles().Count());
     }
 }
        public ActionResult Detail(long id)
        {
            try
            {
                using (var db = new KbVaultContext())
                {
                    var article          = db.PublishedArticles().FirstOrDefault(a => a.Id == id);
                    var author           = article.KbUser;
                    var lastAuthorEdited = db.KbUsers.FirstOrDefault(a => a.Id == article.LastAuthorEdited);

                    if (article != null)
                    {
                        article.Views++;
                        db.SaveChanges();
                        ViewBag.SimilarArticles  = ArticleRepository.GetVisibleSimilarArticles((int)article.Id, DateTime.Today.Date);
                        ViewBag.Author           = author.Name + " " + author.LastName;
                        ViewBag.LastAuthorEdited = lastAuthorEdited.Name + " " + lastAuthorEdited.LastName;
                        return(View(article));
                    }

                    return(View("ArticleNotFound"));
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return(RedirectToAction("Index", "Error"));
            }
        }
示例#3
0
 public ActionResult Detail(string id)
 {
     try
     {
         using (var db = new KbVaultContext())
         {
             Article article = db.PublishedArticles().FirstOrDefault(a => a.SefName == id);
             if (article != null)
             {
                 article.Views++;
                 db.SaveChanges();
                 ViewBag.SimilarArticles = ArticleRepository.GetVisibleSimilarArticles((int)article.Id, DateTime.Today.Date);
                 return(View(article));
             }
             else
             {
                 return(View("ArticleNotFound"));
             }
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex);
         return(RedirectToAction("PublicError", "Error"));
     }
 }
示例#4
0
 public List <Article> GetPopularArticles(int maxItemCount)
 {
     using (var db = new KbVaultContext())
     {
         return(db.PublishedArticles()
                .OrderByDescending(a => a.Likes)
                .Take(maxItemCount)
                .ToList());
     }
 }
示例#5
0
 public ActionResult Index()
 {
     using (var db = new KbVaultContext())
     {
         LandingPageViewModel model = new LandingPageViewModel();
         if (Settings.ShowTotalArticleCountOnFrontPage)
         {
             model.TotalArticleCountMessage = string.Format(UIResources.PublicTotalArticleCountMessage, db.PublishedArticles().Count());
         }
         model.HotCategories = db.Categories.Include("Articles").Where(c => c.IsHot).ToList();
         DateTime dateRangeToday = DateTime.Now.Date;
         ViewBag.Title = Settings.CompanyName;
         model.FirstLevelCategories = db.Categories.Include("Articles").Where(c => c.Parent == null).OrderBy(c => c.Name).ToList();
         model.LatestArticles       = db.PublishedArticles()
                                      .OrderByDescending(a => a.Edited)
                                      .Take(Settings.ArticleCountPerCategoryOnHomePage)
                                      .ToList();
         model.PopularArticles = db.PublishedArticles()
                                 .OrderByDescending(a => a.Likes)
                                 .Take(Settings.ArticleCountPerCategoryOnHomePage)
                                 .ToList();
         /* Build tag cloud */
         model.PopularTags = TagRepository.GetTopTags().OrderBy(c => Guid.NewGuid()).ToList();
         int ratioDiff = model.MaxTagRatio - model.MinTagRatio;
         int minRatio  = model.MinTagRatio;
         foreach (var item in model.PopularTags)
         {
             if (ratioDiff > 0)
             {
                 item.FontSize = 80 + Convert.ToInt32(Math.Truncate((double)(item.Ratio - minRatio) * (100 / ratioDiff)));
             }
             else
             {
                 item.FontSize = 80;
             }
         }
         return(View(model));
     }
 }
示例#6
0
        public ActionResult Do(SearchFormViewModel model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.SearchKeyword))
                {
                    return(RedirectToAction("Index", "Home"));
                }

                var articlePrefix = SettingsService.GetSettings().ArticlePrefix;
                if (!string.IsNullOrEmpty(articlePrefix))
                {
                    if (model.SearchKeyword.Length > articlePrefix.Length + 1 &&
                        model.SearchKeyword.Substring(0, articlePrefix.Length + 1) == articlePrefix + "-")
                    {
                        var articleId = model.SearchKeyword.Substring(articlePrefix.Length + 1);
                        model.ArticleId = Convert.ToInt32(articleId);
                    }

                    if (model.ArticleId > 0)
                    {
                        Article article = null;
                        using (var db = new KbVaultContext())
                        {
                            article = db.PublishedArticles().FirstOrDefault(a => a.Id == model.ArticleId);
                        }

                        if (article != null)
                        {
                            return(RedirectToRoute("Default", new { controller = "Home", action = "Detail", id = article.SefName }));
                        }
                    }
                }

                if (model.CurrentPage == 0)
                {
                    model.CurrentPage++;
                }

                model.Results = KbVaultLuceneHelper.DoSearch(model.SearchKeyword, model.CurrentPage, 10);

                return(View(model));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }
示例#7
0
 public ActionResult Categories(string id, int page = 1)
 {
     try
     {
         using (var db = new KbVaultContext())
         {
             Category cat = db.Categories.Include("ChildCategories").Include("ParentCategory").First(c => c.SefName == id);
             if (cat == null)
             {
                 return(View("CategoryNotFound"));
             }
             ViewBag.Category = cat;
             IList <Article> articles = db.PublishedArticles().Where(a => a.Category.SefName == id).OrderBy(a => a.Title).ToPagedList(page, ArticleCountPerPage);
             return(View(articles));
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex);
         return(RedirectToAction("PublicError", "Error"));
     }
 }
示例#8
0
 public ActionResult Tags(string id, int page = 1)
 {
     try
     {
         using (var db = new KbVaultContext())
         {
             Tag tag = db.Tags.First(c => c.Name == id);
             if (tag == null)
             {
                 return(View("TagNotFound"));
             }
             ViewBag.Tag = tag;
             IList <Article> articles = db.PublishedArticles().Where(a => a.ArticleTags.Any(t => t.Tag.Name == id)).OrderBy(a => a.Title).ToPagedList(page, ArticleCountPerPage);
             return(View(articles));
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex);
         return(RedirectToAction("PublicError", "Error"));
     }
 }