Skip to content

Commit f4ceaea

Browse files
committed
Use generic repository pattern and include tag search page
1 parent 96f4b59 commit f4ceaea

File tree

7 files changed

+94
-12
lines changed

7 files changed

+94
-12
lines changed

LinkDotNet.Blog.Web/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
IList<BlogPost> _blogPosts = new List<BlogPost>();
1818
protected override async Task OnInitializedAsync()
1919
{
20-
_blogPosts = (await _repository.GetAllAsync()).OrderByDescending(o => o.UpdatedDate).ToList();
20+
_blogPosts = (await _repository.GetAllAsync(orderBy: b => b.UpdatedDate)).ToList();
2121
}
2222

2323
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@page "/searchByTag/{tag}"
2+
@using LinkDotNet.Domain
3+
@using LinkDotNet.Infrastructure.Persistence
4+
@inject IRepository _repository
5+
6+
<h3>All posts with Tag <em>@Tag</em></h3>
7+
8+
@for (var i = 0; i < _blogPosts.Count; i++)
9+
{
10+
<ShortBlogPost BlogPost="_blogPosts[i]" UseAlternativeStyle="@(i % 2 != 0)"></ShortBlogPost>
11+
}
12+
13+
@code {
14+
[Parameter]
15+
public string Tag { get; set; }
16+
17+
IList<BlogPost> _blogPosts = new List<BlogPost>();
18+
protected override async Task OnInitializedAsync()
19+
{
20+
_blogPosts = (await _repository.GetAllAsync(b => b.Tags.Any(t => t.Content == Tag), b => b.UpdatedDate)).ToList();
21+
}
22+
}

LinkDotNet.Blog.Web/Shared/ShortBlogPost.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<ul>
1313
@foreach (var tag in BlogPost.Tags.Select(t => t.Content))
1414
{
15-
<li><a href="#">@tag</a></li>
15+
<li><a href="/searchByTag/@tag">@tag</a></li>
1616
}
1717
</ul>
1818
</li>

LinkDotNet.Infrastructure/Persistence/IRepository.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
24
using System.Threading.Tasks;
35
using LinkDotNet.Domain;
46

@@ -8,7 +10,7 @@ public interface IRepository
810
{
911
Task<BlogPost> GetByIdAsync(string blogPostId);
1012

11-
Task<IEnumerable<BlogPost>> GetAllAsync();
13+
Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true);
1214

1315
Task StoreAsync(BlogPost blogPost);
1416
}

LinkDotNet.Infrastructure/Persistence/InMemory/InMemoryRepository.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Linq.Expressions;
35
using System.Threading.Tasks;
46
using LinkDotNet.Domain;
57

@@ -15,8 +17,24 @@ public Task<BlogPost> GetByIdAsync(string blogPostId)
1517
return Task.FromResult(blogPost);
1618
}
1719

18-
public Task<IEnumerable<BlogPost>> GetAllAsync()
20+
public Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
1921
{
22+
var result = blogPosts.AsEnumerable();
23+
if (filter != null)
24+
{
25+
result = result.Where(filter.Compile());
26+
}
27+
28+
if (orderBy != null)
29+
{
30+
if (descending)
31+
{
32+
return Task.FromResult(result.OrderByDescending(orderBy.Compile()).AsEnumerable());
33+
}
34+
35+
return Task.FromResult(result.OrderBy(orderBy.Compile()).AsEnumerable());
36+
}
37+
2038
return Task.FromResult(blogPosts.AsEnumerable());
2139
}
2240

LinkDotNet.Infrastructure/Persistence/RavenDb/BlogPostRepository.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Linq.Expressions;
24
using System.Threading.Tasks;
35
using LinkDotNet.Domain;
46
using Raven.Client.Documents;
7+
using Raven.Client.Documents.Linq;
58

69
namespace LinkDotNet.Infrastructure.Persistence.RavenDb
710
{
@@ -20,10 +23,27 @@ public async Task<BlogPost> GetByIdAsync(string blogPostId)
2023
return await session.LoadAsync<BlogPost>(blogPostId);
2124
}
2225

23-
public async Task<IEnumerable<BlogPost>> GetAllAsync()
26+
public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
2427
{
2528
using var session = documentStore.OpenAsyncSession();
26-
return await session.Query<BlogPost>().ToListAsync();
29+
30+
var query = session.Query<BlogPost>();
31+
if (filter != null)
32+
{
33+
query = query.Where(filter);
34+
}
35+
36+
if (orderBy != null)
37+
{
38+
if (descending)
39+
{
40+
return await query.OrderByDescending(orderBy).ToListAsync();
41+
}
42+
43+
return await query.OrderBy(orderBy).ToListAsync();
44+
}
45+
46+
return await query.ToListAsync();
2747
}
2848

2949
public async Task StoreAsync(BlogPost blogPost)

LinkDotNet.Infrastructure/Persistence/Sql/BlogPostRepository.cs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
25
using System.Threading.Tasks;
36
using LinkDotNet.Domain;
47
using Microsoft.EntityFrameworkCore;
@@ -16,12 +19,29 @@ public BlogPostRepository(BlogPostContext blogPostContext)
1619

1720
public async Task<BlogPost> GetByIdAsync(string blogPostId)
1821
{
19-
return await blogPostContext.BlogPosts.SingleAsync(b => b.Id == blogPostId);
22+
return await blogPostContext.BlogPosts.Include(b => b.Tags).SingleAsync(b => b.Id == blogPostId);
2023
}
2124

22-
public async Task<IEnumerable<BlogPost>> GetAllAsync()
25+
public async Task<IEnumerable<BlogPost>> GetAllAsync(Expression<Func<BlogPost, bool>> filter = null, Expression<Func<BlogPost, object>> orderBy = null, bool descending = true)
2326
{
24-
return await blogPostContext.BlogPosts.ToListAsync();
27+
var blogPosts = blogPostContext.BlogPosts.Include(b => b.Tags).AsQueryable();
28+
29+
if (filter != null)
30+
{
31+
blogPosts = blogPosts.Where(filter);
32+
}
33+
34+
if (orderBy != null)
35+
{
36+
if (descending)
37+
{
38+
return await blogPosts.OrderByDescending(orderBy).ToListAsync();
39+
}
40+
41+
return await blogPosts.OrderBy(orderBy).ToListAsync();
42+
}
43+
44+
return await blogPosts.ToListAsync();
2545
}
2646

2747
public async Task StoreAsync(BlogPost blogPost)

0 commit comments

Comments
 (0)