1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Added HTML support for blog post articles

This commit is contained in:
Uncled1023 2017-08-11 15:27:02 -07:00
parent bb933c5ff1
commit 8554648174
7 changed files with 62 additions and 21 deletions

View File

@ -207,45 +207,45 @@ namespace Teknik.Areas.Blog.Controllers
} }
[HttpPost] [HttpPost]
public ActionResult CreatePost(int blogID, string title, string article) public ActionResult CreatePost(CreatePostViewModel data)
{ {
BlogViewModel model = new BlogViewModel(); BlogViewModel model = new BlogViewModel();
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
bool isAuth = User.IsInRole("Admin"); bool isAuth = User.IsInRole("Admin");
var blog = db.Blogs.Where(p => (p.BlogId == blogID) && (p.User.Username == User.Identity.Name || isAuth)).FirstOrDefault(); var blog = db.Blogs.Where(p => (p.BlogId == data.BlogId) && (p.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
if (blog != null) if (blog != null)
{ {
if (User.IsInRole("Admin") || db.Blogs.Where(b => b.User.Username == User.Identity.Name).FirstOrDefault() != null) if (User.IsInRole("Admin") || db.Blogs.Where(b => b.User.Username == User.Identity.Name).FirstOrDefault() != null)
{ {
// Validate the fields // Validate the fields
if (string.IsNullOrEmpty(title)) if (string.IsNullOrEmpty(data.Title))
{ {
model.Error = true; model.Error = true;
model.ErrorMessage = "You must write something for the title"; model.ErrorMessage = "You must write something for the title";
return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model); return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
} }
if (string.IsNullOrEmpty(article)) if (string.IsNullOrEmpty(data.Article))
{ {
model.Error = true; model.Error = true;
model.ErrorMessage = "You must write something for the article"; model.ErrorMessage = "You must write something for the article";
return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model); return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
} }
bool system = (blogID == Config.BlogConfig.ServerBlogId); bool system = (data.BlogId == Config.BlogConfig.ServerBlogId);
if (system) if (system)
{ {
var user = db.Blogs.Where(b => b.User.Username == User.Identity.Name); var user = db.Blogs.Where(b => b.User.Username == User.Identity.Name);
if (user != null) if (user != null)
{ {
blogID = user.First().BlogId; data.BlogId = user.First().BlogId;
} }
} }
BlogPost post = db.BlogPosts.Create(); BlogPost post = db.BlogPosts.Create();
post.BlogId = blogID; post.BlogId = data.BlogId;
post.Title = title; post.Title = data.Title;
post.Article = article; post.Article = data.Article;
post.System = system; post.System = system;
post.DatePosted = DateTime.Now; post.DatePosted = DateTime.Now;
post.DatePublished = DateTime.Now; post.DatePublished = DateTime.Now;
@ -269,34 +269,34 @@ namespace Teknik.Areas.Blog.Controllers
} }
[HttpPost] [HttpPost]
public ActionResult EditPost(int postID, string title, string article) public ActionResult EditPost(EditPostViewModel data)
{ {
PostViewModel model = new PostViewModel(); PostViewModel model = new PostViewModel();
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
BlogPost post = db.BlogPosts.Where(p => p.BlogPostId == postID).FirstOrDefault(); BlogPost post = db.BlogPosts.Where(p => p.BlogPostId == data.PostId).FirstOrDefault();
if (post != null) if (post != null)
{ {
model = new PostViewModel(post); model = new PostViewModel(post);
if (User.IsInRole("Admin") || post.Blog.User.Username == User.Identity.Name) if (User.IsInRole("Admin") || post.Blog.User.Username == User.Identity.Name)
{ {
// Validate the fields // Validate the fields
if (string.IsNullOrEmpty(title)) if (string.IsNullOrEmpty(data.Title))
{ {
model.Error = true; model.Error = true;
model.ErrorMessage = "You must write something for the title"; model.ErrorMessage = "You must write something for the title";
return View("~/Areas/Blog/Views/Blog/EditPost.cshtml", model); return View("~/Areas/Blog/Views/Blog/EditPost.cshtml", model);
} }
if (string.IsNullOrEmpty(article)) if (string.IsNullOrEmpty(data.Article))
{ {
model.Error = true; model.Error = true;
model.ErrorMessage = "You must write something for the article"; model.ErrorMessage = "You must write something for the article";
return View("~/Areas/Blog/Views/Blog/EditPost.cshtml", model); return View("~/Areas/Blog/Views/Blog/EditPost.cshtml", model);
} }
post.Title = title; post.Title = data.Title;
post.Article = article; post.Article = data.Article;
post.DateEdited = DateTime.Now; post.DateEdited = DateTime.Now;
db.Entry(post).State = EntityState.Modified; db.Entry(post).State = EntityState.Modified;
db.SaveChanges(); db.SaveChanges();

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teknik.ViewModels;
namespace Teknik.Areas.Blog.ViewModels
{
public class CreatePostViewModel : ViewModelBase
{
public int BlogId { get; set; }
public string Title { get; set; }
[AllowHtml]
public string Article { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Teknik.ViewModels;
namespace Teknik.Areas.Blog.ViewModels
{
public class EditPostViewModel : ViewModelBase
{
public int PostId { get; set; }
public string Title { get; set; }
[AllowHtml]
public string Article { get; set; }
}
}

View File

@ -26,18 +26,18 @@
</div> </div>
</div> </div>
<form class="form" action="@Url.SubRouteUrl("blog", "Blog.Action", new { action = "EditPost" })" method="post" id="newPost"> <form class="form" action="@Url.SubRouteUrl("blog", "Blog.Action", new { action = "EditPost" })" method="post" id="newPost">
<input name="postID" id="postID" type="hidden" value="@Model.PostId" /> <input name="PostID" id="postID" type="hidden" value="@Model.PostId" />
<div class="row"> <div class="row">
<div class="form-group col-sm-10 col-sm-offset-1"> <div class="form-group col-sm-10 col-sm-offset-1">
<label for="title"><h4>Title</h4></label> <label for="title"><h4>Title</h4></label>
<input class="form-control" name="title" id="title" placeholder="generic click bait" title="enter a title for your post." type="text" value="@Model.Title" /> <input class="form-control" name="Title" id="title" placeholder="generic click bait" title="enter a title for your post." type="text" value="@Model.Title" />
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-sm-10 col-sm-offset-1"> <div class="form-group col-sm-10 col-sm-offset-1">
<label for="article"><h4>Article</h4></label> <label for="article"><h4>Article</h4></label>
<div class="mdd_toolbar"></div> <div class="mdd_toolbar"></div>
<textarea class="form-control mdd_editor" name="article" id="article" placeholder="I ate a burger today." title="enter any information you want to share with the world." data-provide="markdown" rows="10">@Model.Article</textarea> <textarea class="form-control mdd_editor" name="Article" id="article" placeholder="I ate a burger today." title="enter any information you want to share with the world." data-provide="markdown" rows="10">@Model.Article</textarea>
</div> </div>
</div> </div>
<div class="row"> <div class="row">

View File

@ -26,18 +26,18 @@
</div> </div>
</div> </div>
<form class="form" action="@Url.SubRouteUrl("blog", "Blog.Action", new { action = "CreatePost" })" method="post" id="newPost"> <form class="form" action="@Url.SubRouteUrl("blog", "Blog.Action", new { action = "CreatePost" })" method="post" id="newPost">
<input name="blogID" id="blogID" type="hidden" value="@Model.BlogId" /> <input name="BlogID" id="blogID" type="hidden" value="@Model.BlogId" />
<div class="row"> <div class="row">
<div class="form-group col-sm-10 col-sm-offset-1"> <div class="form-group col-sm-10 col-sm-offset-1">
<label for="title"><h4>Title</h4></label> <label for="title"><h4>Title</h4></label>
<input class="form-control" name="title" id="title" placeholder="generic click bait" title="enter a title for your post." type="text" /> <input class="form-control" name="Title" id="title" placeholder="generic click bait" title="enter a title for your post." type="text" />
</div> </div>
</div> </div>
<div class="row"> <div class="row">
<div class="form-group col-sm-10 col-sm-offset-1"> <div class="form-group col-sm-10 col-sm-offset-1">
<label for="article"><h4>Article</h4></label> <label for="article"><h4>Article</h4></label>
<div class="mdd_toolbar"></div> <div class="mdd_toolbar"></div>
<textarea class="form-control mdd_editor" name="article" id="article" placeholder="I ate a burger today." title="enter any information you want to share with the world." data-provide="markdown" rows="10"></textarea> <textarea class="form-control mdd_editor" name="Article" id="article" placeholder="I ate a burger today." title="enter any information you want to share with the world." data-provide="markdown" rows="10"></textarea>
</div> </div>
</div> </div>
<div class="row"> <div class="row">

View File

@ -235,6 +235,8 @@
<Compile Include="Areas\Blog\Models\BlogPostComment.cs" /> <Compile Include="Areas\Blog\Models\BlogPostComment.cs" />
<Compile Include="Areas\Blog\ViewModels\BlogViewModel.cs" /> <Compile Include="Areas\Blog\ViewModels\BlogViewModel.cs" />
<Compile Include="Areas\Blog\ViewModels\CommentViewModel.cs" /> <Compile Include="Areas\Blog\ViewModels\CommentViewModel.cs" />
<Compile Include="Areas\Blog\ViewModels\CreatePostViewModel.cs" />
<Compile Include="Areas\Blog\ViewModels\EditPostViewModel.cs" />
<Compile Include="Areas\Blog\ViewModels\PostViewModel.cs" /> <Compile Include="Areas\Blog\ViewModels\PostViewModel.cs" />
<Compile Include="Areas\Contact\ContactAreaRegistration.cs" /> <Compile Include="Areas\Contact\ContactAreaRegistration.cs" />
<Compile Include="Areas\Contact\Controllers\ContactController.cs" /> <Compile Include="Areas\Contact\Controllers\ContactController.cs" />

View File

@ -19,6 +19,7 @@ namespace Teknik.Utilities
// Transform the supplied text (Markdown) into HTML. // Transform the supplied text (Markdown) into HTML.
var markdownTransformer = new Markdown(); var markdownTransformer = new Markdown();
markdownTransformer.ExtraMode = true; markdownTransformer.ExtraMode = true;
markdownTransformer.SafeMode = true;
string html = markdownTransformer.Transform(text); string html = markdownTransformer.Transform(text);
// Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :( // Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(