public IActionResult Search(string searchText) { var setting = settingRepository.GetById(1); int TakeBlogCount = setting.TakeBlogCount; ViewBag.Title = "'" + searchText + "' | " + setting.SiteName + " | " + setting.SiteTitle; ViewBag.Description = setting.Description; var blogs = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => p.PublishedDate).ToList(); List <Blog> blogSearch = new List <Blog>(); foreach (var item in blogs) { if (item.Title.ToLower().Contains(searchText.ToLower()) || item.Description.ToLower().Contains(searchText.ToLower()) || item.Explanation.ToLower().Contains(searchText.ToLower())) { blogSearch.Add(item); } } var blogShow = BlogShow.GetShowItems(blogSearch, commentRepository, categoryRepository); ViewBag.SearchText = searchText; return(View(blogShow.ToList())); }
public IActionResult Index() { var setting = settingRepository.GetById(1); int TakeBlogCount = setting.TakeBlogCount; ViewBag.Title = setting.SiteName + " | " + setting.SiteTitle; ViewBag.Description = setting.Description; var blogs = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => p.PublishedDate).Take(TakeBlogCount).ToList(); var blogShow = BlogShow.GetShowItems(blogs, commentRepository, categoryRepository); return(View(blogShow)); }
public override void OnActionExecuting(ActionExecutingContext context) { base.OnActionExecuting(context); ViewBag.Categories = categoryRepository.GetAll().Where(p => p.IsActive == true).ToList(); // Side Content var setting = settingRepository.GetById(1); ViewBag.Settings = setting; var BlogsMostRead = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => Convert.ToInt32(p.VisitorHit)).Take(setting.TakeSideMostReadCount).ToList(); ViewBag.MostReadBlogs = BlogShow.GetShowItems(BlogsMostRead, commentRepository, categoryRepository); var BlogsMostComment = blogRepository.GetAll().Where(p => p.IsActive == true).ToList(); ViewBag.MostCommentBlogs = BlogShow.GetShowItems(BlogsMostComment, commentRepository, categoryRepository).OrderByDescending(p => p.CommentCount).Take(setting.TakeSideMostCommentCount); var BlogsLastPublished = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => p.PublishedDate).Take(setting.TakeSideLastPublishedCount).ToList(); ViewBag.LastPublishedBlogs = BlogShow.GetShowItems(BlogsLastPublished, commentRepository, categoryRepository); int BlogsCount = blogRepository.GetAll().Where(p => p.IsActive == true).Count(); Random r = new Random(); List <Blog> blogs = new List <Blog>(); List <int> randomList = new List <int>(); int RandomBlogCount = setting.TakeSideRandomBlogCount; if (RandomBlogCount > BlogsCount) { RandomBlogCount = BlogsCount; } for (int i = 0; i < RandomBlogCount; i++) { int offset; do { offset = r.Next(0, BlogsCount); }while (randomList.Contains(offset)); randomList.Add(offset); blogs.Add(blogRepository.GetAll().Where(p => p.IsActive == true).Skip(offset).FirstOrDefault()); } ViewBag.RandomBlogs = BlogShow.GetShowItems(blogs, commentRepository, categoryRepository); }
public IActionResult GetMoreCategoryBlogs(int blogCounts, int CategoryId) { var setting = settingRepository.GetById(1); int TakeBlogCount = setting.TakeBlogCount; var category = categoryRepository.GetById(CategoryId); List <Blog> allSubBlogs = new List <Blog>(); if (category.ParentId == 0) { List <Blog> all = new List <Blog>(); var subCategories = categoryRepository.GetAll().Where(p => p.IsActive == true && p.ParentId == category.CategoryId).ToList(); foreach (var item in subCategories) { var subBlogs = blogRepository.GetAll().Where(p => p.IsActive == true && p.CategoryId == item.CategoryId); all.AddRange(subBlogs); } allSubBlogs = all.OrderByDescending(p => p.PublishedDate).Skip(blogCounts).Take(TakeBlogCount).ToList(); } else { allSubBlogs = blogRepository.GetAll() .Where(p => p.IsActive == true && p.CategoryId == CategoryId) .OrderByDescending(p => p.PublishedDate) .Skip(blogCounts).Take(TakeBlogCount).ToList(); } var blogShow = BlogShow.GetShowItems(allSubBlogs, commentRepository, categoryRepository); var bf = true; if (allSubBlogs.Count() == 0) { bf = false; } return(new JsonResult(new { newBlogCounts = blogCounts + TakeBlogCount, blogFind = bf, blogs = blogShow })); }
public IActionResult GetMoreBlog(int blogCounts) { var setting = settingRepository.GetById(1); int TakeBlogCount = setting.TakeBlogCount; var blogAll = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => p.PublishedDate); var blogGet = blogAll.Skip(blogCounts).Take(TakeBlogCount).ToList(); var blogShow = BlogShow.GetShowItems(blogGet, commentRepository, categoryRepository); var bf = true; if (blogGet.Count() == 0) { bf = false; } return(new JsonResult(new { newBlogCounts = blogCounts + TakeBlogCount, blogFind = bf, blogs = blogShow })); }
public IActionResult GetBlogsByCategory(int id) { var setting = settingRepository.GetById(1); int TakeBlogCount = setting.TakeBlogCount; var category = categoryRepository.GetById(id); List <Blog> allSubBlogs = new List <Blog>(); if (category.ParentId == 0) { var subCategories = categoryRepository.GetAll().Where(p => p.IsActive == true && p.ParentId == category.CategoryId).ToList(); foreach (var item in subCategories) { var subBlogs = blogRepository.GetAll().Where(p => p.IsActive == true && p.CategoryId == item.CategoryId).ToList(); allSubBlogs.AddRange(subBlogs); } ViewBag.CategoryActive = id; } else { allSubBlogs = blogRepository.GetAll().Where(p => p.IsActive == true && p.CategoryId == id).ToList(); ViewBag.CategorySubActive = id; } string categoryName = category.Name; ViewBag.Title = char.ToUpper(categoryName[0]) + categoryName.Substring(1).ToLower() + " | " + settingRepository.GetById(1).SiteName; ViewBag.Description = category.Description; ViewBag.CategoryId = id; var blogs = allSubBlogs.OrderByDescending(p => p.PublishedDate).Take(TakeBlogCount).ToList(); var blogShow = BlogShow.GetShowItems(blogs, commentRepository, categoryRepository); return(View(blogShow)); }
public IActionResult Detail(int id) { var setting = settingRepository.GetById(1); // Comments int commentGetCount = setting.TakeCommentCount; int answerGetCount = setting.TakeAnswerCount; var comments = commentRepository.GetAll().Where(p => p.BlogId == id && p.IsActive == true); if (comments.Count() != 0) { var commentsParent = comments.Where(p => p.ParentId == 0).OrderByDescending(c => c.CreateTime).Take(commentGetCount).OrderBy(c => c.CreateTime).ToList(); List <Comment> answers = new List <Comment>(); List <CommentAnswerCounts> commentAnswerCounts = new List <CommentAnswerCounts>(); foreach (var item in commentsParent) { var ans = comments.Where(d => d.ParentId == item.CommentId).OrderByDescending(c => c.CreateTime).Take(answerGetCount).OrderBy(c => c.CreateTime).ToList(); commentAnswerCounts.Add( new CommentAnswerCounts { ParentId = item.CommentId, AnswerCount = comments.Where(d => d.ParentId == item.CommentId).Count() - ans.Count() }); answers.AddRange(ans); } ViewBag.CommentParents = commentsParent; ViewBag.CommentAnswers = answers; ViewBag.CommentParentCount = comments.Where(p => p.ParentId == 0).Count() - commentsParent.Count(); ViewBag.CommentAnswerCount = commentAnswerCounts; ViewBag.CommentAny = true; } else { ViewBag.CommentAny = false; } // Hits Cookie var blog = blogRepository.GetById(id); if (Request.Cookies["VisitorHitId"] == null) { CookieOptions cookie = new CookieOptions { Expires = DateTime.Now.AddYears(1) }; Response.Cookies.Append("VisitorHitId", "[" + blog.BlogId.ToString() + "]", cookie); string blogHit = blog.VisitorHit; blog.VisitorHit = (Convert.ToInt32(blog.VisitorHit) + 1).ToString(); blogRepository.UpdateBlog(blog); } else { string hitIds = Request.Cookies["VisitorHitId"]; if (hitIds.Contains("[" + blog.BlogId.ToString() + "]") == false) { Response.Cookies.Delete("VisitorHitId"); CookieOptions cookie = new CookieOptions { Expires = DateTime.Now.AddYears(1) }; Response.Cookies.Append("VisitorHitId", hitIds + "[" + blog.BlogId.ToString() + "]", cookie); string blogHit = blog.VisitorHit; blog.VisitorHit = (Convert.ToInt32(blog.VisitorHit) + 1).ToString(); blogRepository.UpdateBlog(blog); } } // Detail Side var BlogsMostRead = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => Convert.ToInt32(p.VisitorHit)).Take(setting.TakeSideMostReadCount).ToList(); ViewBag.MostReadBlogs = BlogShow.GetShowItems(BlogsMostRead, commentRepository, categoryRepository); var BlogsMostComment = blogRepository.GetAll().Where(p => p.IsActive == true).ToList(); ViewBag.MostCommentBlogs = BlogShow.GetShowItems(BlogsMostComment, commentRepository, categoryRepository).OrderByDescending(p => p.CommentCount).Take(setting.TakeSideMostCommentCount); var BlogsLastPublished = blogRepository.GetAll().Where(p => p.IsActive == true).OrderByDescending(p => p.PublishedDate).Take(setting.TakeSideLastPublishedCount).ToList(); ViewBag.LastPublishedBlogs = BlogShow.GetShowItems(BlogsLastPublished, commentRepository, categoryRepository); int BlogsCount = blogRepository.GetAll().Where(p => p.IsActive == true).Count(); Random r = new Random(); List <Blog> blogs = new List <Blog>(); List <int> randomList = new List <int>(); int RandomBlogCount = setting.TakeSideRandomBlogCount; if (RandomBlogCount > BlogsCount) { RandomBlogCount = BlogsCount; } for (int i = 0; i < RandomBlogCount; i++) { int offset; do { offset = r.Next(0, BlogsCount); }while (randomList.Contains(offset)); randomList.Add(offset); blogs.Add(blogRepository.GetAll().Where(p => p.IsActive == true).Skip(offset).FirstOrDefault()); } ViewBag.RandomBlogs = BlogShow.GetShowItems(blogs, commentRepository, categoryRepository); ViewBag.FacebookSharing = setting.FacebookSharing; ViewBag.TwitterSharing = setting.TwitterSharing; ViewBag.LinkedinSharing = setting.LinkedInSharing; ViewBag.Title = char.ToUpper(blog.Title[0]) + blog.Title.Substring(1).ToLower() + " | " + settingRepository.GetById(1).SiteName; ViewBag.Description = blog.Description; return(View(blog)); }