Query List for Countries

This commit is contained in:
2025-10-13 20:54:05 -07:00
parent 50a9678669
commit 436ae6d543
2 changed files with 27 additions and 0 deletions
@@ -18,5 +18,14 @@ namespace BoredCareers.Controllers {
return NotFound("Not logged in");
}
[HttpGet("Distinct")]
public async Task<IActionResult> GetCountries() {
if (isLoggedIn()) {
string[] places = await _databaseService.GetDistinctCountries();
return Ok(places.ToArray());
}
return NotFound("Not logged in");
}
}
}
@@ -62,5 +62,23 @@ namespace BoredCareers.Services.DatabaseService {
return closePostalCodes.ToArray();
}
public async Task<string[]> GetDistinctCountries() {
List<string> closePostalCodes = new List<string>();
using (MySqlConnection connection = GetConnection()) {
await connection.OpenAsync();
string command = "SELECT DISTINCT(CountryCode) FROM PostalCodes;";
MySqlCommand cmd = new MySqlCommand(command, connection);
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
while (await reader.ReadAsync()) {
string _city = reader.GetString("CountryCode");
closePostalCodes.Add(_city);
}
}
}
return closePostalCodes.ToArray();
}
}
}