diff --git a/README.md b/README.md index 43b8bce..7335186 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,9 @@ The CountryData.Standard library provides a set of methods that allow you to ret | [`GetPhoneCodeByCountryShortCode(string shortCode)`](./docs/README.md) | Returns a single country's phone code by its short code. | | [`GetCountryByPhoneCode(string phoneCode)`](./docs/README.md) | Returns country data for the country associated with the specified phone code. | | [`GetCurrencyCodesByCountryCode(string shortCode)`](./docs/README.md) | Returns a list of currency codes for a specific country identified by its short code. | -| [`GetCountryByCurrencyCode(string currencyCode)`](./docs/README.md) | Returns a list of countries that use the specified currency code. | +| [`GetCountryByCurrencyCode(string currencyCode)`](./docs/README.md) | Returns a list of countries that use the specified currency code. | +| [`GetCountryCode(string countryName)`](./docs/README.md) | Returns the country code for a specific country name. | + ### Initialize the Country data object diff --git a/docs/README.md b/docs/README.md index 13bd54c..137f014 100644 --- a/docs/README.md +++ b/docs/README.md @@ -186,3 +186,16 @@ Country GetCountryByCurrencyCode(string currencyCode); This method is particularly useful when you have a currency code and need to retrieve the corresponding country's information. Simply pass the currency code as a string argument, and the method will return a `Country` object filled with relevant data. +### GetCountryCode(string countryName) + +The `GetCountryCode()` method is designed to fetch the ISO 3166-1 alpha-2 code for a specific country using its name. This method returns a string containing the country's ISO code, making it easy to identify countries based on their names. + +Here's the method signature in C#: + +```csharp + +string GetCountryCode(string countryName); +``` + + + diff --git a/sample/CountryData.Sample.Console/Program.cs b/sample/CountryData.Sample.Console/Program.cs index d082a2d..f8793bc 100644 --- a/sample/CountryData.Sample.Console/Program.cs +++ b/sample/CountryData.Sample.Console/Program.cs @@ -26,6 +26,7 @@ public static void Main() GetCountryByPhoneCode("+233"); GetCurrencyCodesByCountryCode("US"); GetCountryByCurrencyCode("GHS"); + GetCountryCode("Ghana"); } /// @@ -145,5 +146,22 @@ private static void GetCountryByCurrencyCode(string currencyCode) Console.WriteLine(country.CountryName); } } + + + + /// + /// Retrieves the country code for a given country name and prints it to the console. + /// + /// The name of the country. + private static void GetCountryCode(string countryName) + { + using var manager = new CountryExtensions(); + var result = manager.GetCountryCode(countryName); + Console.WriteLine($"Country code for {countryName} is {result} "); + } + + + + } } diff --git a/sample/CountryData.Sample.Web.API/Controllers/CountryController.cs b/sample/CountryData.Sample.Web.API/Controllers/CountryController.cs index e3126ac..8a5d036 100644 --- a/sample/CountryData.Sample.Web.API/Controllers/CountryController.cs +++ b/sample/CountryData.Sample.Web.API/Controllers/CountryController.cs @@ -186,5 +186,32 @@ public IActionResult GetCountryByCurrencyCode([FromQuery] string currencyCode) return Ok(countryByCurrencyCode); } + + + /// + /// Retrieves the country code for a given country name. + /// + /// The name of the country. + /// The country code if found; otherwise, a NotFound result. + [HttpGet("countryCodeByName")] + [ProducesResponseType(typeof(string), 200)] + [ProducesResponseType(404)] + public IActionResult GetCountryCodeByName([FromQuery] string countryName) + + + { + using var manager = new CountryExtensions(); + var countryCode = manager.GetCountryCode(countryName); + if (countryCode == null) + { + return NotFound(); + } + return Ok(countryCode); + } + + } -} \ No newline at end of file + + + +} diff --git a/sample/CountryData.Sample.Web.API/CountryData.Sample.Web.API.csproj b/sample/CountryData.Sample.Web.API/CountryData.Sample.Web.API.csproj index 8633b4c..5669d9e 100644 --- a/sample/CountryData.Sample.Web.API/CountryData.Sample.Web.API.csproj +++ b/sample/CountryData.Sample.Web.API/CountryData.Sample.Web.API.csproj @@ -4,6 +4,8 @@ net8.0 enable enable + true + $(NoWarn);1591 diff --git a/sample/CountryData.Sample.Web.API/Program.cs b/sample/CountryData.Sample.Web.API/Program.cs index 3487f45..4cf1122 100644 --- a/sample/CountryData.Sample.Web.API/Program.cs +++ b/sample/CountryData.Sample.Web.API/Program.cs @@ -1,3 +1,6 @@ +using Microsoft.OpenApi.Models; +using System.Reflection; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -5,9 +8,24 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen(c => +{ + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = " CountryData.Stand", + Version = "v1", + Description = " Sample endpoint for CountryData.Standard " + + }); + + // Set the comments path for the Swagger JSON and UI. + var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; + var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); + c.IncludeXmlComments(xmlPath); +}); // Register CountryHelper service builder.Services.AddScoped(); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -23,4 +41,4 @@ app.MapControllers(); -app.Run(); +await app.RunAsync(); diff --git a/src/CountryData.Standard/CountryExtensions.cs b/src/CountryData.Standard/CountryExtensions.cs new file mode 100644 index 0000000..cd9b7d5 --- /dev/null +++ b/src/CountryData.Standard/CountryExtensions.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; + +namespace CountryData.Standard +{ + /// + /// Manages the CountryHelper instance and provides methods for retrieving country-related information. + /// + public class CountryExtensions : IDisposable + { + private CountryHelper _countryHelper; + private bool _disposed = false; + + /// + /// Constructor to initialize the CountryHelper instance. + /// + public CountryExtensions() + { + _countryHelper = new CountryHelper(); + } + + /// + /// Gets the country code for the specified country name. + /// + /// The name of the country. + /// The country code if found; otherwise, null. + public string GetCountryCode(string countryName) + { + if (countryName == null) + { + throw new ArgumentNullException(nameof(countryName)); + } + + var country = _countryHelper.GetCountryData() + .FirstOrDefault(c => c.CountryName.Equals(countryName, StringComparison.OrdinalIgnoreCase)); + + return country?.CountryShortCode; + } + + /// + /// Dispose method to clean up resources. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Protected method to dispose resources. + /// + /// Indicates whether the method is called from Dispose or finalizer. + protected virtual void Dispose(bool disposing) + { + if (!_disposed) + { + if (disposing && _countryHelper != null) + { + // Dispose managed resources. + _countryHelper = null; + } + + // Dispose unmanaged resources. + + _disposed = true; + } + } + + /// + /// Finalizer to ensure resources are cleaned up. + /// + ~CountryExtensions() + { + Dispose(false); + } + } +} diff --git a/src/CountryData.Standard/CountryHelper.cs b/src/CountryData.Standard/CountryHelper.cs index 9fd576b..c22ff14 100644 --- a/src/CountryData.Standard/CountryHelper.cs +++ b/src/CountryData.Standard/CountryHelper.cs @@ -24,7 +24,7 @@ public CountryHelper() /// - /// + /// Read /// /// /// @@ -47,7 +47,7 @@ private string GetJsonData(string path) /// that can be querried by Lambda Expressions /// /// IEnumerable - public IEnumerable GetCountryData() + public virtual IEnumerable GetCountryData() { return _Countries; } diff --git a/src/CountryData.Standard/Currency.cs b/src/CountryData.Standard/Currency.cs index de438c5..a4b5137 100644 --- a/src/CountryData.Standard/Currency.cs +++ b/src/CountryData.Standard/Currency.cs @@ -4,5 +4,6 @@ public class Currency { public string Code { get; set; } public string Name { get; set; } + } } diff --git a/src/CountryData.Standard/Regions.cs b/src/CountryData.Standard/Regions.cs index 5790ac8..872c57b 100644 --- a/src/CountryData.Standard/Regions.cs +++ b/src/CountryData.Standard/Regions.cs @@ -6,5 +6,7 @@ public class Regions { public String Name { get; set; } public String ShortCode { get; set; } + + } } diff --git a/test/CountryData.UnitTests/CountryExtensionsTests.cs b/test/CountryData.UnitTests/CountryExtensionsTests.cs new file mode 100644 index 0000000..23355f2 --- /dev/null +++ b/test/CountryData.UnitTests/CountryExtensionsTests.cs @@ -0,0 +1,97 @@ +using CountryData.Standard; +using FluentAssertions; +using System; +using Xunit; + +namespace CountryData.UnitTests +{ + public class CountryHelperManagerTests + { + /// + /// Tests that GetCountryCode returns the correct country code for a valid country name. + /// + [Fact] + public void GetCountryCode_ValidCountryName_ReturnsCountryCode() + { + // Arrange + using var manager = new CountryExtensions(); + var countryName = "United States"; + + // Act + var result = manager.GetCountryCode(countryName); + + // Assert + result.Should().Be("US"); + } + + /// + /// Tests that GetCountryCode returns null for an invalid country name. + /// + [Fact] + public void GetCountryCode_InvalidCountryName_ReturnsNull() + { + // Arrange + using var manager = new CountryExtensions(); + var countryName = "Invalid Country"; + + // Act + var result = manager.GetCountryCode(countryName); + + // Assert + result.Should().BeNull(); + } + + /// + /// Tests that GetCountryCode throws an ArgumentNullException for a null country name. + /// + [Fact] + public void GetCountryCode_NullCountryName_ThrowsArgumentNullException() + { + // Arrange + using var manager = new CountryExtensions(); + string? countryName = null; + + // Act + Action act = () => manager.GetCountryCode(countryName!); + + // Assert + act.Should().Throw(); + } + + /// + /// Tests that Dispose method sets the _disposed flag to true. + /// + [Fact] + public void Dispose_SetsDisposedFlagToTrue() + { + // Arrange + var manager = new CountryExtensions(); + + // Act + manager.Dispose(); + + // Assert + var disposedField = typeof(CountryExtensions).GetField("_disposed", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + var disposedValue = (bool)disposedField.GetValue(manager); + disposedValue.Should().BeTrue(); + } + + /// + /// Tests that Dispose method sets the _countryHelper to null. + /// + [Fact] + public void Dispose_SetsCountryHelperToNull() + { + // Arrange + var manager = new CountryExtensions(); + + // Act + manager.Dispose(); + + // Assert + var countryHelperField = typeof(CountryExtensions).GetField("_countryHelper", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); + var countryHelperValue = countryHelperField.GetValue(manager); + countryHelperValue.Should().BeNull(); + } + } +}