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

Fixed blog errors.

This commit is contained in:
Uncled1023 2015-12-23 22:36:33 -08:00
parent fcf8c9bc1a
commit c6fdf82486
10 changed files with 75 additions and 41 deletions

View File

@ -61,6 +61,8 @@ namespace Teknik.Areas.Blog
// Register Script Bundles // Register Script Bundles
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/blog").Include( BundleTable.Bundles.Add(new ScriptBundle("~/bundles/blog").Include(
"~/Scripts/ocupload/1.1.2/ocupload.js", "~/Scripts/ocupload/1.1.2/ocupload.js",
"~/Scripts/PageDown/Markdown.Converter.js",
"~/Scripts/PageDown/Markdown.Sanitizer.js",
"~/Scripts/bootstrap/markdown/bootstrap-markdown.js", "~/Scripts/bootstrap/markdown/bootstrap-markdown.js",
"~/Scripts/bootbox/bootbox.min.js", "~/Scripts/bootbox/bootbox.min.js",
"~/Areas/Blog/Scripts/Blog.js")); "~/Areas/Blog/Scripts/Blog.js"));

View File

@ -23,44 +23,50 @@ namespace Teknik.Areas.Blog.Controllers
[AllowAnonymous] [AllowAnonymous]
public ActionResult Blog(string username) public ActionResult Blog(string username)
{ {
Models.Blog blog = null;
BlogViewModel model = new BlogViewModel(); BlogViewModel model = new BlogViewModel();
// The blog is the main site's blog // The blog is the main site's blog
if (string.IsNullOrEmpty(username)) if (string.IsNullOrEmpty(username))
{ {
ViewBag.Title = "Teknik Blog - " + Config.Title; ViewBag.Title = "Teknik Blog - " + Config.Title;
var blogs = db.Blogs.Include("User").Where(p => (p.BlogId == Constants.SERVERBLOGID)); var foundPosts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.System))
if (blogs != null && blogs.Any()) : db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.System && p.Published));
{ model = new BlogViewModel();
blog = blogs.First(); model.BlogId = Constants.SERVERBLOGID;
blog.Title = Config.BlogConfig.Title;
blog.Description = Config.BlogConfig.Description; User user = (User.IsInRole("Admin")) ? db.Users.Where(u => u.Username == User.Identity.Name).First() : null;
} model.UserId = (user != null) ? user.UserId : 0;
model.User = user;
model.Title = Config.BlogConfig.Title;
model.Description = Config.BlogConfig.Description;
model.HasPosts = (foundPosts != null && foundPosts.Any());
return View(model);
} }
else // A user specific blog else // A user specific blog
{ {
var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username && p.BlogId != Constants.SERVERBLOGID); Models.Blog blog = null;
var blogs = db.Blogs.Include("User").Where(p => p.User.Username == username);
if (blogs.Any()) if (blogs.Any())
{ {
blog = blogs.First(); blog = blogs.First();
ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title; ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title;
} }
} // find the blog specified
// find the blog specified if (blog != null)
if (blog != null) {
{ var foundPosts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId && !p.System))
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.System) &&
: db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blog.BlogId) && (p.Published || p.Blog.User.Username == User.Identity.Name));
(p.Published || p.Blog.User.Username == User.Identity.Name)); model = new BlogViewModel();
model = new BlogViewModel(); model.BlogId = blog.BlogId;
model.BlogId = blog.BlogId; model.UserId = blog.UserId;
model.UserId = blog.UserId; model.User = blog.User;
model.User = blog.User; model.Title = blog.Title;
model.Title = blog.Title; model.Description = blog.Description;
model.Description = blog.Description; model.HasPosts = (foundPosts != null && foundPosts.Any());
model.HasPosts = (foundPosts != null && foundPosts.Any());
return View(model); return View(model);
}
} }
model.Error = true; model.Error = true;
return View(model); return View(model);
@ -93,8 +99,8 @@ namespace Teknik.Areas.Blog.Controllers
[AllowAnonymous] [AllowAnonymous]
public ActionResult GetPosts(int blogID, int startPostID, int count) public ActionResult GetPosts(int blogID, int startPostID, int count)
{ {
var posts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => p.BlogId == blogID).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList() var posts = (User.IsInRole("Admin")) ? db.Posts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Constants.SERVERBLOGID))).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList()
: db.Posts.Include("Blog").Include("Blog.User").Where(p => (p.BlogId == blogID) && (p.Published || p.Blog.User.Username == User.Identity.Name) : db.Posts.Include("Blog").Include("Blog.User").Where(p => ((p.BlogId == blogID && !p.System) || (p.System && blogID == Constants.SERVERBLOGID)) && (p.Published || p.Blog.User.Username == User.Identity.Name)
).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList(); ).OrderByDescending(p => p.DatePosted).Skip(startPostID).Take(count).ToList();
List<PostViewModel> postViews = new List<PostViewModel>(); List<PostViewModel> postViews = new List<PostViewModel>();
if (posts != null) if (posts != null)
@ -141,10 +147,20 @@ namespace Teknik.Areas.Blog.Controllers
{ {
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
bool system = (blogID == Constants.SERVERBLOGID);
if (system)
{
var user = db.Blogs.Include("User").Where(b => b.User.Username == User.Identity.Name);
if (user != null)
{
blogID = user.First().BlogId;
}
}
Post post = db.Posts.Create(); Post post = db.Posts.Create();
post.BlogId = blogID; post.BlogId = blogID;
post.Title = title; post.Title = title;
post.Article = article; post.Article = article;
post.System = system;
post.DatePosted = DateTime.Now; post.DatePosted = DateTime.Now;
post.DatePublished = DateTime.Now; post.DatePublished = DateTime.Now;

View File

@ -12,6 +12,8 @@ namespace Teknik.Areas.Blog.Models
public Blog Blog { get; set; } public Blog Blog { get; set; }
public bool System { get; set; }
public DateTime DatePosted { get; set; } public DateTime DatePosted { get; set; }
public DateTime DatePublished { get; set; } public DateTime DatePublished { get; set; }

View File

@ -16,6 +16,8 @@ namespace Teknik.Areas.Blog.ViewModels
public Models.Blog Blog { get; set; } public Models.Blog Blog { get; set; }
public bool System { get; set; }
public DateTime DatePosted { get; set; } public DateTime DatePosted { get; set; }
public DateTime DatePublished { get; set; } public DateTime DatePublished { get; set; }
@ -35,6 +37,7 @@ namespace Teknik.Areas.Blog.ViewModels
BlogId = post.BlogId; BlogId = post.BlogId;
PostId = post.PostId; PostId = post.PostId;
Blog = post.Blog; Blog = post.Blog;
System = post.System;
DatePosted = post.DatePosted; DatePosted = post.DatePosted;
Published = post.Published; Published = post.Published;
DatePublished = post.DatePublished; DatePublished = post.DatePublished;

View File

@ -41,7 +41,7 @@
</p> </p>
</div> </div>
</div> </div>
if (Model.User.Username == User.Identity.Name) if (User.IsInRole("Admin") || (Model.User != null && Model.User.Username == User.Identity.Name))
{ {
<div class="row"> <div class="row">
<div class="col-sm-12 text-center"> <div class="col-sm-12 text-center">

View File

@ -28,7 +28,7 @@
<div class="container"> <div class="container">
@if (Model != null) @if (Model != null)
{ {
if (Model.Blog.User.Username == User.Identity.Name) if (User.IsInRole("Admin") || Model.Blog.User.Username == User.Identity.Name)
{ {
<div class="modal fade" id="editPost" tabindex="-1" role="dialog" aria-labelledby="editPostLabel" aria-hidden="true"> <div class="modal fade" id="editPost" tabindex="-1" role="dialog" aria-labelledby="editPostLabel" aria-hidden="true">
<div class="modal-dialog"> <div class="modal-dialog">
@ -65,7 +65,7 @@
} }
<ol class="breadcrumb"> <ol class="breadcrumb">
<li><a href="@Url.SubRouteUrl("blog", "Blog.Blog", new { username = (Model.BlogId == Constants.SERVERBLOGID) ? string.Empty : Model.Blog.User.Username })">@((Model.BlogId == Constants.SERVERBLOGID) ? Model.Config.BlogConfig.Title : Model.Blog.Title)</a></li> <li><a href="@Url.SubRouteUrl("blog", "Blog.Blog", new { username = (Model.System) ? string.Empty : Model.Blog.User.Username })">@((Model.System) ? Model.Config.BlogConfig.Title : (string.IsNullOrEmpty(Model.Blog.Title) ? string.Format("{0}'s Blog", Model.Blog.User.Username) : Model.Blog.Title))</a></li>
<li class="active"><a href="#">@Model.Title</a></li> <li class="active"><a href="#">@Model.Title</a></li>
</ol> </ol>

View File

@ -22,12 +22,12 @@ namespace Teknik.Areas.Home.Controllers
HomeViewModel model = new HomeViewModel(); HomeViewModel model = new HomeViewModel();
// Grab the latest site blog posts // Grab the latest site blog posts
List<Post> lastSite = new List<Post>(); List<Post> lastSite = new List<Post>();
var foundSite = db.Posts.Include("Blog").Include("Blog.User").OrderBy(post => post.DatePosted).Where(p => p.Published && p.BlogId == Constants.SERVERBLOGID).Take(10); var foundSite = db.Posts.Include("Blog").Include("Blog.User").OrderBy(post => post.DatePosted).Where(p => p.Published && p.System).Take(10);
if (foundSite != null) if (foundSite != null)
lastSite = foundSite.ToList(); lastSite = foundSite.ToList();
// Grab the latest user blog posts // Grab the latest user blog posts
List<Post> lastPosts = new List<Post>(); List<Post> lastPosts = new List<Post>();
var foundPosts = db.Posts.Include("Blog").Include("Blog.User").OrderBy(post => post.DatePosted).Where(p => p.Published && p.BlogId != Constants.SERVERBLOGID).Take(10); var foundPosts = db.Posts.Include("Blog").Include("Blog.User").OrderBy(post => post.DatePosted).Where(p => p.Published && !p.System).Take(10);
if (foundPosts != null) if (foundPosts != null)
lastPosts = foundPosts.ToList(); lastPosts = foundPosts.ToList();
// Grab the latest podcasts // Grab the latest podcasts

View File

@ -52,7 +52,7 @@ namespace Teknik.Areas.Profile.Controllers
model.Quote = user.Quote; model.Quote = user.Quote;
// fill in Blog details // fill in Blog details
var blog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID); var blog = db.Blogs.Where(b => b.UserId == user.UserId);
if (blog != null && blog.Any()) if (blog != null && blog.Any())
{ {
Blog.Models.Blog foundBlog = blog.First(); Blog.Models.Blog foundBlog = blog.First();
@ -169,7 +169,7 @@ namespace Teknik.Areas.Profile.Controllers
User user = db.Users.Where(u => u.Username == User.Identity.Name).First(); User user = db.Users.Where(u => u.Username == User.Identity.Name).First();
if (user != null) if (user != null)
{ {
var foundBlog = db.Blogs.Where(b => b.UserId == user.UserId && b.BlogId != Constants.SERVERBLOGID); var foundBlog = db.Blogs.Where(b => b.UserId == user.UserId);
if (foundBlog != null && foundBlog.Any()) if (foundBlog != null && foundBlog.Any())
{ {
Blog.Models.Blog blog = foundBlog.First(); Blog.Models.Blog blog = foundBlog.First();

View File

@ -11,10 +11,10 @@
<div class="container"> <div class="container">
@if (!Model.Error) @if (!Model.Error)
{ {
bool OwnProfile = (Model.Username == User.Identity.Name); bool OwnProfile = (Model.Username == User.Identity.Name || User.IsInRole("Admin"));
<div class="row"> <div class="row">
<div class="col-sm-3@((string.IsNullOrEmpty(Model.About) && !OwnProfile) ? " col-sm-offset-4" : string.Empty)"><h1>@Model.Username></h1></div> <div class="col-sm-3@((string.IsNullOrEmpty(Model.About) && !OwnProfile) ? " col-sm-offset-4" : string.Empty)"><h1>@Model.Username</h1></div>
</div> </div>
<div class="row"> <div class="row">
<div class="col-sm-3@((string.IsNullOrEmpty(Model.About) && !OwnProfile) ? " col-sm-offset-4" : string.Empty)"><!--left col--> <div class="col-sm-3@((string.IsNullOrEmpty(Model.About) && !OwnProfile) ? " col-sm-offset-4" : string.Empty)"><!--left col-->
@ -26,10 +26,7 @@
<li class="list-group-item text-right"><span class="pull-left"><strong>Last Seen</strong></span> @Model.LastSeen.ToString("MMMM dd, yyyy")</li> <li class="list-group-item text-right"><span class="pull-left"><strong>Last Seen</strong></span> @Model.LastSeen.ToString("MMMM dd, yyyy")</li>
} }
<li class="list-group-item text-right"><span class="pull-left"><strong>Email</strong></span> <a href="mailto:@Model.Email">@Model.Email</a></li> <li class="list-group-item text-right"><span class="pull-left"><strong>Email</strong></span> <a href="mailto:@Model.Email">@Model.Email</a></li>
@if (!string.IsNullOrEmpty(Model.BlogTitle)) <li class="list-group-item text-right"><span class="pull-left"><strong>Blog</strong></span> <a href="@Url.SubRouteUrl("blog", "Blog.Blog", new { username = Model.Username })" id="blog_title">@(string.IsNullOrEmpty(Model.BlogTitle) ? string.Format("{0}'s Blog", Model.Username) : Model.BlogTitle)</a></li>
{
<li class="list-group-item text-right"><span class="pull-left"><strong>Blog</strong></span> <a href="@Url.SubRouteUrl("blog", "Blog.Blog", new { username = Model.Username })" id="blog_title">@Model.BlogTitle</a></li>
}
<li class="list-group-item text-right"><span class="pull-left"><strong>Git</strong></span> <a href="@Url.SubRouteUrl("git", "Git.Index", new { username = Model.Username })">Public Repos</a></li> <li class="list-group-item text-right"><span class="pull-left"><strong>Git</strong></span> <a href="@Url.SubRouteUrl("git", "Git.Index", new { username = Model.Username })">Public Repos</a></li>
@if (OwnProfile) @if (OwnProfile)
{ {

View File

@ -81,19 +81,33 @@ namespace Teknik.Configuration
public static string Serialize(Config config) public static string Serialize(Config config)
{ {
return JsonConvert.SerializeObject(config); return JsonConvert.SerializeObject(config, Formatting.Indented);
} }
public static Config Load() public static Config Load()
{ {
Config config = new Config(); Config config = new Config();
string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString(); string path = AppDomain.CurrentDomain.GetData("DataDirectory").ToString();
if (File.Exists(Path.Combine(path, "Config.json"))) if (!File.Exists(Path.Combine(path, "Config.json")))
{
Config.Save(Path.Combine(path, "Config.json"), config);
}
else
{ {
string configContents = File.ReadAllText(Path.Combine(path, "Config.json")); string configContents = File.ReadAllText(Path.Combine(path, "Config.json"));
config = Config.Deserialize(configContents); config = Config.Deserialize(configContents);
} }
return config; return config;
} }
public static void Save(string path, Config config)
{
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
string configContents = Config.Serialize(config);
File.WriteAllText(path, configContents);
}
} }
} }