-
Notifications
You must be signed in to change notification settings - Fork 0
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
4debb96
commit 8ef14d3
Showing
11 changed files
with
164 additions
and
53 deletions.
There are no files selected for viewing
15 changes: 0 additions & 15 deletions
15
VirtuaMind.Infrastructure/FunctionalServices/MonetaryConversions.cs
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
VirtuaMind.Infrastructure/RestServices/ExternalServices/BancoProvinciaService.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
78 changes: 78 additions & 0 deletions
78
VirtualMind.Application/Commands/CreateOperationCommand.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,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); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
VirtualMind.Application/Commands/CreateOperationCommandValidator.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,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]!"); | ||
} | ||
} | ||
} |
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 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; | ||
} | ||
} | ||
} | ||
} |
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 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); | ||
} | ||
} | ||
} |
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
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