-
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.
- Loading branch information
1 parent
df5d869
commit be1202d
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...sistant.Blazor/Server/WebAPI/v1/CoreModule/CurrencyController.CurrencyCreateRequestDto.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,19 @@ | ||
using AdminAssistant.DomainModel.Modules.CoreModule; | ||
using AdminAssistant.Framework.TypeMapping; | ||
using Swashbuckle.AspNetCore.Annotations; | ||
|
||
namespace AdminAssistant.WebAPI.v1.CoreModule | ||
{ | ||
[SwaggerSchema(Required = new[] { "Symbol", "DecimalFormat" })] | ||
public class CurrencyCreateRequestDto : IMapTo<Currency> | ||
{ | ||
[SwaggerSchema("The Currency identifier.", ReadOnly = true)] | ||
public int CurrencyID { get; set; } | ||
public string Symbol { get; set; } = string.Empty; | ||
public string DecimalFormat { get; set; } = string.Empty; | ||
|
||
public void MapTo(AutoMapper.Profile profile) | ||
=> profile.CreateMap<CurrencyCreateRequestDto, Currency>() | ||
.ForMember(x => x.CurrencyID, opt => opt.Ignore()); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...AdminAssistant.Test/DomainModel/Modules/CoreModule/CQRS/CurrencyCreateCommand_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,72 @@ | ||
#pragma warning disable CA1707 // Identifiers should not contain underscores | ||
using System.Threading.Tasks; | ||
using AdminAssistant.Infra.DAL.Modules.CoreModule; | ||
using Ardalis.Result; | ||
using FluentAssertions; | ||
using MediatR; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Moq; | ||
using Xunit; | ||
using ObjectCloner.Extensions; // https://github.com/marcelltoth/ObjectCloner | ||
|
||
namespace AdminAssistant.DomainModel.Modules.CoreModule.CQRS | ||
{ | ||
public class CurrencyCreateCommand_Should | ||
{ | ||
[Fact] | ||
[Trait("Category", "Unit")] | ||
public async Task Return_APersistedCurrency_GivenAValidCurrency() | ||
{ | ||
// Arrange | ||
var currency = Factory.Currency.WithTestData().Build(); | ||
|
||
var services = new ServiceCollection(); | ||
services.AddMockServerSideLogging(); | ||
services.AddAdminAssistantServerSideDomainModel(); | ||
|
||
var mockCurrencyRepository = new Mock<ICurrencyRepository>(); | ||
mockCurrencyRepository.Setup(x => x.SaveAsync(currency)) | ||
.Returns(() => | ||
{ | ||
var result = currency.DeepClone(); | ||
result = result with { CurrencyID = 30 }; | ||
return Task.FromResult(result); | ||
}); | ||
|
||
services.AddTransient((sp) => mockCurrencyRepository.Object); | ||
|
||
// Act | ||
var result = await services.BuildServiceProvider().GetRequiredService<IMediator>().Send(new CurrencyCreateCommand(currency)).ConfigureAwait(false); | ||
|
||
// Assert | ||
result.Should().NotBeNull(); | ||
result.Status.Should().Be(ResultStatus.Ok); | ||
result.ValidationErrors.Should().BeEmpty(); | ||
result.Value.Should().NotBeNull(); | ||
result.Value.CurrencyID.Should().BeGreaterThan(Constants.NewRecordID); | ||
} | ||
|
||
[Fact] | ||
[Trait("Category", "Unit")] | ||
public async Task Return_ValidationError_GivenAnInvalidBank() | ||
{ | ||
// Arrange | ||
var services = new ServiceCollection(); | ||
services.AddMockServerSideLogging(); | ||
services.AddAdminAssistantServerSideDomainModel(); | ||
services.AddTransient((sp) => new Mock<ICurrencyRepository>().Object); | ||
|
||
var bank = Factory.Currency.WithTestData() | ||
.WithoutASymbol() | ||
.Build(); | ||
// Act | ||
var result = await services.BuildServiceProvider().GetRequiredService<IMediator>().Send(new CurrencyCreateCommand(bank)).ConfigureAwait(false); | ||
|
||
// Assert | ||
result.Should().NotBeNull(); | ||
result.Status.Should().Be(ResultStatus.Invalid); | ||
result.ValidationErrors.Should().NotBeEmpty(); | ||
} | ||
} | ||
} | ||
#pragma warning restore CA1707 // Identifiers should not contain underscores |
47 changes: 47 additions & 0 deletions
47
src/AdminAssistant/DomainModel/Modules/CoreModule/CQRS/CurrencyCreateCommand.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,47 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using AdminAssistant.Infra.DAL.Modules.CoreModule; | ||
using AdminAssistant.DomainModel.Modules.CoreModule.Validation; | ||
using AdminAssistant.Infra.Providers; | ||
using Ardalis.Result; | ||
using MediatR; | ||
using Ardalis.Result.FluentValidation; | ||
|
||
namespace AdminAssistant.DomainModel.Modules.CoreModule.CQRS | ||
{ | ||
public class CurrencyCreateCommand : IRequest<Result<Currency>> | ||
{ | ||
public CurrencyCreateCommand(Currency currency) => Currency = currency; | ||
|
||
public Currency Currency { get; private set; } | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Build", "CA1812", Justification = "Compiler dosen't understand dependency injection")] | ||
internal class CurrencyCreateHandler : RequestHandlerBase<CurrencyCreateCommand, Result<Currency>> | ||
{ | ||
private readonly ICurrencyRepository currencyRepository; | ||
private readonly ICurrencyValidator currencyValidator; | ||
|
||
public CurrencyCreateHandler(ILoggingProvider loggingProvider, ICurrencyRepository currencyRepository, ICurrencyValidator currencyValidator) | ||
: base(loggingProvider) | ||
{ | ||
this.currencyRepository = currencyRepository; | ||
this.currencyValidator = currencyValidator; | ||
} | ||
|
||
public override async Task<Result<Currency>> Handle(CurrencyCreateCommand command, CancellationToken cancellationToken) | ||
{ | ||
var validationResult = await currencyValidator.ValidateAsync(command.Currency, cancellationToken).ConfigureAwait(false); | ||
|
||
if (validationResult.IsValid == false) | ||
{ | ||
return Result<Currency>.Invalid(validationResult.AsErrors()); | ||
} | ||
|
||
var result = await currencyRepository.SaveAsync(command.Currency).ConfigureAwait(false); | ||
return Result<Currency>.Success(result); | ||
} | ||
} | ||
} | ||
} |