From 9fd9beb860c6f7c8a2b02384494935284d63e2c2 Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Fri, 3 Jan 2020 16:44:40 +0000 Subject: [PATCH] Add blog navigation --- Website/Data/BlogRepository.cs | 4 ++-- .../BlogNavigationViewComponent.cs | 18 ++++++++++++++++++ .../Components/BlogNavigation/Default.cshtml | 9 +++++++++ Website/Views/Shared/_Layout.cshtml | 9 +-------- Website/wwwroot/css/style.less | 14 ++++++++++++-- 5 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 Website/ViewComponents/BlogNavigationViewComponent.cs create mode 100644 Website/Views/Shared/Components/BlogNavigation/Default.cshtml diff --git a/Website/Data/BlogRepository.cs b/Website/Data/BlogRepository.cs index 20f1e16..2d23650 100644 --- a/Website/Data/BlogRepository.cs +++ b/Website/Data/BlogRepository.cs @@ -22,7 +22,7 @@ namespace Website.Data } } - public async Task> GetLatestPostsAsync(int limit, int offset) { + public async Task> GetLatestPostsAsync(int limit, int offset = 0) { const string query = "SELECT * FROM blog_posts WHERE post_content<>'' AND post_deleted=0 ORDER BY post_timestamp DESC LIMIT @offset,@limit"; using (var connection = _dbProvider.NewConnection()) { connection.Open(); @@ -31,7 +31,7 @@ namespace Website.Data } } - public async Task GetLatestPostAsync() => (await GetLatestPostsAsync(1, 0)).First(); + public async Task GetLatestPostAsync() => (await GetLatestPostsAsync(1)).First(); public async Task GetCountAsync() { diff --git a/Website/ViewComponents/BlogNavigationViewComponent.cs b/Website/ViewComponents/BlogNavigationViewComponent.cs new file mode 100644 index 0000000..fcec7c2 --- /dev/null +++ b/Website/ViewComponents/BlogNavigationViewComponent.cs @@ -0,0 +1,18 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Website.Data; + +namespace Website.ViewComponents { + public class BlogNavigationViewComponent:ViewComponent { + private readonly BlogRepository _repo; + + public BlogNavigationViewComponent(BlogRepository repo) { + _repo = repo; + } + + public async Task InvokeAsync() { + var posts = await _repo.GetLatestPostsAsync(5); + return View(posts); + } + } +} \ No newline at end of file diff --git a/Website/Views/Shared/Components/BlogNavigation/Default.cshtml b/Website/Views/Shared/Components/BlogNavigation/Default.cshtml new file mode 100644 index 0000000..6aafada --- /dev/null +++ b/Website/Views/Shared/Components/BlogNavigation/Default.cshtml @@ -0,0 +1,9 @@ +@model System.Collections.Generic.IEnumerable + +
Blog:
+ \ No newline at end of file diff --git a/Website/Views/Shared/_Layout.cshtml b/Website/Views/Shared/_Layout.cshtml index 16f3f19..f5e83b1 100644 --- a/Website/Views/Shared/_Layout.cshtml +++ b/Website/Views/Shared/_Layout.cshtml @@ -23,7 +23,6 @@ @RenderBody()