Skip to content

Commit 538617e

Browse files
committed
Added DraftBlogPost Page
1 parent c5cbc5d commit 538617e

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading.Tasks;
2+
using Bunit;
3+
using FluentAssertions;
4+
using LinkDotNet.Blog.TestUtilities;
5+
using LinkDotNet.Blog.Web.Pages.Admin;
6+
using LinkDotNet.Blog.Web.Shared;
7+
using LinkDotNet.Infrastructure.Persistence;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Xunit;
10+
11+
namespace LinkDotNet.Blog.IntegrationTests.Web.Pages.Admin
12+
{
13+
public class DraftBlogPostPageTests : SqlDatabaseTestBase
14+
{
15+
[Fact]
16+
public async Task ShouldOnlyShowPublishedPosts()
17+
{
18+
var publishedPost = new BlogPostBuilder().WithTitle("Published").IsPublished().Build();
19+
var unpublishedPost = new BlogPostBuilder().WithTitle("Not published").IsPublished(false).Build();
20+
await BlogPostRepository.StoreAsync(publishedPost);
21+
await BlogPostRepository.StoreAsync(unpublishedPost);
22+
using var ctx = new TestContext();
23+
ctx.JSInterop.Mode = JSRuntimeMode.Loose;
24+
ctx.Services.AddScoped<IRepository>(_ => BlogPostRepository);
25+
var cut = ctx.RenderComponent<DraftBlogPosts>();
26+
27+
var blogPosts = cut.FindComponents<ShortBlogPost>();
28+
29+
blogPosts.Should().HaveCount(1);
30+
blogPosts[0].Find(".description h1").InnerHtml.Should().Be("Not published");
31+
}
32+
}
33+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
@page "/draft"
2+
@using LinkDotNet.Infrastructure.Persistence
3+
@using LinkDotNet.Domain
4+
@attribute [Authorize]
5+
@inject IRepository _repository
6+
<h3>Draft Blog Posts</h3>
7+
8+
<div class="content px-4">
9+
@for (var i = 0; i < _blogPosts.Count; i++)
10+
{
11+
<ShortBlogPost BlogPost="_blogPosts[i]" UseAlternativeStyle="@(i % 2 != 0)"></ShortBlogPost>
12+
}
13+
</div>
14+
15+
@code {
16+
private IList<BlogPost> _blogPosts = new List<BlogPost>();
17+
18+
protected override async Task OnInitializedAsync()
19+
{
20+
_blogPosts = (await _repository.GetAllAsync(p => !p.IsPublished, b => b.UpdatedDate)).ToList();
21+
}
22+
23+
}

LinkDotNet.Blog.Web/Shared/AccessControl.razor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<AuthorizeView>
22
<Authorized>
33
<a href="create">Create</a>
4+
<a href="draft">Draft Blog Posts</a>
45
<a href="logout">Log out</a>
56
</Authorized>
67
<NotAuthorized>

LinkDotNet.Infrastructure/Persistence/Sql/BlogPostRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task<BlogPost> GetByIdAsync(string blogPostId)
2424

2525
public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
2626
{
27-
var blogPosts = blogPostContext.BlogPosts.Include(b => b.Tags).AsQueryable();
27+
var blogPosts = blogPostContext.BlogPosts.AsNoTracking().Include(b => b.Tags).AsQueryable();
2828

2929
if (filter != null)
3030
{

0 commit comments

Comments
 (0)