Skip to content

Commit

Permalink
#167 Added CurrencyCreateCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonGeering committed Apr 7, 2021
1 parent df5d869 commit be1202d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
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());
}
}
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
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);
}
}
}
}

0 comments on commit be1202d

Please sign in to comment.