Skip to content

Commit 10c123e

Browse files
committed
Fixed #59 - Visit counter works with every repo now
1 parent 3162255 commit 10c123e

File tree

2 files changed

+42
-22
lines changed

2 files changed

+42
-22
lines changed

src/LinkDotNet.Blog.Web/Features/Admin/Dashboard/Components/VisitCountPerPage.razor

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
@using LinkDotNet.Blog.Infrastructure.Persistence
55
@using LinkDotNet.Blog.Infrastructure.Persistence.Sql
66
@using LinkDotNet.Blog.Web.Features.Admin.Dashboard.Services
7-
@inject BlogDbContext BlogDbContext
7+
@using X.PagedList
8+
@inject IRepository<BlogPost> BlogPostRepository
9+
@inject IRepository<UserRecord> UserRecordRepository
810

911
<div class="card">
1012
<div class="card-header">Blog Post Visit Counts</div>
@@ -54,32 +56,35 @@
5456

5557
protected override async Task OnInitializedAsync()
5658
{
57-
await LoadBlogPostInformationAsync();
59+
visitData = await LoadBlogPostInformationAsync();
5860
}
5961

60-
private async Task LoadBlogPostInformationAsync()
62+
private async Task<List<VisitCountPageData>> LoadBlogPostInformationAsync()
6163
{
62-
var query = from ur in BlogDbContext.UserRecords
63-
where (!filter.StartDate.HasValue || ur.DateTimeUtcClicked >= filter.StartDate)
64-
&& (!filter.EndDate.HasValue || ur.DateTimeUtcClicked <= filter.EndDate)
65-
join bp in BlogDbContext.BlogPosts
64+
var blogPosts = (await BlogPostRepository.GetAllAsync()).ToList();
65+
var userRecords = (await UserRecordRepository.GetAllAsync(
66+
ur => (!filter.StartDate.HasValue || ur.DateTimeUtcClicked >= filter.StartDate)
67+
&& (!filter.EndDate.HasValue || ur.DateTimeUtcClicked <= filter.EndDate)))
68+
.ToList();
69+
70+
var visitCountPage = (from ur in userRecords
71+
join bp in blogPosts
6672
on ur.UrlClicked.Replace("blogPost/", string.Empty) equals bp.Id
6773
group new { ur, bp } by new { ur.UrlClicked } into gp
6874
orderby gp.Count() descending
6975
select new VisitCountPageData
7076
{
71-
Id = gp.FirstOrDefault().bp.Id,
72-
Title = gp.FirstOrDefault().bp.Title,
73-
Likes = gp.FirstOrDefault().bp.Likes,
77+
Id = gp.First().bp.Id,
78+
Title = gp.First().bp.Title,
79+
Likes = gp.First().bp.Likes,
7480
ClickCount = gp.Count()
75-
};
76-
visitData = await query.ToListAsync();
81+
}).ToList();
82+
return visitCountPage;
7783
}
7884

7985
private async Task RefreshVisitCount(Filter newBeginning)
8086
{
8187
filter = newBeginning;
82-
visitData = null;
83-
await LoadBlogPostInformationAsync();
88+
visitData = await LoadBlogPostInformationAsync();
8489
}
8590
}

tests/LinkDotNet.Blog.IntegrationTests/Web/Features/Admin/Dashboard/Components/VisitCountPerPageTests.cs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
using System.Threading.Tasks;
44
using AngleSharp.Html.Dom;
55
using AngleSharpWrappers;
6-
using Bunit;
76
using LinkDotNet.Blog.Domain;
7+
using LinkDotNet.Blog.Infrastructure.Persistence;
8+
using LinkDotNet.Blog.Infrastructure.Persistence.Sql;
89
using LinkDotNet.Blog.TestUtilities;
910
using LinkDotNet.Blog.Web.Features.Admin.Dashboard.Components;
11+
using LinkDotNet.Blog.Web.Features.Admin.Dashboard.Services;
12+
using Microsoft.AspNetCore.Components;
1013
using Microsoft.Extensions.DependencyInjection;
1114

1215
namespace LinkDotNet.Blog.IntegrationTests.Web.Features.Admin.Dashboard.Components;
@@ -19,7 +22,8 @@ public async Task ShouldShowCounts()
1922
var blogPost = new BlogPostBuilder().WithTitle("I was clicked").WithLikes(2).Build();
2023
await Repository.StoreAsync(blogPost);
2124
using var ctx = new TestContext();
22-
ctx.Services.AddScoped(_ => DbContext);
25+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => new Repository<BlogPost>(DbContext));
26+
ctx.Services.AddScoped<IRepository<UserRecord>>(_ => new Repository<UserRecord>(DbContext));
2327
await SaveBlogPostArticleClicked(blogPost.Id, 10);
2428

2529
var cut = ctx.RenderComponent<VisitCountPerPage>();
@@ -53,20 +57,22 @@ public async Task ShouldFilterByDate()
5357
await DbContext.UserRecords.AddRangeAsync(new[] { clicked1, clicked2, clicked3, clicked4 });
5458
await DbContext.SaveChangesAsync();
5559
using var ctx = new TestContext();
56-
ctx.Services.AddScoped(_ => DbContext);
60+
ctx.ComponentFactories.Add<DateRangeSelector, FilterStubComponent>();
61+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => new Repository<BlogPost>(DbContext));
62+
ctx.Services.AddScoped<IRepository<UserRecord>>(_ => new Repository<UserRecord>(DbContext));
5763
var cut = ctx.RenderComponent<VisitCountPerPage>();
64+
var filter = new Filter { StartDate = new DateTime(2019, 1, 1), EndDate = new DateTime(2020, 12, 31) };
5865

59-
cut.FindComponent<DateRangeSelector>().Find("#startDate").Change(new DateTime(2019, 1, 1));
60-
cut.FindComponent<DateRangeSelector>().Find("#endDate").Change(new DateTime(2020, 12, 31));
66+
await cut.InvokeAsync(() => cut.FindComponent<FilterStubComponent>().Instance.FireFilterChanged(filter));
6167

62-
cut.WaitForState(() => cut.FindAll("td").Any());
68+
cut.WaitForState(() => cut.FindAll("td").Count == 3);
6369
var elements = cut.FindAll("td").ToList();
6470
elements.Count.Should().Be(3);
6571
var titleData = elements[0].ChildNodes.Single() as IHtmlAnchorElement;
6672
titleData.Should().NotBeNull();
6773
titleData.InnerHtml.Should().Be(blogPost1.Title);
6874
titleData.Href.Should().Contain($"blogPost/{blogPost1.Id}");
69-
elements[1].InnerHtml.Should().Be("1");
75+
cut.WaitForAssertion(() => elements[1].InnerHtml.Should().Be("1"));
7076
}
7177

7278
[Fact]
@@ -87,7 +93,8 @@ public async Task ShouldShowTotalClickCount()
8793
await DbContext.UserRecords.AddRangeAsync(new[] { clicked1, clicked2, clicked3, clicked4 });
8894
await DbContext.SaveChangesAsync();
8995
using var ctx = new TestContext();
90-
ctx.Services.AddScoped(_ => DbContext);
96+
ctx.Services.AddScoped<IRepository<BlogPost>>(_ => new Repository<BlogPost>(DbContext));
97+
ctx.Services.AddScoped<IRepository<UserRecord>>(_ => new Repository<UserRecord>(DbContext));
9198

9299
var cut = ctx.RenderComponent<VisitCountPerPage>();
93100

@@ -109,4 +116,12 @@ private async Task SaveBlogPostArticleClicked(string blogPostId, int count)
109116

110117
await DbContext.SaveChangesAsync();
111118
}
119+
120+
private class FilterStubComponent : ComponentBase
121+
{
122+
[Parameter]
123+
public EventCallback<Filter> FilterChanged { get; set; }
124+
125+
public void FireFilterChanged(Filter filter) => FilterChanged.InvokeAsync(filter);
126+
}
112127
}

0 commit comments

Comments
 (0)