-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d093fd
commit d83a3de
Showing
6 changed files
with
81 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
namespace Blogger.Application.Articles.GetTags; | ||
public record GetTagsQuery() | ||
: IRequest<IReadOnlyList<GetTagsQueryResponse>>; | ||
: IRequest<IReadOnlyCollection<GetTagsQueryResponse>>; |
8 changes: 3 additions & 5 deletions
8
src/Blogger.Application/Articles/GetTags/GetTagsQueryHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,14 @@ | ||
namespace Blogger.Application.Articles.GetTags; | ||
|
||
public class GetTagsQueryHandler(IArticleRepository articleRepository) | ||
: IRequestHandler<GetTagsQuery, IReadOnlyList<GetTagsQueryResponse>> | ||
: IRequestHandler<GetTagsQuery, IReadOnlyCollection<GetTagsQueryResponse>> | ||
{ | ||
private readonly IArticleRepository _articleRepository = articleRepository; | ||
|
||
public async Task<IReadOnlyList<GetTagsQueryResponse>> Handle(GetTagsQuery request, CancellationToken cancellationToken) | ||
public async Task<IReadOnlyCollection<GetTagsQueryResponse>> Handle(GetTagsQuery request, CancellationToken cancellationToken) | ||
{ | ||
var tags = await _articleRepository.GetTagsAsync(cancellationToken); | ||
|
||
return tags.GroupBy(x => x) | ||
.Select(x => new GetTagsQueryResponse(x.Key, x.Count())) | ||
.ToImmutableList(); | ||
return [.. tags.Select(x => new GetTagsQueryResponse(x.Tag, x.Count))]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
namespace Blogger.Domain.ArticleAggregate.Models; | ||
public sealed record TagModel(Tag Tag, int Count); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/Blogger.IntegrationTests/Articles/GetTagsQueryHandlerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System.Collections.Immutable; | ||
using Blogger.Application.Articles.GetTags; | ||
using Blogger.Domain.ArticleAggregate; | ||
using Blogger.Infrastructure.Persistence.Repositories; | ||
using Blogger.IntegrationTests.Fixtures; | ||
|
||
using FluentAssertions; | ||
|
||
namespace Blogger.IntegrationTests.Articles; | ||
public class GetTagsQueryHandlerTests : IClassFixture<BloggerDbContextFixture> | ||
{ | ||
private readonly BloggerDbContextFixture _fixture; | ||
|
||
public GetTagsQueryHandlerTests(BloggerDbContextFixture fixture) | ||
{ | ||
_fixture = fixture; | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldReturnTags_WhenTagsExist() | ||
{ | ||
// Arrange | ||
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); | ||
var sut = new GetTagsQueryHandler(articleRepository); | ||
|
||
var article_1 = Article.CreateArticle("Title 1", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag3")]); | ||
var article_2 = Article.CreateArticle("Title 2", "Test Body", "Test Summary", [Tag.Create("tag1"), Tag.Create("tag4")]); | ||
|
||
articleRepository.Add(article_1); | ||
articleRepository.Add(article_2); | ||
|
||
await articleRepository.SaveChangesAsync(CancellationToken.None); | ||
|
||
var request = new GetTagsQuery(); | ||
|
||
// Act | ||
var response = (await sut.Handle(request, CancellationToken.None)).ToImmutableList(); | ||
|
||
// Assert | ||
response.Should().NotBeNull(); | ||
response.Should().HaveCount(3); | ||
|
||
response[0].Tag.Value.Should().Be("tag1"); | ||
response[0].Count.Should().Be(2); | ||
|
||
response[1].Tag.Value.Should().Be("tag3"); | ||
response[1].Count.Should().Be(1); | ||
|
||
response[2].Tag.Value.Should().Be("tag4"); | ||
response[2].Count.Should().Be(1); | ||
} | ||
|
||
[Fact] | ||
public async Task Handle_ShouldReturnEmpty_WhenNoTagsExist() | ||
{ | ||
// Arrange | ||
var articleRepository = new ArticleRepository(_fixture.BuildDbContext(Guid.NewGuid().ToString())); | ||
var sut = new GetTagsQueryHandler(articleRepository); | ||
|
||
var request = new GetTagsQuery(); | ||
|
||
// Act | ||
var response = await sut.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
response.Should().NotBeNull(); | ||
response.Should().BeEmpty(); | ||
} | ||
} |