forked from gothinkster/realworld-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#24 added endpoint to get all valid tags
- Loading branch information
Showing
13 changed files
with
211 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using NoSqlMigrator.Infrastructure; | ||
|
||
namespace Conduit.Migrations; | ||
|
||
[Migration(4)] | ||
public class CreateTagData : MigrateBase | ||
{ | ||
private readonly string? _collectionName; | ||
private readonly string? _scopeName; | ||
|
||
public CreateTagData() | ||
{ | ||
_collectionName = _config["Couchbase:TagsCollectionName"]; | ||
_scopeName = _config["Couchbase:ScopeName"]; | ||
} | ||
|
||
public override void Up() | ||
{ | ||
Create.Collection(_collectionName) | ||
.InScope(_scopeName); | ||
|
||
Insert.Into | ||
.Scope(_scopeName) | ||
.Collection(_collectionName) | ||
.Document("tagData", new | ||
{ | ||
tags = new List<string> | ||
{ | ||
"Couchbase", "NoSQL", ".NET", "cruising", "baseball" | ||
} | ||
}); | ||
} | ||
|
||
public override void Down() | ||
{ | ||
Delete.Collection(_collectionName) | ||
.FromScope(_scopeName); | ||
} | ||
} |
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
46 changes: 46 additions & 0 deletions
46
Conduit/Conduit.Tests/Integration/Articles/Handlers/GetTagsHandlerTests.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,46 @@ | ||
using Conduit.Web.Articles.Handlers; | ||
using Conduit.Web.Articles.Services; | ||
using Conduit.Web.Models; | ||
using Couchbase.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Conduit.Tests.Integration.Articles.Handlers; | ||
|
||
[TestFixture] | ||
public class GetTagsHandlerTests : CouchbaseIntegrationTest | ||
{ | ||
private GetTagsHandler _handler; | ||
private IConduitTagsCollectionProvider _tagsCollectionProvider; | ||
|
||
[SetUp] | ||
public async Task SetUp() | ||
{ | ||
await base.Setup(); | ||
|
||
ServiceCollection.AddCouchbaseBucket<IConduitBucketProvider>("ConduitIntegrationTests", b => | ||
{ | ||
b | ||
.AddScope("_default") | ||
.AddCollection<IConduitTagsCollectionProvider>("Tags"); | ||
}); | ||
|
||
_tagsCollectionProvider = ServiceProvider.GetRequiredService<IConduitTagsCollectionProvider>(); | ||
|
||
// setup handler and dependencies | ||
_handler = new GetTagsHandler(new TagsDataService(_tagsCollectionProvider)); | ||
} | ||
|
||
[Test] | ||
public async Task Handle_ReturnsGetTagsResult() | ||
{ | ||
// Arrange | ||
var request = new GetTagsRequest(); | ||
|
||
// Act | ||
var result = await _handler.Handle(request, CancellationToken.None); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.That(result.Tags.Contains("cruising"), Is.True); | ||
} | ||
} |
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,32 @@ | ||
using Conduit.Web.Articles.Handlers; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Conduit.Web.Articles.Controllers; | ||
|
||
public class TagController : Controller | ||
{ | ||
private readonly IMediator _mediator; | ||
|
||
public TagController(IMediator mediator) | ||
{ | ||
_mediator = mediator; | ||
} | ||
|
||
/// <summary> | ||
/// List all (allowed) tags | ||
/// </summary> | ||
/// <remarks> | ||
/// <a href="https://realworld-docs.netlify.app/docs/specs/backend-specs/endpoints/#get-tags">Conduit Spec for get tags endpoint</a> | ||
/// </remarks> | ||
/// <returns>List of tags</returns> | ||
/// <response code="200">Returns all allowed tags</response> | ||
[Route("/api/tags")] | ||
[HttpGet] | ||
public async Task<IActionResult> Get() | ||
{ | ||
var tags = await _mediator.Send(new GetTagsRequest()); | ||
|
||
return Ok(tags); | ||
} | ||
} |
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,24 @@ | ||
using Conduit.Web.Articles.Services; | ||
using MediatR; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class GetTagsHandler : IRequestHandler<GetTagsRequest, GetTagsResult> | ||
{ | ||
private readonly ITagsDataService _tagsDataService; | ||
|
||
public GetTagsHandler(ITagsDataService tagsDataService) | ||
{ | ||
_tagsDataService = tagsDataService; | ||
} | ||
|
||
public async Task<GetTagsResult> Handle(GetTagsRequest request, CancellationToken cancellationToken) | ||
{ | ||
var result = await _tagsDataService.GetAllTags(); | ||
|
||
return new GetTagsResult | ||
{ | ||
Tags = result | ||
}; | ||
} | ||
} |
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,13 @@ | ||
using MediatR; | ||
|
||
namespace Conduit.Web.Articles.Handlers; | ||
|
||
public class GetTagsRequest : IRequest<GetTagsResult> | ||
{ | ||
|
||
} | ||
|
||
public class GetTagsResult | ||
{ | ||
public List<string> Tags { get; set; } | ||
} |
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,28 @@ | ||
using Conduit.Web.Models; | ||
|
||
namespace Conduit.Web.Articles.Services; | ||
|
||
public interface ITagsDataService | ||
{ | ||
Task<List<string>> GetAllTags(); | ||
} | ||
|
||
public class TagsDataService : ITagsDataService | ||
{ | ||
private readonly IConduitTagsCollectionProvider _tagsCollectionProvider; | ||
|
||
public TagsDataService(IConduitTagsCollectionProvider tagsCollectionProvider) | ||
{ | ||
_tagsCollectionProvider = tagsCollectionProvider; | ||
} | ||
|
||
public async Task<List<string>> GetAllTags() | ||
{ | ||
var collection = await _tagsCollectionProvider.GetCollectionAsync(); | ||
|
||
var tagsDoc = await collection.GetAsync("tagData"); | ||
var tagData = tagsDoc.ContentAs<TagData>(); | ||
|
||
return tagData.Tags; | ||
} | ||
} |
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,8 @@ | ||
using Couchbase.Extensions.DependencyInjection; | ||
|
||
namespace Conduit.Web.Models; | ||
|
||
public interface IConduitTagsCollectionProvider : INamedCollectionProvider | ||
{ | ||
|
||
} |
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,6 @@ | ||
namespace Conduit.Web.Models; | ||
|
||
public class TagData | ||
{ | ||
public List<string> Tags { get; set; } | ||
} |
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
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