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
BundleTable.Bundles.Add(new ScriptBundle("~/bundles/blog").Include(
"~/Scripts/ocupload/1.1.2/ocupload.js",
"~/Scripts/PageDown/Markdown.Converter.js",
"~/Scripts/PageDown/Markdown.Sanitizer.js",
"~/Scripts/bootstrap/markdown/bootstrap-markdown.js",
"~/Scripts/bootbox/bootbox.min.js",
"~/Areas/Blog/Scripts/Blog.js"));

View File

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

View File

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

View File

@ -41,7 +41,7 @@
</p>
</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="col-sm-12 text-center">

View File

@ -28,7 +28,7 @@
<div class="container">
@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-dialog">
@ -65,7 +65,7 @@
}
<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>
</ol>

View File

@ -22,12 +22,12 @@ namespace Teknik.Areas.Home.Controllers
HomeViewModel model = new HomeViewModel();
// Grab the latest site blog posts
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)
lastSite = foundSite.ToList();
// Grab the latest user blog posts
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)
lastPosts = foundPosts.ToList();
// Grab the latest podcasts

View File

@ -52,7 +52,7 @@ namespace Teknik.Areas.Profile.Controllers
model.Quote = user.Quote;
// 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())
{
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();
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())
{
Blog.Models.Blog blog = foundBlog.First();

View File

@ -11,10 +11,10 @@
<div class="container">
@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="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 class="row">
<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>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">@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">@(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>Git</strong></span> <a href="@Url.SubRouteUrl("git", "Git.Index", new { username = Model.Username })">Public Repos</a></li>
@if (OwnProfile)
{

View File

@ -81,19 +81,33 @@ namespace Teknik.Configuration
public static string Serialize(Config config)
{
return JsonConvert.SerializeObject(config);
return JsonConvert.SerializeObject(config, Formatting.Indented);
}
public static Config Load()
{
Config config = new Config();
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"));
config = Config.Deserialize(configContents);
}
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);
}
}
}