Skip to content
Open
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
28 changes: 19 additions & 9 deletions src/CountryData.Standard/CountryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class CountryHelper
{
private readonly IEnumerable<Country> _Countries;
private const string strFileName = "CountryData.Standard.data.json";
private readonly Dictionary<string, Country> _countriesByShortCode;

public CountryHelper()
{
Expand All @@ -20,6 +21,7 @@ public CountryHelper()
{
country.CountryFlag = GetCountryEmojiFlag(country.CountryShortCode);
}
_countriesByShortCode = _Countries.ToDictionary(c => c.CountryShortCode);
}


Expand Down Expand Up @@ -58,9 +60,12 @@ public virtual IEnumerable<Country> GetCountryData()
/// </summary>
/// <param name="ShortCode"></param>
/// <returns>Country</returns>
/// <exception cref="ArgumentNullException">Thrown when the shortCode parameter is null.</exception>

public Country GetCountryByCode(string shortCode)
{
return _Countries.SingleOrDefault(c => c.CountryShortCode == shortCode);
_countriesByShortCode.TryGetValue(shortCode, out var country);
return country;
}

/// <summary>
Expand All @@ -75,15 +80,18 @@ public string GetCountryEmojiFlag(string shortCode)


/// <summary>
/// Selects Regions in a Particular Country
/// Selects Regions in a Particular Country, if the country is not found, it returns an empty list
/// </summary>
/// <param name="ShortCode"></param>
/// <returns>List<Regions> a list of regions</returns>
/// <exception cref="ArgumentNullException">Thrown when the shortCode parameter is null.</exception>
public List<Regions> GetRegionByCountryCode(string ShortCode)
{
return _Countries.Where(x => x.CountryShortCode == ShortCode)
.Select(r => r.Regions).FirstOrDefault()
.ToList();
_countriesByShortCode.TryGetValue(ShortCode, out var country);

return country is null
? Enumerable.Empty<Regions>().ToList()
: country.Regions.ToList();
}

/// <summary>
Expand All @@ -100,9 +108,11 @@ public List<Regions> GetRegionByCountryCode(string ShortCode)
/// </summary>
/// <param name="shortCode"></param>
/// <returns>string</returns>
/// <exception cref="ArgumentNullException">Thrown when the shortCode parameter is null.</exception>

public string GetPhoneCodeByCountryShortCode(string shortCode)
{
var country = _Countries.SingleOrDefault(c => c.CountryShortCode == shortCode);
_countriesByShortCode.TryGetValue(shortCode, out var country);
return country?.PhoneCode;
}

Expand All @@ -125,11 +135,11 @@ public IEnumerable<Country> GetCountryByPhoneCode(string phoneCode)
/// </summary>
/// <param name="shortCode">The short code of the country.</param>
/// <returns>An IEnumerable of Currency objects associated with the specified country.</returns>
/// <exception cref="ArgumentNullException">Thrown when the shortCode parameter is null.</exception>
public IEnumerable<Currency> GetCurrencyCodesByCountryCode(string shortCode)
{
return _Countries.Where(x => x.CountryShortCode == shortCode)
.SelectMany(c => c.Currency)
.ToList();
_countriesByShortCode.TryGetValue(shortCode, out var country);
return country?.Currency.ToList();
}


Expand Down