Skip to content

Commit

Permalink
commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigojap committed Feb 3, 2021
1 parent 4debb96 commit 8ef14d3
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 53 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using VirtualMind.Application.Extensions;
using VirtualMind.Application.Interfaces;
using VirtuaMind.Infrastructure.FunctionalServices;
using VirtuaMind.Infrastructure.RestServices.Template;

namespace VirtuaMind.Infrastructure.RestServices.ExternalServices
Expand Down
78 changes: 78 additions & 0 deletions VirtualMind.Application/Commands/CreateOperationCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Threading;
using System.Threading.Tasks;
using VirtualMind.Application.Interfaces;
using MediatR;
using VirtualMind.Application.Queries;
using VirtualMind.Application.DTOs;
using System.Collections.Generic;
using VirtualMind.Domain.Entities;
using VirtualMind.Domain.Enums;
using System;
using VirtualMind.Application.Extensions;

namespace VirtualMind.Application.Commands
{
public class CreateOperationCommand : IRequest<int>
{
public int UserId { get; set; }

public decimal RequestedAmount { get; set; }

public string CurrencyType { get; set; }
}

public class CreateOperationCommandHandler : IRequestHandler<CreateOperationCommand, int>
{
private readonly IVirtualMindDbContext VirtualMindDbContext;
private readonly ICurrencyExchangeFactory CurrencyExchangeFactory;

public CreateOperationCommandHandler(IVirtualMindDbContext virtualMindDbContext, ICurrencyExchangeFactory currencyExchangeFactory)
{
VirtualMindDbContext = virtualMindDbContext;
CurrencyExchangeFactory = currencyExchangeFactory;
}

public async Task<int> Handle(CreateOperationCommand request, CancellationToken cancellationToken)
{
var currentQuote = await GetCurrentQuote(request.CurrencyType);
var purchasedAmount = CalcPurchasedAmount(request, currentQuote);





var operation = new Operation
{
Currency = (Currency)Enum.Parse(typeof(Currency), request.CurrencyType),
UserId = request.UserId,
RequestedAmount = request.RequestedAmount,
CurrentQuote = currentQuote.Purchase.ConvertStringToDecimal(),
PurchasedAmount = purchasedAmount
};

await VirtualMindDbContext.Operations.AddAsync(operation);
await VirtualMindDbContext.SaveChangesAsync();

return await Task.FromResult(0);
}


private async Task<ExchangeRateDTO> GetCurrentQuote(string currencyType)
{
var result = await CurrencyExchangeFactory.GetExchangeRate(currencyType);

var currnetQuote = new ExchangeRateDTO
{
Purchase = result[0],
Sale = result[1],
LastUpdate = result[2]
};
return currnetQuote;
}

private static decimal CalcPurchasedAmount(CreateOperationCommand request, ExchangeRateDTO currentQuote)
{
return decimal.Round(request.RequestedAmount / currentQuote.Purchase.ConvertStringToDecimal(), 2);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using FluentValidation;
using VirtualMind.Domain.Enums;

namespace VirtualMind.Application.Commands
{
public class CreateOperationCommandValidator : AbstractValidator<CreateOperationCommand>
{
public CreateOperationCommandValidator()
{
RuleFor(input => input.UserId)
.GreaterThan(0).WithMessage("[UserId] must be greater Than 0")
.NotNull().WithMessage("[UserId] can't be null!");

RuleFor(input => input.RequestedAmount)
.GreaterThan(0).WithMessage("[RequestedAmount] must be greater Than 0")
.NotNull().WithMessage("[RequestedAmount] can't be null!");

RuleFor(input => input.CurrencyType)
.NotNull().WithMessage("[CurrencyType] can't be null!")
.NotEmpty().WithMessage("[CurrencyType] field is required!")
.IsEnumName(typeof(Currency), false).WithMessage("Invalid [CurrencyType]!");
}
}
}
24 changes: 24 additions & 0 deletions VirtualMind.Application/Extensions/DecimalExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Globalization;

namespace VirtualMind.Application.Extensions
{
public static class DecimalExtensions
{
public static decimal ConvertStringToDecimal(this string value)
{
var culture = new CultureInfo("en-US");
decimal outPutValue;

if (decimal.TryParse(s: value, style: NumberStyles.Float, result: out outPutValue, provider: culture))
{
return outPutValue;
}
else
{
Console.WriteLine($"Invalid Decimal Conversion with this value : [{value}]");
return 0;
}
}
}
}
19 changes: 19 additions & 0 deletions VirtualMind.Application/Extensions/MonetaryConversions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Globalization;

namespace VirtualMind.Application.Extensions
{
public static class MonetaryConversions
{
public static string ConvertUSDToBRL(this string value)
{
var culture = new CultureInfo("en-US");

var resultCast = value.ConvertStringToDecimal();

var conversionResult = resultCast / 4;

return conversionResult.ToString(culture);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

namespace VirtualMind.Application.Queries
{
public class GetCurrencyExchange : IRequest<List<ExchangeRateDTO>>
public class GetCurrencyExchangeQuery : IRequest<List<ExchangeRateDTO>>
{
public string CurrencyType { get; set; }
}

public class GetCurrencyExchangeHandler : IRequestHandler<GetCurrencyExchange, List<ExchangeRateDTO>>
public class GetCurrencyExchangeHandler : IRequestHandler<GetCurrencyExchangeQuery, List<ExchangeRateDTO>>
{
private readonly ICurrencyExchangeFactory CurrencyExchangeFactory;

Expand All @@ -20,7 +20,7 @@ public GetCurrencyExchangeHandler(ICurrencyExchangeFactory currencyExchangeFacto
CurrencyExchangeFactory = currencyExchangeFactory;
}

public async Task<List<ExchangeRateDTO>> Handle(GetCurrencyExchange request, CancellationToken cancellationToken)
public async Task<List<ExchangeRateDTO>> Handle(GetCurrencyExchangeQuery request, CancellationToken cancellationToken)
{
var result = await CurrencyExchangeFactory.GetExchangeRate(request.CurrencyType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace VirtualMind.Application.Queries
{
public class GetCurrencyExchangeValidator : AbstractValidator<GetCurrencyExchange>
public class GetCurrencyExchangeValidator : AbstractValidator<GetCurrencyExchangeQuery>
{
public GetCurrencyExchangeValidator()
{
Expand Down
4 changes: 2 additions & 2 deletions VirtualMind.Application/VirtualMind.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Commands\" />
<ProjectReference Include="..\VirtualMind.Domain\VirtualMind.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VirtualMind.Domain\VirtualMind.Domain.csproj" />
<Folder Include="Commom\Extensions\" />
</ItemGroup>

</Project>
41 changes: 11 additions & 30 deletions VirtualMind.WebApp/Controllers/ExchangeController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Threading.Tasks;
using VirtualMind.Application.Commands;
using VirtualMind.Application.DTOs;
using VirtualMind.Application.Interfaces;
using VirtualMind.Application.Queries;
using MediatR;

namespace VirtualMind.WebApp.Controllers
{
Expand All @@ -15,43 +14,25 @@ namespace VirtualMind.WebApp.Controllers
public class ExchangeController : ControllerBase
{
private readonly ILogger<ExchangeController> _logger;
private readonly IMediator _mediator;
private readonly IVirtualMindDbContext _virtualMindDbContext;
private readonly IMediator _mediator;

public ExchangeController(ILogger<ExchangeController> logger,
IMediator mediator,
IVirtualMindDbContext virtualMindDbContext)
IMediator mediator)
{
_logger = logger;
_mediator = mediator;
_virtualMindDbContext = virtualMindDbContext;
_mediator = mediator;
}

[HttpGet]
public async Task<IEnumerable<ExchangeRateDTO>> GetExchangeRate([FromQuery]GetCurrencyExchange getCurrencyExchange)
public async Task<IEnumerable<ExchangeRateDTO>> GetExchangeRate([FromQuery]GetCurrencyExchangeQuery getCurrencyExchange)
{
var response = await this._mediator.Send(getCurrencyExchange);

return response;
return await _mediator.Send(getCurrencyExchange);
}

[HttpPost]
public async Task<IActionResult> PostExchangeOperation()
{
//await _virtualMindDbContext.Operations.AddAsync(new Domain.Entities.Operation
//{
// Currency = Domain.Enums.Currency.BRL,
// CurrentQuote = 10,
// PurchasedAmount = 15,
// RequestedAmount = 15,
// UserId = 1
//});

//await _virtualMindDbContext.SaveChangesAsync();

//var list = await _virtualMindDbContext.Operations.AsNoTracking().ToListAsync();

return Ok();
public async Task<ActionResult<int>> PostExchangeOperation([FromBody]CreateOperationCommand createOperationCommand)
{
return await _mediator.Send(createOperationCommand);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public WeatherForecastController(ILogger<WeatherForecastController> logger, IMed
}

[HttpGet]
public async Task<IEnumerable<WeatherForecast>> Get([FromQuery]GetCurrencyExchange getCurrencyExchange)
public async Task<IEnumerable<WeatherForecast>> Get([FromQuery]GetCurrencyExchangeQuery getCurrencyExchange)
{
var resposne = await this._mediator.Send(getCurrencyExchange);

Expand Down

0 comments on commit 8ef14d3

Please sign in to comment.