-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#124 Implemented GetDocuments on DocumentController API
- Loading branch information
1 parent
54631fa
commit 6eb75c6
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/AdminAssistant.Blazor/Server/WebAPI/v1/Documents/DocumentController.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,36 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using AdminAssistant.DomainModel.Modules.DocumentsModule.CQRS; | ||
using AdminAssistant.Framework.Providers; | ||
using AutoMapper; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Swashbuckle.AspNetCore.Annotations; | ||
|
||
namespace AdminAssistant.WebAPI.v1 | ||
{ | ||
[ApiController] | ||
[Route("api/v1/core/[controller]")] | ||
[ApiExplorerSettings(GroupName = "Core - Currency")] | ||
public class DocumentController : WebAPIControllerBase | ||
{ | ||
public DocumentController(IMapper mapper, IMediator mediator, ILoggingProvider loggingProvider) | ||
: base(mapper, mediator, loggingProvider) | ||
{ | ||
} | ||
|
||
[HttpGet] | ||
[SwaggerOperation("Lists all documents.", OperationId = "GetDocument")] | ||
[SwaggerResponse(StatusCodes.Status200OK, "Ok - returns a list of DocumentResponseDto", type: typeof(IEnumerable<DocumentResponseDto>))] | ||
public async Task<ActionResult<IEnumerable<DocumentResponseDto>>> GetDocuments() | ||
{ | ||
Log.Start(); | ||
|
||
var result = await Mediator.Send(new DocumentQuery()).ConfigureAwait(false); | ||
var response = Mapper.Map<IEnumerable<DocumentResponseDto>>(result.Value); | ||
|
||
return Log.Finish(Ok(response)); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/AdminAssistant.Blazor/Server/WebAPI/v1/Documents/DocumentResponseDto.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,11 @@ | ||
using AdminAssistant.DomainModel.Modules.DocumentsModule; | ||
using AdminAssistant.Framework.TypeMapping; | ||
|
||
namespace AdminAssistant.WebAPI.v1 | ||
{ | ||
public class DocumentResponseDto : IMapFrom<Document> | ||
{ | ||
public int DocumentID { get; set; } | ||
public string FileName { get; set; } = string.Empty; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/AdminAssistant.Test/WebAPI/v1/Documents/DocumentController_UnitTest.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,68 @@ | ||
#pragma warning disable CA1707 // Identifiers should not contain underscores | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AdminAssistant.DomainModel; | ||
using AdminAssistant.DomainModel.Modules.AccountsModule; | ||
using AdminAssistant.DomainModel.Modules.DocumentsModule; | ||
using AdminAssistant.DomainModel.Modules.DocumentsModule.CQRS; | ||
using Ardalis.Result; | ||
using AutoMapper; | ||
using FluentAssertions; | ||
using MediatR; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace AdminAssistant.WebAPI.v1.Documents | ||
{ | ||
public class DocumentController_UnitTest_Should | ||
{ | ||
[Fact] | ||
[Trait("Category", "Unit")] | ||
public async Task Return_Status200OK_With_AListOfDocuments_Given_NoArguments() | ||
{ | ||
// Arrange | ||
var documents = new List<Document>() | ||
{ | ||
Factory.Document.WithTestData(10).Build(), | ||
Factory.Document.WithTestData(20).Build() | ||
}; | ||
|
||
var services = new ServiceCollection(); | ||
services.AddMockServerSideLogging(); | ||
services.AddAutoMapper(typeof(MappingProfile)); | ||
|
||
var mockMediator = new Mock<IMediator>(); | ||
mockMediator.Setup(x => x.Send(It.IsAny<DocumentQuery>(), It.IsAny<CancellationToken>())) | ||
.Returns(Task.FromResult(Result<IEnumerable<Document>>.Success(documents))); | ||
|
||
services.AddTransient((sp) => mockMediator.Object); | ||
services.AddTransient<DocumentController>(); | ||
|
||
// Act | ||
var response = await services.BuildServiceProvider().GetRequiredService<DocumentController>().GetDocuments().ConfigureAwait(false); | ||
|
||
// Assert | ||
response.Result.Should().BeOfType<OkObjectResult>(); | ||
response.Value.Should().BeNull(); | ||
|
||
var result = (OkObjectResult)response.Result; | ||
result.Value.Should().BeAssignableTo<IEnumerable<DocumentResponseDto>>(); | ||
|
||
//var value = ((IEnumerable<CurrencyResponseDto>)result.Value).ToArray(); | ||
//value.Should().HaveCount(currencies.Count); | ||
|
||
//var expected = currencies.ToArray(); | ||
//for (int i = 0; i < expected.Length; i++) | ||
//{ | ||
// value[i].CurrencyID.Should().Be(expected[i].CurrencyID); | ||
// value[i].Symbol.Should().Be(expected[i].Symbol); | ||
// value[i].DecimalFormat.Should().Be(expected[i].DecimalFormat); | ||
//} | ||
} | ||
} | ||
} | ||
#pragma warning restore CA1707 // Identifiers should not contain underscores |
28 changes: 28 additions & 0 deletions
28
src/AdminAssistant/DomainModel/Modules/DocumentsModule/Builders/DocumentBuilder.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,28 @@ | ||
namespace AdminAssistant.DomainModel.Modules.DocumentsModule.Builders | ||
{ | ||
public interface IDocumentBuilder | ||
{ | ||
Document Build(); | ||
IDocumentBuilder WithTestData(int documentID = Constants.UnknownRecordID); | ||
IDocumentBuilder WithFileName(string fileName); | ||
} | ||
internal class DocumentBuilder : Document, IDocumentBuilder | ||
{ | ||
public static Document Default(IDocumentBuilder builder) => builder.Build(); | ||
public static Document Default(DocumentBuilder builder) => builder.Build(); | ||
|
||
public Document Build() => this; | ||
|
||
public IDocumentBuilder WithTestData(int documentID = Constants.UnknownRecordID) | ||
{ | ||
DocumentID = documentID; | ||
FileName = "SomRandomFileName.txt"; | ||
return this; | ||
} | ||
public IDocumentBuilder WithFileName(string fileName) | ||
{ | ||
FileName = fileName; | ||
return this; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/AdminAssistant/DomainModel/Modules/DocumentsModule/CQRS/DocumentQuery.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,10 @@ | ||
using System.Collections.Generic; | ||
using Ardalis.Result; | ||
using MediatR; | ||
|
||
namespace AdminAssistant.DomainModel.Modules.DocumentsModule.CQRS | ||
{ | ||
public class DocumentQuery : IRequest<Result<IEnumerable<Document>>> | ||
{ | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/AdminAssistant/DomainModel/Modules/DocumentsModule/Document.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,14 @@ | ||
using AdminAssistant.Infra.DAL; | ||
|
||
namespace AdminAssistant.DomainModel.Modules.DocumentsModule | ||
{ | ||
public class Document : IDatabasePersistable | ||
{ | ||
public const int FileNameMaxLength = 255; | ||
|
||
public int DocumentID { get; set; } | ||
public string FileName { get; set; } = string.Empty; | ||
|
||
public int PrimaryKey => DocumentID; | ||
} | ||
} |