Skip to content
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
node_modules
bower_components
npm-debug.log

.vs/
28 changes: 28 additions & 0 deletions jobs/Backend/Task/Chain of Responsibility/CNB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using ExchangeRateUpdater.CNB;
using ExchangeRateUpdater.Decorator;
using ExchangeRateUpdater.Models;
using ExchangeRateUpdater.Singleton;

namespace ExchangeRateUpdater.Chain_of_Responsibility
{
public class CNB : Handler
{
private LoadRates _load;

public CNB() => _load = new APICall(new DataClean(new LoadData()));

public CNB(LoadRates loadRates) => _load = loadRates;

public override ExchangeRate GetExchangeRate(Currency currency)
{
bool result = _load.Load("").Result;

if (DB.GetInstance().TryGetValue(currency.Code, out Rate rate))
{
return new ExchangeRate(currency, new Currency("CZK"), rate.rate);
}

return null;
}
}
}
11 changes: 11 additions & 0 deletions jobs/Backend/Task/Chain of Responsibility/Handler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace ExchangeRateUpdater.Chain_of_Responsibility
{
public abstract class Handler : IHandler
{
protected IHandler? next;

public void SetNext(Handler next) => this.next = next;

public abstract ExchangeRate GetExchangeRate(Currency currency);
}
}
7 changes: 7 additions & 0 deletions jobs/Backend/Task/Chain of Responsibility/IHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ExchangeRateUpdater.Chain_of_Responsibility
{
public interface IHandler
{
ExchangeRate GetExchangeRate(Currency currency);
}
}
26 changes: 26 additions & 0 deletions jobs/Backend/Task/Chain of Responsibility/Redis.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using ExchangeRateUpdater.Models;
using ExchangeRateUpdater.Singleton;

namespace ExchangeRateUpdater.Chain_of_Responsibility
{
public class Redis : Handler
{
private DB _rates;

public Redis() => _rates = DB.GetInstance();

public override ExchangeRate GetExchangeRate(Currency currency)
{
if (_rates.TryGetValue(currency.Code, out Rate rate))
{
return new ExchangeRate(currency, new Currency("CZK"), rate.rate);
}
else if(!_rates.IsEmpty())
{
return null;
}

return next.GetExchangeRate(currency);
}
}
}
74 changes: 74 additions & 0 deletions jobs/Backend/Task/Decorator/APICall.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ExchangeRateUpdater.Decorator;
using ExchangeRateUpdater.Helpers;

namespace ExchangeRateUpdater.CNB
{
public class APICall : LoadRates
{
string path;

readonly HttpClient client;
StringBuilder result;

HttpResponseMessage response;

public APICall(ILoadRates loadRates) : base(loadRates)
{
path = ConfigHelper.GetCnbApiPath();
client = new();
}

public APICall(ILoadRates loadRates, HttpClient httpClient) : base(loadRates)
{
path = ConfigHelper.GetCnbApiPath();
client = httpClient;
}

public override async Task<bool> Load(string data)
{
try
{
if (Regex.IsMatch(data, @"^([0-2]?[0-9]|3[0-1])\.(0[1-9]|1[0-2])\.\d{4}$"))
{
path += $"?date = {data}";
}
}
catch(ArgumentNullException e)
{
Console.WriteLine($"Wrong input: '{e.Message}'.");
}

return await load();
}

private async Task<bool> load()
{
result = new();

try
{
response = await client.GetAsync(path);

if (response.IsSuccessStatusCode)
{
result.Append(await response.Content.ReadAsStringAsync());
}
else
{
throw new Exception("No data from the resource");
}
}
catch(HttpRequestException)
{
throw;
}

return await wrapper.Load(result.ToString());
}
}
}
46 changes: 46 additions & 0 deletions jobs/Backend/Task/Decorator/DataClean.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ExchangeRateUpdater.Decorator
{
public class DataClean : LoadRates
{
private string _line;
private StringBuilder _correctLines;

public DataClean(ILoadRates wrapper) : base(wrapper) { }

public override async Task<bool> Load(string data)
{
_correctLines = new();

try
{
using (StringReader reader = new(data))
{
while ((_line = reader.ReadLine()) != null)
{
if (Regex.IsMatch(_line, @"^[A-Za-z ]+\|[A-Za-z ]+\|(1|100|1000)\|[A-Z]{3}\|\d+\.\d+$"))
{
_correctLines.AppendLine(_line);
}
}
}
}
catch (ArgumentNullException)
{
throw;
}

if (_correctLines.Length == 0)
{
throw new Exception("No data to be added to the data base");
}

return await wrapper.Load(_correctLines.ToString());
}
}
}
13 changes: 13 additions & 0 deletions jobs/Backend/Task/Decorator/ILoadRates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExchangeRateUpdater.Decorator
{
public interface ILoadRates
{
public Task<bool> Load(string data);
}
}
46 changes: 46 additions & 0 deletions jobs/Backend/Task/Decorator/LoadData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using ExchangeRateUpdater.Models;
using ExchangeRateUpdater.Singleton;

namespace ExchangeRateUpdater.Decorator
{
public class LoadData : ILoadRates
{
private string _line;
private DB _rates;

public LoadData() => _rates = DB.GetInstance();

public async Task<bool> Load(string data)
{
try
{
using (StringReader reader = new(data))
{
while ((_line = reader.ReadLine()) != null)
{
string[] list = _line.Split('|');
_rates.Add(list[3], new Rate(list[0], list[1], Convert.ToInt16(list[2]), list[3], Convert.ToDecimal(list[4], CultureInfo.InvariantCulture)));
}
}
}
catch (IndexOutOfRangeException)
{
throw;
}
catch (ArgumentNullException)
{
throw;
}
catch (FormatException)
{
throw;
}

return !_rates.IsEmpty();
}
}
}
13 changes: 13 additions & 0 deletions jobs/Backend/Task/Decorator/LoadRates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Threading.Tasks;

namespace ExchangeRateUpdater.Decorator
{
public abstract class LoadRates : ILoadRates
{
protected ILoadRates wrapper;

protected LoadRates(ILoadRates wrapper) => this.wrapper = wrapper;

public abstract Task<bool> Load(string data);
}
}
27 changes: 25 additions & 2 deletions jobs/Backend/Task/ExchangeRateProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using ExchangeRateUpdater.Chain_of_Responsibility;

namespace ExchangeRateUpdater
{
Expand All @@ -11,9 +11,32 @@ public class ExchangeRateProvider
/// 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>

private Handler handler;
private List<ExchangeRate> _exchangeRates;

public ExchangeRateProvider()
{
_exchangeRates = new();

handler = new Redis();
Chain_of_Responsibility.CNB cnb = new();
handler.SetNext(cnb);
}

public IEnumerable<ExchangeRate> GetExchangeRates(IEnumerable<Currency> currencies)
{
return Enumerable.Empty<ExchangeRate>();
foreach (Currency currency in currencies)
{
ExchangeRate exchangeRate = handler.GetExchangeRate(currency);

if(exchangeRate is not null)
{
_exchangeRates.Add(exchangeRate);
}
}

return _exchangeRates;
}
}
}
14 changes: 14 additions & 0 deletions jobs/Backend/Task/ExchangeRateUpdater.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,18 @@
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<None Remove="ExchangeRateProvider.cs~RF143d8135.TMP" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="6.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
13 changes: 11 additions & 2 deletions jobs/Backend/Task/ExchangeRateUpdater.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25123.0
# Visual Studio Version 17
VisualStudioVersion = 17.14.36414.22 d17.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExchangeRateUpdater", "ExchangeRateUpdater.csproj", "{7B2695D6-D24C-4460-A58E-A10F08550CE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestExchangeRateUpdater", "..\TestExchangeRateUpdater\TestExchangeRateUpdater.csproj", "{3401A3CA-1731-4126-A7AA-D28D430DA0E2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,8 +17,15 @@ Global
{7B2695D6-D24C-4460-A58E-A10F08550CE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B2695D6-D24C-4460-A58E-A10F08550CE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B2695D6-D24C-4460-A58E-A10F08550CE0}.Release|Any CPU.Build.0 = Release|Any CPU
{3401A3CA-1731-4126-A7AA-D28D430DA0E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3401A3CA-1731-4126-A7AA-D28D430DA0E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3401A3CA-1731-4126-A7AA-D28D430DA0E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3401A3CA-1731-4126-A7AA-D28D430DA0E2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2D09087A-D6FC-436D-BBF9-1CE6413B2831}
EndGlobalSection
EndGlobal
18 changes: 18 additions & 0 deletions jobs/Backend/Task/Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.IO;
using System.Text.Json;

namespace ExchangeRateUpdater.Helpers
{
internal static class ConfigHelper
{
public static string GetCnbApiPath()
{
var json = File.ReadAllText("appsettings.json");
using var doc = JsonDocument.Parse(json);
return doc.RootElement
.GetProperty("CNB")
.GetProperty("ApiPath")
.GetString()!;
}
}
}
Loading