Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mews Backend Task - ExchangeRateUpdater solution #677

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 42 additions & 21 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
!.gitkeep
!.gitignore
!*.dll
[Oo]bj
[Bb]in
*.user
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode/

# Rider
.idea/

# Visual Studio
.vs/

# Fleet
.fleet/

# Code Rush
.cr/

# User-specific files
*.suo
*.[Cc]ache
*.bak
*.ncb
*.DS_Store
*.userprefs
*.iml
*.ncrunch*
.*crunch*.local.xml
.idea
[Tt]humbs.db
*.tgz
*.sublime-*
*.user
*.userosscache
*.sln.docstates

node_modules
bower_components
npm-debug.log
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn
appsettings.Development.json
19 changes: 0 additions & 19 deletions jobs/Backend/Task/ExchangeRateProvider.cs

This file was deleted.

8 changes: 0 additions & 8 deletions jobs/Backend/Task/ExchangeRateUpdater.csproj

This file was deleted.

22 changes: 0 additions & 22 deletions jobs/Backend/Task/ExchangeRateUpdater.sln

This file was deleted.

43 changes: 0 additions & 43 deletions jobs/Backend/Task/Program.cs

This file was deleted.

44 changes: 44 additions & 0 deletions jobs/Backend/Task/src/ExchangeRateUpdater.App/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode/

# Rider
.idea/

# Visual Studio
.vs/

# Fleet
.fleet/

# Code Rush
.cr/

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
*.sln
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn
appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ExchangeRateUpdater.Domain\ExchangeRateUpdater.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExchangeRateUpdater.Domain.DTO;
using ExchangeRateUpdater.Domain.Models;

namespace ExchangeRateUpdater.Extensions
{
public static class ConverterExtension
{
public static IEnumerable<ExchangeRate> ToExchangeRates(this ExchangeRatesDTO exchangeRates)
{
return exchangeRates.Rates.Select(r => new ExchangeRate(new Currency(r.CurrencyCode), new Currency("CZK"), r.Rate / r.Amount));
}
}
}
60 changes: 60 additions & 0 deletions jobs/Backend/Task/src/ExchangeRateUpdater.App/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using ExchangeRateUpdater;
using ExchangeRateUpdater.Domain.Configurations;
using ExchangeRateUpdater.Providers;
using ExchangeRateUpdater.Services;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;



var config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(config)
.CreateLogger();
var host = new HostBuilder()
.ConfigureServices((services) =>
{

var settings = new ExchangeRateProviderSettings();
config.GetSection("ExchangeRateProviderSettings").Bind(settings);
services.AddHttpClient<ExchangeRateService>("exchange",
(serviceProvider, client) =>
{
client.BaseAddress = new Uri($"{settings.UrlBaseAPI}/{settings.UrlExchangeRate}");
})
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new SocketsHttpHandler()
{
PooledConnectionLifetime = TimeSpan.FromMinutes(15)
};
})
.SetHandlerLifetime(Timeout.InfiniteTimeSpan);

services.AddTransient<IExchangeRateProvider, ExchangeRateProvider>();
services.AddTransient<IExchangeRateService, ExchangeRateService>();
services.AddSingleton<IMemoryCache, MemoryCache>();
})
.UseSerilog()
.Build();

var exchangeRateProvider = host.Services.GetRequiredService<IExchangeRateProvider>();

for (int i = 0; i < 3; i++)
{
var rates = await exchangeRateProvider.GetExchangeRatesAsync(TestingData.currencies);
foreach (var rate in rates)
{
Console.WriteLine(rate.ToString());
}
Console.WriteLine("-----------------");

}



Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using ExchangeRateUpdater.Domain.Models;
using Microsoft.Extensions.Logging;
using ExchangeRateUpdater.Services;
using System.Threading.Tasks;
using ExchangeRateUpdater.Domain.DTO;
namespace ExchangeRateUpdater.Providers
{
public class ExchangeRateProvider(ILogger<ExchangeRateProvider> _logger, IExchangeRateService exchangeRateService) : IExchangeRateProvider
{
/// <summary>
/// Should return exchange rates among the specified currencies that are defined by the source. But only those defined
/// by the source, do not return calculated exchange rates. E.g. if the source contains "CZK/USD" but not "USD/CZK",
/// do not return exchange rate "USD/CZK" with value calculated as 1 / "CZK/USD". If the source does not provide
/// some of the currencies, ignore them.
/// </summary>
public async Task<IEnumerable<ExchangeRate>> GetExchangeRatesAsync(IEnumerable<Currency> currencies)
{
var result = new List<ExchangeRate>();
try
{
_logger.LogInformation("Started fetching exchange rates");
var exchangeRates = await exchangeRateService.GetExchangeRateAsync();
foreach (var currency in currencies)
{
_logger.LogInformation($"Adding exchange rate for {currency.Code}");
result.AddRange(exchangeRates.Where(e => e.TargetCurrency.Code == currency.Code));
_logger.LogInformation($"Added exchange rate for {currency.Code}");
}
}
catch (Exception ex)
{
_logger.LogError(ex, ex.Message);
return null;
}
return result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExchangeRateUpdater.Domain.DTO;
using ExchangeRateUpdater.Domain.Models;
using ExchangeRateUpdater.Services;

namespace ExchangeRateUpdater.Providers
{
public interface IExchangeRateProvider
{
public Task<IEnumerable<ExchangeRate>> GetExchangeRatesAsync(IEnumerable<Currency> currencies);
}
}
Loading