Query List for Countries
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user