Skip to content

Commit

Permalink
:)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigojap committed Feb 3, 2021
1 parent 22cf314 commit 3c802e4
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 23 deletions.
16 changes: 16 additions & 0 deletions VirtuaMind.Infrastructure/DI/DependencyInjection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.DependencyInjection;
using VirtualMind.Application.Interfaces;
using VirtuaMind.Infrastructure.RestServices.ExternalServices;

namespace VirtuaMind.Infrastructure.DI
{
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services)
{
services.AddScoped<IBancoProvinciaRestService, BancoProvinciaService>();

return services;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace VirtuaMind.Infrastructure.FunctionalServices
{
public static class MonetaryConversions
{
public static string ConvertUSDToBRL(this string value)
{
decimal resultCast;
decimal.TryParse(value, out resultCast);

var conversionResult = resultCast / 4;

return conversionResult.ToString();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Threading.Tasks;
using VirtualMind.Application.Interfaces;
using VirtuaMind.Infrastructure.FunctionalServices;
using VirtuaMind.Infrastructure.RestServices.Template;

namespace VirtuaMind.Infrastructure.RestServices.ExternalServices
{
public class BancoProvinciaService : RestService, IBancoProvinciaRestService
{
private readonly string BASEURL = "https://www.bancoprovincia.com.ar";

public BancoProvinciaService()
{
SetClientRequest(BASEURL);
}

public async Task<List<string>> GetUSDExchangeRate()
{
var result = await Execute(null, "Principal/Dolar", RestSharp.Method.GET);

var content = JsonConvert.DeserializeObject<List<string>>(result);

return content;
}

public async Task<List<string>> GetBRLExchangeRate()
{
var content = await GetUSDExchangeRate();

var convertedList = new List<string>()
{
content[0].ConvertUSDToBRL(),
content[1].ConvertUSDToBRL(),
content[2]
};

return convertedList;
}
}
}
80 changes: 80 additions & 0 deletions VirtuaMind.Infrastructure/RestServices/Template/RestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using RestSharp;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace VirtuaMind.Infrastructure.RestServices.Template
{
public abstract class RestService
{
private RestClient BaseClient;
private RestRequest Request;
private dynamic Response;

public RestService()
{
BaseClient = new RestClient();
Request = new RestRequest();
}

protected async Task<dynamic> Execute(Dictionary<string, object> keyValueParameters, string requestUri, Method method)
{
InitializeParametersRequest(requestUri, method, keyValueParameters);

await SendRequestAsync();

return Response.Content;
}

protected void SetClientRequest(string baseURL)
{
BaseClient = new RestClient(baseURL);
}

private void InitializeParametersRequest(string requestUri,
Method method,
Dictionary<string, object> keyValueParameters)
{
SetResource(requestUri);
SetHttpMethod(method);
AddParameters(keyValueParameters);
}

private void SetResource(string requestUri)
{
Request.Resource = requestUri;
}

private void SetHttpMethod(Method method)
{
Request.Method = method;
}

private void AddParameters(Dictionary<string, object> keyValueParameters)
{
if (keyValueParameters == null)
return;

foreach (var (key, value) in keyValueParameters)
{
Request.AddParameter(key, value);
}
}

private async Task SendRequestAsync<T>()
{
Response = await BaseClient.ExecuteAsync<T>(this.Request);
}

private async Task SendRequestAsync()
{
Response = await BaseClient.ExecuteAsync(this.Request);
}

public void Dispose()
{
BaseClient = null;
Request = null;
Response = null;
}
}
}
16 changes: 16 additions & 0 deletions VirtuaMind.Infrastructure/VirtuaMind.Infrastructure.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="RestSharp" Version="106.11.7" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\VirtualMind.Application\VirtualMind.Application.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static IServiceCollection AddApplicationDI(this IServiceCollection servic
{
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
services.AddMediatR(Assembly.GetExecutingAssembly());
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehaviour<,>));

return services;
}
Expand Down
12 changes: 12 additions & 0 deletions VirtualMind.Application/Interfaces/IBancoProvinciaService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace VirtualMind.Application.Interfaces
{
public interface IBancoProvinciaRestService
{
Task<List<string>> GetUSDExchangeRate();

Task<List<string>> GetBRLExchangeRate();
}
}
30 changes: 21 additions & 9 deletions VirtualMind.Application/Queries/GetCurrencyExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using VirtualMind.Application.DTOs;
using VirtualMind.Application.Interfaces;

namespace VirtualMind.Application.Queries
{
Expand All @@ -13,18 +14,29 @@ public class GetCurrencyExchange : IRequest<List<ExchangeRateDTO>>

public class GetCurrencyExchangeHandler : IRequestHandler<GetCurrencyExchange, List<ExchangeRateDTO>>
{
private readonly IBancoProvinciaRestService BancoProvinciaRestService;

public GetCurrencyExchangeHandler(IBancoProvinciaRestService bancoProvinciaService)
{
this.BancoProvinciaRestService = bancoProvinciaService;
}

public async Task<List<ExchangeRateDTO>> Handle(GetCurrencyExchange request, CancellationToken cancellationToken)
{
//MUST BE A SERVICE API
var exchange = new ExchangeRateDTO();
exchange.LastUpdate = "today";
exchange.Purchase = "12.00";
exchange.Sale = "10";
{
var result = await BancoProvinciaRestService
.GetBRLExchangeRate();

var exchangeList = new List<ExchangeRateDTO>();
exchangeList.Add(exchange);
var exchangeList = new List<ExchangeRateDTO>
{
new ExchangeRateDTO
{
Purchase = result[0],
Sale = result[1],
LastUpdate = result[2]
}
};

return await Task.FromResult(exchangeList);
return exchangeList;
}
}
}
11 changes: 0 additions & 11 deletions VirtualMind.Domain/Entities/ExchangeRate.cs

This file was deleted.

4 changes: 4 additions & 0 deletions VirtualMind.Domain/VirtualMind.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Entities\" />
</ItemGroup>

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

namespace VirtualMind.WebApp.Controllers
{
[ApiController]
[Route("[controller]")]
public class ExchangeController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IMediator _mediator;

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

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

return response;
}
}
}
2 changes: 2 additions & 0 deletions VirtualMind.WebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Hosting;
using VirtualMind.Application.Configurations;
using VirtualMind.WebApp.Filters;
using VirtuaMind.Infrastructure.DI;

namespace VirtualMind.WebApp
{
Expand All @@ -23,6 +24,7 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationDI();
services.RegisterGlobalFilters();
services.AddInfrastructure();

// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
Expand Down
1 change: 1 addition & 0 deletions VirtualMind.WebApp/VirtualMind.WebApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

<ItemGroup>
<ProjectReference Include="..\VirtualMind.Application\VirtualMind.Application.csproj" />
<ProjectReference Include="..\VirtuaMind.Infrastructure\VirtuaMind.Infrastructure.csproj" />
</ItemGroup>

<Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
Expand Down
11 changes: 9 additions & 2 deletions VirtualMind.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "3 - Domain", "3 - Domain",
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "4 - Infra", "4 - Infra", "{7804AEC5-0FE4-4706-A064-F1F4AA9421F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtualMind.Application", "VirtualMind.Application\VirtualMind.Application.csproj", "{C7E47AE3-D1C8-4395-88C8-AAE5818D5ACC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VirtualMind.Application", "VirtualMind.Application\VirtualMind.Application.csproj", "{C7E47AE3-D1C8-4395-88C8-AAE5818D5ACC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtualMind.Domain", "VirtualMind.Domain\VirtualMind.Domain.csproj", "{4F80DA53-FD10-4CDF-93B5-CA99737CE7D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VirtualMind.Domain", "VirtualMind.Domain\VirtualMind.Domain.csproj", "{4F80DA53-FD10-4CDF-93B5-CA99737CE7D4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VirtuaMind.Infrastructure", "VirtuaMind.Infrastructure\VirtuaMind.Infrastructure.csproj", "{EEF1CC3D-65A5-4AE5-A394-86F294261237}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -35,6 +37,10 @@ Global
{4F80DA53-FD10-4CDF-93B5-CA99737CE7D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F80DA53-FD10-4CDF-93B5-CA99737CE7D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F80DA53-FD10-4CDF-93B5-CA99737CE7D4}.Release|Any CPU.Build.0 = Release|Any CPU
{EEF1CC3D-65A5-4AE5-A394-86F294261237}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEF1CC3D-65A5-4AE5-A394-86F294261237}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEF1CC3D-65A5-4AE5-A394-86F294261237}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEF1CC3D-65A5-4AE5-A394-86F294261237}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -43,6 +49,7 @@ Global
{D72102A2-1750-4B82-B91A-1B132D86B094} = {92605427-E7F4-457F-B8E7-9A31412FD4B7}
{C7E47AE3-D1C8-4395-88C8-AAE5818D5ACC} = {0AEF29E2-7E5A-4012-AEB4-184C1BC44328}
{4F80DA53-FD10-4CDF-93B5-CA99737CE7D4} = {F3E702F6-1383-4A90-AE2C-2863A1E1EDD1}
{EEF1CC3D-65A5-4AE5-A394-86F294261237} = {7804AEC5-0FE4-4706-A064-F1F4AA9421F1}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BBFF54F8-17FC-477F-852E-5089020CE7BF}
Expand Down

0 comments on commit 3c802e4

Please sign in to comment.