From a4a11d707686c3a0549880b432a76501a3bf1bc0 Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Sun, 20 Dec 2015 20:56:06 -0800 Subject: [PATCH] Fixed server specific blog viewing (Set to be first blog in DB) --- Teknik/Areas/Blog/Controllers/BlogController.cs | 23 +++++++++++++++-------- Teknik/Areas/Blog/ViewModels/BlogViewModel.cs | 2 +- Teknik/Areas/Blog/Views/Blog/Blog.cshtml | 7 ++----- Teknik/Areas/Blog/Views/Blog/ViewPost.cshtml | 2 +- Teknik/Areas/Home/Controllers/HomeController.cs | 2 +- Teknik/Configuration/Config.cs | 13 +++++++++++-- Teknik/Helpers/Constants.cs | 2 +- Teknik/Views/Shared/_Layout.cshtml | 1 - Teknik/Web.config | 4 ++++ 9 files changed, 36 insertions(+), 20 deletions(-) diff --git a/Teknik/Areas/Blog/Controllers/BlogController.cs b/Teknik/Areas/Blog/Controllers/BlogController.cs index b783fe0..d7c2454 100644 --- a/Teknik/Areas/Blog/Controllers/BlogController.cs +++ b/Teknik/Areas/Blog/Controllers/BlogController.cs @@ -25,33 +25,40 @@ namespace Teknik.Areas.Blog.Controllers { Models.Blog blog = null; BlogViewModel model = null; + // The blog is the main site's blog if (string.IsNullOrEmpty(username)) { ViewBag.Title = "Teknik Blog - " + Config.Title; - blog = db.Blogs.Find(Constants.SERVERBLOGID); + var blogs = db.Blogs.Include("User").Where(p => (p.BlogId == Constants.SERVERBLOGID)); + if (blogs != null && blogs.Any()) + { + blog = blogs.First(); + blog.Title = Config.BlogTitle; + blog.Description = Config.BlogDescription; + } } - else + else // A user specific blog { - var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username); + var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Constants.SERVERBLOGID); if (blogs.Any()) { blog = blogs.First(); ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title; } } - // find the post specified + // find the blog specified if (blog != null) { - var foundPosts = db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.BlogId == blog.BlogId) && - (p.Published || p.Blog.User.Username == User.Identity.Name) - ).OrderByDescending(p => p.DatePosted); + var foundPosts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId)) + : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId) && + (p.Published || p.Blog.User.Username == User.Identity.Name)); model = new BlogViewModel(); model.BlogId = blog.BlogId; model.UserId = blog.UserId; model.User = blog.User; model.Title = blog.Title; model.Description = blog.Description; - model.Posts = (foundPosts != null && foundPosts.Any()) ? foundPosts.ToList() : null; + model.HasPosts = (foundPosts != null && foundPosts.Any()); return View(model); } diff --git a/Teknik/Areas/Blog/ViewModels/BlogViewModel.cs b/Teknik/Areas/Blog/ViewModels/BlogViewModel.cs index 9ef2781..a7088b4 100644 --- a/Teknik/Areas/Blog/ViewModels/BlogViewModel.cs +++ b/Teknik/Areas/Blog/ViewModels/BlogViewModel.cs @@ -20,6 +20,6 @@ namespace Teknik.Areas.Blog.ViewModels public User User { get; set; } - public List Posts { get; set; } + public bool HasPosts { get; set; } } } diff --git a/Teknik/Areas/Blog/Views/Blog/Blog.cshtml b/Teknik/Areas/Blog/Views/Blog/Blog.cshtml index 25c339d..ee6ff62 100644 --- a/Teknik/Areas/Blog/Views/Blog/Blog.cshtml +++ b/Teknik/Areas/Blog/Views/Blog/Blog.cshtml @@ -28,8 +28,6 @@
@if (Model != null && Model.User != null) { - if (Model.BlogId != Constants.SERVERBLOGID) - {

@Model.Title

@@ -39,11 +37,10 @@

- +

- } if (Model.User.Username == User.Identity.Name) {
@@ -115,7 +112,7 @@
} - if (Model.Posts != null && Model.Posts.Any()) + if (Model.HasPosts) {