Skip to content

Refactored Home controller to get posts from common method (also fixes search) #293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src/Blogifier/Blog.db
Binary file not shown.
195 changes: 107 additions & 88 deletions src/Blogifier/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,73 +37,46 @@ public HomeController(IBlogProvider blogProvider,
_compositeViewEngine = compositeViewEngine;
}

public async Task<IActionResult> Index(string term, int page = 1)
public async Task<IActionResult> Index(int page = 1)
{
var model = new ListModel { PostListType = PostListType.Blog };
try
{
model.Blog = await _blogProvider.GetBlogItem();
}
catch
{
return Redirect("~/admin");
}

model.Pager = new Pager(page, model.Blog.ItemsPerPage);
var model = await getBlogPosts(pager: page);

if (string.IsNullOrEmpty(term))
{
if (model.Blog.IncludeFeatured)
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "FP");
else
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "P");
}
else
{
model.PostListType = PostListType.Search;
model.Blog.Title = term;
model.Blog.Description = "";
model.Posts = await _postProvider.Search(model.Pager, term, 0, "FP");
}
return View($"~/Views/Themes/{model.Blog.Theme}/Index.cshtml", model);
}

if (model.Pager.ShowOlder) model.Pager.LinkToOlder = $"?page={model.Pager.Older}";
if (model.Pager.ShowNewer) model.Pager.LinkToNewer = $"?page={model.Pager.Newer}";
[HttpGet("/{slug}")]
public async Task<IActionResult> Index(string slug)
{
if (!string.IsNullOrEmpty(slug))
{
return await getSingleBlogPost(slug);
}
return Redirect("/");
}

[HttpPost]
public async Task<IActionResult> Search(string term, int page = 1)
{

if (!string.IsNullOrEmpty(term))
{
var model = await getBlogPosts(term, page);
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Search.cshtml";
if (IsViewExists(viewPath))
return View(viewPath, model);
else
return Redirect("~/home");
}
else{
return Redirect("~/home");
}
}

return View($"~/Views/Themes/{model.Blog.Theme}/Index.cshtml", model);
}

[HttpPost]
public IActionResult Search(string term)
{
return Redirect($"/home?term={term}");
}

[HttpGet("categories/{category}")]
[HttpGet("categories/{category}")]
public async Task<IActionResult> Categories(string category, int page = 1)
{
var model = new ListModel { PostListType = PostListType.Category };
try
{
model.Blog = await _blogProvider.GetBlogItem();
}
catch
{
return Redirect("~/admin");
}

model.Pager = new Pager(page, model.Blog.ItemsPerPage);
model.Posts = await _postProvider.GetList(model.Pager, 0, category, "PF");

if (model.Pager.ShowOlder) model.Pager.LinkToOlder = $"?page={model.Pager.Older}";
if (model.Pager.ShowNewer) model.Pager.LinkToNewer = $"?page={model.Pager.Newer}";

var model = await getBlogPosts("", page, category);
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Category.cshtml";

if (IsViewExists(viewPath))
Expand All @@ -112,41 +85,11 @@ public async Task<IActionResult> Categories(string category, int page = 1)
return View($"~/Views/Themes/{model.Blog.Theme}/Index.cshtml", model);
}

[HttpGet("posts/{slug}")]
public async Task<IActionResult> Single(string slug)
{
try
{
ViewBag.Slug = slug;
PostModel model = await _postProvider.GetPostModel(slug);

// If unpublished and unauthorised redirect to error / 404.
if (model.Post.Published == DateTime.MinValue && !User.Identity.IsAuthenticated)
{
return Redirect("~/error");
}

model.Blog = await _blogProvider.GetBlogItem();
model.Post.Description = model.Post.Description.MdToHtml();
model.Post.Content = model.Post.Content.MdToHtml();

if(!model.Post.Author.Avatar.StartsWith("data:"))
model.Post.Author.Avatar = Url.Content($"~/{model.Post.Author.Avatar}");

if(model.Post.PostType == PostType.Page)
{
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Page.cshtml";
if (IsViewExists(viewPath))
return View(viewPath, model);
}

return View($"~/Views/Themes/{model.Blog.Theme}/Post.cshtml", model);
}
catch
{
return Redirect("~/error");
}
}
[HttpGet("posts/{slug}")]
public async Task<IActionResult> Single(string slug)
{
return await getSingleBlogPost(slug);
}

[HttpGet("error")]
public async Task<IActionResult> Error()
Expand Down Expand Up @@ -226,5 +169,81 @@ private bool IsViewExists(string viewPath)
var result = _compositeViewEngine.GetView("", viewPath, false);
return result.Success;
}


public async Task<IActionResult> getSingleBlogPost(string slug){
try
{
ViewBag.Slug = slug;
PostModel model = await _postProvider.GetPostModel(slug);

// If unpublished and unauthorised redirect to error / 404.
if (model.Post.Published == DateTime.MinValue && !User.Identity.IsAuthenticated)
{
return Redirect("~/error");
}

model.Blog = await _blogProvider.GetBlogItem();
model.Post.Description = model.Post.Description.MdToHtml();
model.Post.Content = model.Post.Content.MdToHtml();

if (!model.Post.Author.Avatar.StartsWith("data:"))
model.Post.Author.Avatar = Url.Content($"~/{model.Post.Author.Avatar}");

if (model.Post.PostType == PostType.Page)
{
string viewPath = $"~/Views/Themes/{model.Blog.Theme}/Page.cshtml";
if (IsViewExists(viewPath))
return View(viewPath, model);
}

return View($"~/Views/Themes/{model.Blog.Theme}/Post.cshtml", model);
}
catch
{
return Redirect("~/error");
}
}
public async Task<ListModel> getBlogPosts(string term ="", int pager = 1, string category = "", string slug = ""){

var model = new ListModel{};

try
{
model.Blog = await _blogProvider.GetBlogItem();
}
catch
{
Redirect("~/admin");
}

model.Pager = new Pager(pager, model.Blog.ItemsPerPage);

if(!string.IsNullOrEmpty(category))
{
model.PostListType = PostListType.Category;
model.Posts = await _postProvider.GetList(model.Pager, 0, category, "PF");
}
else if (string.IsNullOrEmpty(term))
{
model.PostListType = PostListType.Blog;
if (model.Blog.IncludeFeatured)
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "FP");
else
model.Posts = await _postProvider.GetList(model.Pager, 0, "", "P");
}
else
{
model.PostListType = PostListType.Search;
model.Blog.Title = term;
model.Blog.Description = "";
model.Posts = await _postProvider.Search(model.Pager, term, 0, "FP");
}

if (model.Pager.ShowOlder) model.Pager.LinkToOlder = $"?page={model.Pager.Older}";
if (model.Pager.ShowNewer) model.Pager.LinkToNewer = $"?page={model.Pager.Newer}";

return model;
}
}
}
2 changes: 1 addition & 1 deletion src/Blogifier/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void ConfigureServices(IServiceCollection services)

services.AddBlogProviders();

services.AddControllersWithViews();
services.AddRazorPages();
services.AddControllersWithViews();

Log.Warning("Done configure services");
}
Expand Down
11 changes: 8 additions & 3 deletions src/Blogifier/Views/Shared/HeaderScript.cshtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Blogifier.Core
@using Blogifier.Core
@{
var request = Url.ActionContext.HttpContext.Request;
var absoluteUrl = $"{request.Scheme}://{request.Host.ToUriComponent()}{request.PathBase.ToUriComponent()}";
Expand All @@ -9,7 +9,12 @@
<title>@postModel.Post.Title &amp; @postModel.Blog.Title</title>
<meta name="author" content="@postModel.Post.Author.DisplayName">
<meta name="description" content="@postModel.Post.Description.StripHtml()">
<link rel="canonical" href="@absoluteUrl/posts/@postModel.Post.Slug" />
if(postModel.Post.PostType == PostType.Page){
<link rel="canonical" href="@absoluteUrl/@postModel.Post.Slug" />
}
else{
<link rel="canonical" href="@absoluteUrl/posts/@postModel.Post.Slug" />
}
}
else
{
Expand All @@ -19,4 +24,4 @@
}
<link rel="alternate" type="application/rss+xml" title="@Model.Blog.Title" href="@absoluteUrl/feed/rss" />
}
@Html.Raw(Model.Blog.HeaderScript)
@Html.Raw(Model.Blog.HeaderScript)
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
PostModel postModel = (PostModel)Model;
var postTitle = postModel.Post.Title;
var postDesc = postModel.Post.Description.StripHtml();
var postUrl = absoluteUrl + "/posts/" + postModel.Post.Slug;
var postUrl = postModel.Post.PostType == PostType.Post ? absoluteUrl + "/posts/" + postModel.Post.Slug : absoluteUrl + "/" + postModel.Post.Slug;
var postCover = absoluteUrl + "/" + postModel.Post.Cover;
var postPublished = postModel.Post.Published.ToString("s");
var postAuthor = postModel.Post.Author.DisplayName;
Expand Down
2 changes: 1 addition & 1 deletion src/Blogifier/Views/Themes/standard/layouts/_main.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
PostModel postModel = (PostModel)Model;
pageTitle = postModel.Post.Title + " | " + siteTitle;
pageDesc = postModel.Post.Description.StripHtml();
pageCanonical = absoluteUrl + "/posts/" + postModel.Post.Slug;
pageCanonical = postModel.Post.PostType == PostType.Post ? absoluteUrl + "/posts/" + postModel.Post.Slug : absoluteUrl + "/" + postModel.Post.Slug;
}
}

Expand Down