From 1bfddb9ec99bd955c279d0bbb181cc0488e4692b Mon Sep 17 00:00:00 2001 From: Uncled1023 Date: Thu, 17 Dec 2015 14:14:11 -0800 Subject: [PATCH] Fixed blogs not showing correctly. Added individual blog post viewing. Fixed javascript to use valid urls. Fixed Route registrations. --- Teknik/Areas/Blog/BlogAreaRegistration.cs | 14 ++ .../Areas/Blog/Controllers/BlogController.cs | 50 ++++-- Teknik/Areas/Blog/Scripts/Blog.js | 60 ++++--- Teknik/Areas/Blog/ViewModels/PostViewModel.cs | 21 +++ Teknik/Areas/Blog/Views/Blog/Blog.cshtml | 62 +++---- Teknik/Areas/Blog/Views/Blog/Post.cshtml | 65 +++---- Teknik/Areas/Blog/Views/Blog/Posts.cshtml | 17 ++ Teknik/Areas/Blog/Views/Blog/ViewPost.cshtml | 164 ++++++++++++++++++ .../Areas/Contact/ContactAreaRegistration.cs | 4 +- .../Profile/Controllers/ProfileController.cs | 2 +- .../Areas/Profile/ProfileAreaRegistration.cs | 18 +- .../Areas/Profile/Views/Profile/Login.cshtml | 8 +- .../Profile/Views/Profile/Register.cshtml | 4 - Teknik/Configuration/Config.cs | 24 ++- .../PublishProfiles/Teknik Dev.pubxml | 2 +- Teknik/Teknik.csproj | 2 + Teknik/Views/Shared/_LoginPartial.cshtml | 4 +- Teknik/Views/Shared/_Navbar.cshtml | 4 +- 18 files changed, 380 insertions(+), 145 deletions(-) create mode 100644 Teknik/Areas/Blog/Views/Blog/Posts.cshtml create mode 100644 Teknik/Areas/Blog/Views/Blog/ViewPost.cshtml diff --git a/Teknik/Areas/Blog/BlogAreaRegistration.cs b/Teknik/Areas/Blog/BlogAreaRegistration.cs index 8eaab78..0b4e6d1 100644 --- a/Teknik/Areas/Blog/BlogAreaRegistration.cs +++ b/Teknik/Areas/Blog/BlogAreaRegistration.cs @@ -29,6 +29,13 @@ namespace Teknik.Areas.Blog new { controller = "Blog", action = "Post", username = "", id = 0 }, // Parameter defaults new[] { typeof(Controllers.BlogController).Namespace } ); + context.MapSubdomainRoute( + "Blog_dev_post_unique", // Route name + "dev", + "Blog/Action/{controller}/{action}", // URL with parameters + new { controller = "Blog", action = "Blog" }, // Parameter defaults + new[] { typeof(Controllers.BlogController).Namespace } + ); context.MapSubdomainRoute( "Blog_default_blog", // Route name "blog", @@ -43,6 +50,13 @@ namespace Teknik.Areas.Blog new { controller = "Blog", action = "Post", username = "", id = 0 }, // Parameter defaults new[] { typeof(Controllers.BlogController).Namespace } ); + context.MapSubdomainRoute( + "Blog_default_post_unique", // Route name + "blog", + "Action/{controller}/{action}", // URL with parameters + new { controller = "Blog", action = "Blog" }, // Parameter defaults + new[] { typeof(Controllers.BlogController).Namespace } + ); // Register Bundles BundleTable.Bundles.Add(new ScriptBundle("~/bundles/blog").Include( diff --git a/Teknik/Areas/Blog/Controllers/BlogController.cs b/Teknik/Areas/Blog/Controllers/BlogController.cs index 5d1d125..09eafcc 100644 --- a/Teknik/Areas/Blog/Controllers/BlogController.cs +++ b/Teknik/Areas/Blog/Controllers/BlogController.cs @@ -46,24 +46,31 @@ namespace Teknik.Areas.Blog.Controllers Models.Blog blog = null; if (string.IsNullOrEmpty(username)) { + ViewBag.Title = "Teknik Blog - " + Config.Title; blog = db.Blogs.Find(Constants.SERVERBLOGID); } else { - var blogs = db.Blogs.Include("Blog.User").Where(p => p.User.Username == username); + var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username); if (blogs.Any()) { blog = blogs.First(); + ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title; } } // find the post 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) + ); BlogViewModel model = new BlogViewModel(); model.BlogId = blog.BlogId; model.UserId = blog.UserId; model.User = blog.User; - model.Posts = blog.Posts; + model.Title = blog.Title; + model.Description = blog.Description; + model.Posts = (foundPosts != null && foundPosts.Any()) ? foundPosts.ToList() : null; return View(model); } @@ -79,31 +86,36 @@ namespace Teknik.Areas.Blog.Controllers return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } // find the post specified - - var post = db.Posts.Include("Blog").Include("Blog.User").Where(p => p.Blog.User.Username == username && p.PostId == id); - if (post.Any()) + var post = db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.Blog.User.Username == username && p.PostId == id) && + (p.Published || p.Blog.User.Username == User.Identity.Name) + ); + if (post != null && post.Any()) { Post curPost = post.First(); - PostViewModel model = new PostViewModel(); - model.BlogId = curPost.BlogId; - model.PostId = curPost.PostId; - model.DatePosted = curPost.DatePosted; - model.Published = curPost.Published; - model.DatePublished = curPost.DatePublished; - model.Title = curPost.Title; - model.Tags = curPost.Tags; - model.Article = curPost.Article; + PostViewModel model = new PostViewModel(curPost); - return View(model); + ViewBag.Title = model.Title + " - " + username + "'s Blog - " + Config.Title; + return View("~/Areas/Blog/Views/Blog/ViewPost.cshtml", model); } - return new HttpStatusCodeResult(HttpStatusCode.NotFound); + return View("~/Areas/Blog/Views/Blog/ViewPost.cshtml", null); } + [HttpPost] + [AllowAnonymous] public ActionResult GetPosts(int blogID, int startPostID, int count) { - object model = null; - - return PartialView("Post", model); + var posts = db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blogID && p.PostId > startPostID) && + (p.Published || p.Blog.User.Username == User.Identity.Name) + ).Take(count); + List postViews = new List(); + if (posts != null) + { + foreach (Post post in posts) + { + postViews.Add(new PostViewModel(post)); + } + } + return PartialView("~/Areas/Blog/Views/Blog/Posts.cshtml", postViews); } // GET: Blogs/Create diff --git a/Teknik/Areas/Blog/Scripts/Blog.js b/Teknik/Areas/Blog/Scripts/Blog.js index aa72452..634e6c5 100644 --- a/Teknik/Areas/Blog/Scripts/Blog.js +++ b/Teknik/Areas/Blog/Scripts/Blog.js @@ -1,13 +1,12 @@ $(document).ready(function () { $("#blog_submit").click(function () { $('#newPost').modal('hide'); - userID = encodeURIComponent($("#blog_userid").val()); title = encodeURIComponent($("#blog_title").val()); post = encodeURIComponent($("#blog_post").val()); $.ajax({ type: "POST", - url: "../../../add_post.php", - data: "userID=" + userID + "&title=" + title + "&post=" + post, + url: addPostURL, + data: { title: title, post: post }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -23,12 +22,12 @@ $('#editPost').on('show.bs.modal', function (e) { $("#edit_blog_post").val(""); - userID = encodeURIComponent($(e.relatedTarget).attr("id")); - $("#edit_blog_postid").val(userID); + postID = encodeURIComponent($(e.relatedTarget).attr("id")); + $("#edit_blog_postid").val(postID); $.ajax({ type: "POST", - url: "../../../get_title_content.php", - data: "id=" + userID, + url: getPostTitleURL, + data: { id: postID }, success: function (html) { if (html) { $("#edit_blog_title").val(html); @@ -37,8 +36,8 @@ }); $.ajax({ type: "POST", - url: "../../../get_post_content.php", - data: "id=" + userID, + url: getPostArticleURL, + data: { id: postID }, success: function (html) { if (html) { $("#edit_blog_post").val(html); @@ -50,13 +49,12 @@ $("#edit_submit").click(function () { $('#editPost').modal('hide'); postID = encodeURIComponent($("#edit_blog_postid").val()); - userID = encodeURIComponent($("#edit_blog_userid").val()); title = encodeURIComponent($("#edit_blog_title").val()); post = encodeURIComponent($("#edit_blog_post").val()); $.ajax({ type: "POST", - url: "../../../edit_post.php", - data: "userID=" + userID + "&postID=" + postID + "&title=" + title + "&post=" + post, + url: editPostURL, + data: { postID: postID, title: title, post: post }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -76,8 +74,8 @@ post = encodeURIComponent($("#comment_post").val()); $.ajax({ type: "POST", - url: "../../../includes/add_comment.php", - data: "postID=" + postID + "&service=blog&comment=" + post, + url: addCommentURL, + data: { postID: postID, service: 'blog', comment: post }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -97,8 +95,8 @@ $("#edit_comment_postid").val(commentID); $.ajax({ type: "POST", - url: "../../../includes/get_comment_content.php", - data: "id=" + commentID, + url: getCommentContentURL, + data: { id: commentID }, success: function (html) { if (html) { $("#edit_comment_post").val(html); @@ -113,8 +111,8 @@ post = encodeURIComponent($("#edit_comment_post").val()); $.ajax({ type: "POST", - url: "../../../includes/edit_comment.php", - data: "commentID=" + postID + "&post=" + post, + url: editCommentURL, + data: { commentID: postID, post: post }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -130,7 +128,7 @@ var imageUpload = $('#upload_image').upload({ name: 'file', - action: '../../../includes/upload.php', + action: uploadURL, enctype: 'multipart/form-data', params: {}, autoSubmit: true, @@ -153,8 +151,8 @@ function loadMorePosts(start, count) { blog_id = encodeURIComponent($(".blog-main").attr("id")); $.ajax({ type: "POST", - url: "../../../get_post.php", - data: "userID=" + blog_id + "&postCount=" + count + "&startPost=" + start, + url: getPostsURL, + data: { blogID: blog_id, count: count, startPostID: start }, success: function (html) { if (html) { $(".blog-main").append(html); @@ -171,8 +169,8 @@ function loadMoreComments(start, count) { post_id = encodeURIComponent($(".post-comments").attr("id")); $.ajax({ type: "POST", - url: "../../../includes/get_comment.php", - data: "postID=" + post_id + "&service=blog&postCount=" + count + "&startPost=" + start, + url: getCommentsURL, + data: { postID: post_id, service: 'blog', postCount: count, startPost: start }, success: function (html) { if (html) { $(".post-comments").append(html); @@ -205,8 +203,8 @@ function linkPostUnpublish(selector) { post_id = encodeURIComponent(object.attr("id")); $.ajax({ type: "POST", - url: "../../../publish_post.php", - data: "publish=0&id=" + post_id, + url: publishPostURL, + data: { publish: false, id: post_id }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -226,8 +224,8 @@ function linkPostPublish(selector) { post_id = encodeURIComponent(object.attr("id")); $.ajax({ type: "POST", - url: "../../../publish_post.php", - data: "publish=1&id=" + post_id, + url: publishPostURL, + data: { publish: true, id: post_id }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -249,8 +247,8 @@ function linkPostDelete(selector) { if (result) { $.ajax({ type: "POST", - url: "../../../delete_post.php", - data: "id=" + post_id, + url: deletePostURL, + data: { id: post_id }, success: function (html) { if (html == 'true') { window.location.reload(); @@ -274,8 +272,8 @@ function linkCommentDelete(selector) { if (result) { $.ajax({ type: "POST", - url: "../../../includes/delete_comment.php", - data: "id=" + post_id, + url: deleteCommentURL, + data: { id: post_id }, success: function (html) { if (html == 'true') { window.location.reload(); diff --git a/Teknik/Areas/Blog/ViewModels/PostViewModel.cs b/Teknik/Areas/Blog/ViewModels/PostViewModel.cs index bbf7a46..43507c9 100644 --- a/Teknik/Areas/Blog/ViewModels/PostViewModel.cs +++ b/Teknik/Areas/Blog/ViewModels/PostViewModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using Teknik.Areas.Blog.Models; +using System.Security.Principal; using System.Linq; using System.Web; @@ -12,6 +13,8 @@ namespace Teknik.Areas.Blog.ViewModels public int BlogId { get; set; } + public Models.Blog Blog { get; set; } + public DateTime DatePosted { get; set; } public DateTime DatePublished { get; set; } @@ -23,5 +26,23 @@ namespace Teknik.Areas.Blog.ViewModels public string Article { get; set; } public List Tags { get; set; } + + public PostViewModel(Post post) + { + BlogId = post.BlogId; + PostId = post.PostId; + Blog = post.Blog; + DatePosted = post.DatePosted; + Published = post.Published; + DatePublished = post.DatePublished; + Title = post.Title; + Tags = post.Tags; + Article = post.Article; + } + + public bool CanView(IPrincipal user) + { + return (Published || Blog.User.Username == user.Identity.Name || user.IsInRole("Admin")); + } } } \ No newline at end of file diff --git a/Teknik/Areas/Blog/Views/Blog/Blog.cshtml b/Teknik/Areas/Blog/Views/Blog/Blog.cshtml index d5fa062..31924ac 100644 --- a/Teknik/Areas/Blog/Views/Blog/Blog.cshtml +++ b/Teknik/Areas/Blog/Views/Blog/Blog.cshtml @@ -1,29 +1,48 @@ @model Teknik.Areas.Blog.ViewModels.BlogViewModel +@using Teknik.Helpers + + + @Scripts.Render("~/bundles/blog") +
- = 0) + @if (Model.BlogId != Constants.SERVERBLOGID) { - ?>
-

-

+

@Model.Title

+

@Model.Description

- +

-
@@ -38,7 +57,6 @@