Skip to content

Commit 0fceeef

Browse files
committed
Use a Set instead of a Dictionary to store filtered rates
1 parent e8f8d5b commit 0fceeef

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

jobs/Backend/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ Example `appsettings.json`:
102102
- Add new providers by implementing `IApiClient<T>`
103103
- Expose rates via REST or gRPC
104104
- Add health checks or metrics
105-
- Add CD pipeline for automated deployment
105+
- Add CI/CD pipeline for automated testing and deployment

jobs/Backend/Task/Services/ExchangeRateProvider.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public async Task<ICollection<ExchangeRate>> GetExchangeRates(IEnumerable<Curren
3131
return [];
3232

3333
var cachedRates = cacheService.GetCachedRates(currencyCodes);
34-
var cachedCodes = cachedRates.Select(r => r.SourceCurrency.Code).ToHashSet(StringComparer.OrdinalIgnoreCase);
34+
var cachedCodes = cachedRates.Select(r => r.SourceCurrency.Code);
3535

3636
// Check if any of the missing currency codes have already been cached as invalid.
3737
// There's no need to call the API if all the remain missing codes are simply invalid.
3838
var invalidCodes = cacheService.GetInvalidCodes();
39-
var missingCodes = currencyCodes.Except(cachedCodes).Except(invalidCodes).ToHashSet(StringComparer.OrdinalIgnoreCase);
39+
var missingCodes = currencyCodes.Except(cachedCodes).Except(invalidCodes).ToHashSet();
4040

4141
if (missingCodes.Count == 0)
4242
return cachedRates;
@@ -46,33 +46,31 @@ public async Task<ICollection<ExchangeRate>> GetExchangeRates(IEnumerable<Curren
4646

4747
if (exchangeRates.Count < currencyCodes.Count)
4848
{
49-
var codesNotFound = currencyCodes.Except(exchangeRates.Keys);
49+
var codesNotFound = currencyCodes.Except(exchangeRates.Select(r => r.SourceCurrency.Code));
5050
cacheService.UpdateInvalidCodes(codesNotFound);
5151
logger.LogWarning("Unable to find rates for the following currencies: [{CodesNotFound}]", string.Join(", ", codesNotFound));
5252
}
5353

54-
return cachedRates.Concat(exchangeRates.Values).ToList();
54+
return cachedRates.Concat(exchangeRates).ToList();
5555
}
5656

57-
private static Dictionary<string, ExchangeRate> FilterExchangeRates(IEnumerable<CnbRate> rates, HashSet<string> currencyCodes)
57+
private static HashSet<ExchangeRate> FilterExchangeRates(IEnumerable<CnbRate> rates, HashSet<string> currencyCodes)
5858
{
5959
if (rates is null || currencyCodes.Count == 0)
6060
return [];
6161

62-
// Add matching rates to a dictionary with currency code as the key
63-
// and `ExchangeRate` as its value. To ensure consistent output,
64-
// normalise currency rates return by the api so it's per 1 unit.
62+
// Filter rates with matching currency codes to a new collection.
63+
// To ensure consistent output, normalise currency rates returned
64+
// by the api so it's always per 1 unit.
6565
return rates
6666
.Where(rate =>
67-
!string.IsNullOrWhiteSpace(rate.CurrencyCode) &&
68-
currencyCodes.Contains(rate.CurrencyCode))
69-
.ToDictionary(
70-
rate => rate.CurrencyCode,
71-
rate => new ExchangeRate(
72-
new Currency(rate.CurrencyCode),
73-
new Currency(TargetCurrencyCode),
74-
rate.Amount == 1 ? rate.Rate : rate.Rate / rate.Amount),
75-
StringComparer.OrdinalIgnoreCase);
67+
currencyCodes.Contains(rate.CurrencyCode))
68+
.Select(rate =>
69+
new ExchangeRate(
70+
new Currency(rate.CurrencyCode),
71+
new Currency(TargetCurrencyCode),
72+
rate.Amount == 1 ? rate.Rate : rate.Rate / rate.Amount))
73+
.ToHashSet();
7674
}
7775
}
7876
}

0 commit comments

Comments
 (0)