Get latest post and display on homepage

This commit is contained in:
Robert Marshall 2019-04-27 21:39:22 +01:00
parent 9e35d0ab90
commit 15b01412f2
3 changed files with 36 additions and 4 deletions

View file

@ -4,15 +4,24 @@ using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Website.Data;
using Website.ViewModels;
namespace Website.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
private readonly BlogRepository _blogRepo;
public HomeController(BlogRepository blogRepo)
{
return View();
_blogRepo = blogRepo;
}
public async Task<IActionResult> Index()
{
var post = await _blogRepo.GetLatestPostAsync();
return View(post);
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

View file

@ -20,5 +20,14 @@ namespace Website.Data
return new BlogPost(result.First());
}
}
public async Task<BlogPost> GetLatestPostAsync() {
const string query = "SELECT * FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT 1";
using (var connection = _dbProvider.NewConnection()) {
connection.Open();
var result = await connection.QueryAsync<BlogPostState>(query);
return new BlogPost(result.First());
}
}
}
}

View file

@ -1,6 +1,20 @@
@{
@using Website.Models;
@model BlogPost;
@{
ViewData["Title"] = "Home";
}
@section CSS{
h3 a {
color:inherit;
}
}
<p>Hi, I'm Rob. I'm a senior software engineer at <a href="https://www.codecomputerlove.com/">Code Computerlove</a>.</p>
<p>If you wish to get in contact, then get in touch via my <a href="http://uk.linkedin.com/in/robertmarshall/"><img alt="LinkedIn profile" src="https://static.licdn.com/scds/common/u/img/webpromo/btn_liprofile_blue_80x15.png" title=""></a></p>
<hr />
<h2>Latest Blog Post</h2>
<h3><a href="/blog/view/@Model.Url">@Model.Title</a></h3>
@Model.Content
<small>Posted on @Model.Timestamp.ToString()</small>
<p><a href="/blog/view/@Model.Url>">Read more...</a></p>