Skip to content

Commit 64f0d45

Browse files
committed
Add GetCountryCode method and enhance API and docs
- Added `GetCountryCode(string countryName)` to `CountryData.Standard` to return ISO 3166-1 alpha-2 codes. - Updated `README.md` with documentation for `GetCountryCode`. - Modified `Program.cs` in `CountryData.Sample.CountryConsoleProject` to call and print the result of `GetCountryCode`. - Added `GetCountryCodeByName` API endpoint in `CountryController` within `CountryData.Sample.Web.API.Controllers`. - Updated `CountryData.Sample.Web.API.csproj` to generate XML docs and suppress warning 1591. - Enhanced Swagger configuration in `Program.cs` to include XML comments. - Made `GetCountryData` method in `CountryHelper` virtual for potential overrides. - Added `CountryExtensions` class in `CountryData.Standard` to manage `CountryHelper` and provide country-related methods. - Implemented unit tests for `CountryExtensions` in `CountryExtensionsTests.cs`. - Minor formatting changes in `Currency.cs` and `Regions.cs` to add missing braces. - Changed `app.Run()` to `await app.RunAsync()` in `Program.cs` for async execution.
1 parent e8b183a commit 64f0d45

11 files changed

Lines changed: 263 additions & 6 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ The CountryData.Standard library provides a set of methods that allow you to ret
5757
| [`GetPhoneCodeByCountryShortCode(string shortCode)`](./docs/README.md) | Returns a single country's phone code by its short code. |
5858
| [`GetCountryByPhoneCode(string phoneCode)`](./docs/README.md) | Returns country data for the country associated with the specified phone code. |
5959
| [`GetCurrencyCodesByCountryCode(string shortCode)`](./docs/README.md) | Returns a list of currency codes for a specific country identified by its short code. |
60-
| [`GetCountryByCurrencyCode(string currencyCode)`](./docs/README.md) | Returns a list of countries that use the specified currency code. |
60+
| [`GetCountryByCurrencyCode(string currencyCode)`](./docs/README.md) | Returns a list of countries that use the specified currency code. |
61+
| [`GetCountryCode(string countryName)`](./docs/README.md) | Returns the country code for a specific country name. |
62+
6163

6264

6365
### Initialize the Country data object

docs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,16 @@ Country GetCountryByCurrencyCode(string currencyCode);
186186
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.
187187

188188

189+
### GetCountryCode(string countryName)
190+
191+
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.
192+
193+
Here's the method signature in C#:
194+
195+
```csharp
196+
197+
string GetCountryCode(string countryName);
198+
```
199+
200+
201+

sample/CountryData.Sample.Console/Program.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static void Main()
2626
GetCountryByPhoneCode("+233");
2727
GetCurrencyCodesByCountryCode("US");
2828
GetCountryByCurrencyCode("GHS");
29+
GetCountryCode("Ghana");
2930
}
3031

3132
/// <summary>
@@ -145,5 +146,22 @@ private static void GetCountryByCurrencyCode(string currencyCode)
145146
Console.WriteLine(country.CountryName);
146147
}
147148
}
149+
150+
151+
152+
/// <summary>
153+
/// Retrieves the country code for a given country name and prints it to the console.
154+
/// </summary>
155+
/// <param name="countryName">The name of the country.</param>
156+
private static void GetCountryCode(string countryName)
157+
{
158+
using var manager = new CountryExtensions();
159+
var result = manager.GetCountryCode(countryName);
160+
Console.WriteLine($"Country code for {countryName} is {result} ");
161+
}
162+
163+
164+
165+
148166
}
149167
}

sample/CountryData.Sample.Web.API/Controllers/CountryController.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,32 @@ public IActionResult GetCountryByCurrencyCode([FromQuery] string currencyCode)
186186
return Ok(countryByCurrencyCode);
187187
}
188188

189+
190+
191+
/// <summary>
192+
/// Retrieves the country code for a given country name.
193+
/// </summary>
194+
/// <param name="countryName">The name of the country.</param>
195+
/// <returns>The country code if found; otherwise, a NotFound result.</returns>
196+
[HttpGet("countryCodeByName")]
197+
[ProducesResponseType(typeof(string), 200)]
198+
[ProducesResponseType(404)]
199+
public IActionResult GetCountryCodeByName([FromQuery] string countryName)
200+
201+
202+
{
203+
using var manager = new CountryExtensions();
204+
var countryCode = manager.GetCountryCode(countryName);
205+
if (countryCode == null)
206+
{
207+
return NotFound();
208+
}
209+
return Ok(countryCode);
210+
}
211+
212+
189213
}
190-
}
214+
215+
216+
217+
}

sample/CountryData.Sample.Web.API/CountryData.Sample.Web.API.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
8+
<NoWarn>$(NoWarn);1591</NoWarn>
79
</PropertyGroup>
810

911
<ItemGroup>

sample/CountryData.Sample.Web.API/Program.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1+
using Microsoft.OpenApi.Models;
2+
using System.Reflection;
3+
14
var builder = WebApplication.CreateBuilder(args);
25

36
// Add services to the container.
47

58
builder.Services.AddControllers();
69
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
710
builder.Services.AddEndpointsApiExplorer();
8-
builder.Services.AddSwaggerGen();
11+
builder.Services.AddSwaggerGen(c =>
12+
{
13+
c.SwaggerDoc("v1", new OpenApiInfo
14+
{
15+
Title = " CountryData.Stand",
16+
Version = "v1",
17+
Description = " Sample endpoint for CountryData.Standard "
18+
19+
});
20+
21+
// Set the comments path for the Swagger JSON and UI.
22+
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
23+
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
24+
c.IncludeXmlComments(xmlPath);
25+
});
926
// Register CountryHelper service
1027
builder.Services.AddScoped<CountryData.Standard.CountryHelper>();
28+
1129
var app = builder.Build();
1230

1331
// Configure the HTTP request pipeline.
@@ -23,4 +41,4 @@
2341

2442
app.MapControllers();
2543

26-
app.Run();
44+
await app.RunAsync();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace CountryData.Standard
5+
{
6+
/// <summary>
7+
/// Manages the CountryHelper instance and provides methods for retrieving country-related information.
8+
/// </summary>
9+
public class CountryExtensions : IDisposable
10+
{
11+
private CountryHelper _countryHelper;
12+
private bool _disposed = false;
13+
14+
/// <summary>
15+
/// Constructor to initialize the CountryHelper instance.
16+
/// </summary>
17+
public CountryExtensions()
18+
{
19+
_countryHelper = new CountryHelper();
20+
}
21+
22+
/// <summary>
23+
/// Gets the country code for the specified country name.
24+
/// </summary>
25+
/// <param name="countryName">The name of the country.</param>
26+
/// <returns>The country code if found; otherwise, null.</returns>
27+
public string GetCountryCode(string countryName)
28+
{
29+
if (countryName == null)
30+
{
31+
throw new ArgumentNullException(nameof(countryName));
32+
}
33+
34+
var country = _countryHelper.GetCountryData()
35+
.FirstOrDefault(c => c.CountryName.Equals(countryName, StringComparison.OrdinalIgnoreCase));
36+
37+
return country?.CountryShortCode;
38+
}
39+
40+
/// <summary>
41+
/// Dispose method to clean up resources.
42+
/// </summary>
43+
public void Dispose()
44+
{
45+
Dispose(true);
46+
GC.SuppressFinalize(this);
47+
}
48+
49+
/// <summary>
50+
/// Protected method to dispose resources.
51+
/// </summary>
52+
/// <param name="disposing">Indicates whether the method is called from Dispose or finalizer.</param>
53+
protected virtual void Dispose(bool disposing)
54+
{
55+
if (!_disposed)
56+
{
57+
if (disposing && _countryHelper != null)
58+
{
59+
// Dispose managed resources.
60+
_countryHelper = null;
61+
}
62+
63+
// Dispose unmanaged resources.
64+
65+
_disposed = true;
66+
}
67+
}
68+
69+
/// <summary>
70+
/// Finalizer to ensure resources are cleaned up.
71+
/// </summary>
72+
~CountryExtensions()
73+
{
74+
Dispose(false);
75+
}
76+
}
77+
}

src/CountryData.Standard/CountryHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public CountryHelper()
2424

2525

2626
/// <summary>
27-
///
27+
/// Read
2828
/// </summary>
2929
/// <param name="path"></param>
3030
/// <returns></returns>
@@ -47,7 +47,7 @@ private string GetJsonData(string path)
4747
/// that can be querried by Lambda Expressions
4848
/// </summary>
4949
/// <returns>IEnumerable<Country></returns>
50-
public IEnumerable<Country> GetCountryData()
50+
public virtual IEnumerable<Country> GetCountryData()
5151
{
5252
return _Countries;
5353
}

src/CountryData.Standard/Currency.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public class Currency
44
{
55
public string Code { get; set; }
66
public string Name { get; set; }
7+
78
}
89
}

src/CountryData.Standard/Regions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public class Regions
66
{
77
public String Name { get; set; }
88
public String ShortCode { get; set; }
9+
10+
911
}
1012
}

0 commit comments

Comments
 (0)