Browse Source

Added HTML support for blog post articles

tags/3.0.0
Teknikode 2 years ago
parent
commit
8554648174

+ 15
- 15
Teknik/Areas/Blog/Controllers/BlogController.cs View File

@@ -207,45 +207,45 @@ namespace Teknik.Areas.Blog.Controllers
}

[HttpPost]
public ActionResult CreatePost(int blogID, string title, string article)
public ActionResult CreatePost(CreatePostViewModel data)
{
BlogViewModel model = new BlogViewModel();
if (ModelState.IsValid)
{
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 (User.IsInRole("Admin") || db.Blogs.Where(b => b.User.Username == User.Identity.Name).FirstOrDefault() != null)
{
// Validate the fields
if (string.IsNullOrEmpty(title))
if (string.IsNullOrEmpty(data.Title))
{
model.Error = true;
model.ErrorMessage = "You must write something for the title";
return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
}

if (string.IsNullOrEmpty(article))
if (string.IsNullOrEmpty(data.Article))
{
model.Error = true;
model.ErrorMessage = "You must write something for the article";
return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
}

bool system = (blogID == Config.BlogConfig.ServerBlogId);
bool system = (data.BlogId == Config.BlogConfig.ServerBlogId);
if (system)
{
var user = db.Blogs.Where(b => b.User.Username == User.Identity.Name);
if (user != null)
{
blogID = user.First().BlogId;
data.BlogId = user.First().BlogId;
}
}
BlogPost post = db.BlogPosts.Create();
post.BlogId = blogID;
post.Title = title;
post.Article = article;
post.BlogId = data.BlogId;
post.Title = data.Title;
post.Article = data.Article;
post.System = system;
post.DatePosted = DateTime.Now;
post.DatePublished = DateTime.Now;
@@ -269,34 +269,34 @@ namespace Teknik.Areas.Blog.Controllers
}

[HttpPost]
public ActionResult EditPost(int postID, string title, string article)
public ActionResult EditPost(EditPostViewModel data)
{
PostViewModel model = new PostViewModel();
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)
{
model = new PostViewModel(post);
if (User.IsInRole("Admin") || post.Blog.User.Username == User.Identity.Name)
{
// Validate the fields
if (string.IsNullOrEmpty(title))
if (string.IsNullOrEmpty(data.Title))
{
model.Error = true;
model.ErrorMessage = "You must write something for the title";
return View("~/Areas/Blog/Views/Blog/EditPost.cshtml", model);
}

if (string.IsNullOrEmpty(article))
if (string.IsNullOrEmpty(data.Article))
{
model.Error = true;
model.ErrorMessage = "You must write something for the article";
return View("~/Areas/Blog/Views/Blog/EditPost.cshtml", model);
}

post.Title = title;
post.Article = article;
post.Title = data.Title;
post.Article = data.Article;
post.DateEdited = DateTime.Now;
db.Entry(post).State = EntityState.Modified;
db.SaveChanges();

+ 19
- 0
Teknik/Areas/Blog/ViewModels/CreatePostViewModel.cs 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; }
}
}

+ 19
- 0
Teknik/Areas/Blog/ViewModels/EditPostViewModel.cs 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; }
}
}

+ 3
- 3
Teknik/Areas/Blog/Views/Blog/EditPost.cshtml View File

@@ -26,18 +26,18 @@
</div>
</div>
<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="form-group col-sm-10 col-sm-offset-1">
<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 class="row">
<div class="form-group col-sm-10 col-sm-offset-1">
<label for="article"><h4>Article</h4></label>
<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 class="row">

+ 3
- 3
Teknik/Areas/Blog/Views/Blog/NewPost.cshtml View File

@@ -26,18 +26,18 @@
</div>
</div>
<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="form-group col-sm-10 col-sm-offset-1">
<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 class="row">
<div class="form-group col-sm-10 col-sm-offset-1">
<label for="article"><h4>Article</h4></label>
<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 class="row">

+ 2
- 0
Teknik/Teknik.csproj View File

@@ -235,6 +235,8 @@
<Compile Include="Areas\Blog\Models\BlogPostComment.cs" />
<Compile Include="Areas\Blog\ViewModels\BlogViewModel.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\Contact\ContactAreaRegistration.cs" />
<Compile Include="Areas\Contact\Controllers\ContactController.cs" />

+ 1
- 0
Utilities/Utilities/MarkdownHelper.cs View File

@@ -19,6 +19,7 @@ namespace Teknik.Utilities
// Transform the supplied text (Markdown) into HTML.
var markdownTransformer = new Markdown();
markdownTransformer.ExtraMode = true;
markdownTransformer.SafeMode = true;
string html = markdownTransformer.Transform(text);

// Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(

Loading…
Cancel
Save