Skip to content

Commit 41ae7cc

Browse files
committed
Added archive
1 parent 5d7c0c6 commit 41ae7cc

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@page "/archive"
2+
@using LinkDotNet.Blog.Infrastructure.Persistence
3+
@using LinkDotNet.Blog.Domain
4+
@inject IRepository<BlogPost> repository
5+
<div class="ps-2">
6+
<h3 class="pb-3">Archive</h3>
7+
8+
@if (blogPostsPerYear == null)
9+
{
10+
<Loading></Loading>
11+
}
12+
else
13+
{
14+
@foreach (var yearGroup in blogPostsPerYear)
15+
{
16+
<h2>@yearGroup.Key</h2>
17+
<ul class="ps-5">
18+
@foreach (var blogPost in yearGroup.OrderByDescending(b => b.UpdatedDate))
19+
{
20+
<li class="pt-1"><a href="/blogPost/@blogPost.Id">@blogPost.Title</a></li>
21+
}
22+
</ul>
23+
}
24+
}
25+
</div >
26+
27+
@code {
28+
private List<IGrouping<int, BlogPost>> blogPostsPerYear;
29+
30+
protected override async Task OnInitializedAsync()
31+
{
32+
blogPostsPerYear = (await repository.GetAllAsync()).GroupBy(r => r.UpdatedDate.Year).ToList();
33+
}
34+
35+
}

src/LinkDotNet.Blog.Web/Features/Home/Components/NavMenu.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<div class="collapse navbar-collapse" id="navbarSupportedContent">
2323
<ul class="navbar-nav ms-auto mb-2 mb-lg-0 me-5">
2424
<li class="nav-item active"><a class="nav-link" href="/">Home</a></li>
25+
<li class="nav-item active"><a class="nav-link" href="/archive">Archive</a></li>
2526
@if (configuration.HasLinkedinAccount)
2627
{
2728
<li class="nav-item">
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Linq;
3+
using System.Linq.Expressions;
4+
using System.Threading.Tasks;
5+
using Bunit;
6+
using LinkDotNet.Blog.Domain;
7+
using LinkDotNet.Blog.Infrastructure.Persistence;
8+
using LinkDotNet.Blog.TestUtilities;
9+
using LinkDotNet.Blog.Web.Features.Archive;
10+
using LinkDotNet.Blog.Web.Features.Components;
11+
using Microsoft.Extensions.DependencyInjection;
12+
using X.PagedList;
13+
14+
namespace LinkDotNet.Blog.UnitTests.Web.Features.Archive;
15+
16+
public class ArchiveTests : TestContext
17+
{
18+
[Fact]
19+
public void ShouldDisplayAllBlogPosts()
20+
{
21+
var repository = new Mock<IRepository<BlogPost>>();
22+
Services.AddScoped(_ => repository.Object);
23+
var allBlogPosts = new[]
24+
{
25+
CreateBlogPost(new DateTime(2021, 1, 1), "Blog Post 1"),
26+
CreateBlogPost(new DateTime(2021, 2, 1), "Blog Post 2"),
27+
CreateBlogPost(new DateTime(2022, 1, 1), "Blog Post 3"),
28+
}.OrderByDescending(a => a.UpdatedDate);
29+
repository.Setup(r => r.GetAllAsync(
30+
It.IsAny<Expression<Func<BlogPost, bool>>>(),
31+
It.IsAny<Expression<Func<BlogPost, object>>>(),
32+
It.IsAny<bool>(),
33+
It.IsAny<int>(),
34+
It.IsAny<int>()))
35+
.ReturnsAsync(new PagedList<BlogPost>(allBlogPosts, 1, 10));
36+
37+
var cut = RenderComponent<ArchivePage>();
38+
39+
cut.WaitForElements("h2");
40+
var yearHeader = cut.FindAll("h2");
41+
yearHeader.Should().HaveCount(2);
42+
yearHeader[0].TextContent.Should().Be("2022");
43+
yearHeader[1].TextContent.Should().Be("2021");
44+
var entries = cut.FindAll("li");
45+
entries.Should().HaveCount(3);
46+
entries[0].TextContent.Should().Be("Blog Post 3");
47+
entries[1].TextContent.Should().Be("Blog Post 2");
48+
entries[2].TextContent.Should().Be("Blog Post 1");
49+
}
50+
51+
[Fact]
52+
public void ShouldShowLoading()
53+
{
54+
var repository = new Mock<IRepository<BlogPost>>();
55+
Services.AddScoped(_ => repository.Object);
56+
repository.Setup(r => r.GetAllAsync(
57+
It.IsAny<Expression<Func<BlogPost, bool>>>(),
58+
It.IsAny<Expression<Func<BlogPost, object>>>(),
59+
It.IsAny<bool>(),
60+
It.IsAny<int>(),
61+
It.IsAny<int>()))
62+
.Returns(async () =>
63+
{
64+
await Task.Delay(250);
65+
return new PagedList<BlogPost>(Array.Empty<BlogPost>(), 1, 1);
66+
});
67+
68+
var cut = RenderComponent<ArchivePage>();
69+
70+
cut.FindComponents<Loading>().Count.Should().Be(1);
71+
}
72+
73+
private static BlogPost CreateBlogPost(DateTime date, string title)
74+
{
75+
return new BlogPostBuilder()
76+
.WithTitle(title)
77+
.WithUpdatedDate(date)
78+
.Build();
79+
}
80+
}

0 commit comments

Comments
 (0)