@@ -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