diff --git a/src/Server/Controllers/LocationController.cs b/src/Server/Controllers/LocationController.cs index 90ab731..f469a00 100644 --- a/src/Server/Controllers/LocationController.cs +++ b/src/Server/Controllers/LocationController.cs @@ -18,5 +18,14 @@ namespace BoredCareers.Controllers { return NotFound("Not logged in"); } + [HttpGet("Distinct")] + public async Task GetCountries() { + if (isLoggedIn()) { + string[] places = await _databaseService.GetDistinctCountries(); + return Ok(places.ToArray()); + } + return NotFound("Not logged in"); + } + } } diff --git a/src/Server/Services/DatabaseService/Location.cs b/src/Server/Services/DatabaseService/Location.cs index 16efe83..4468acc 100644 --- a/src/Server/Services/DatabaseService/Location.cs +++ b/src/Server/Services/DatabaseService/Location.cs @@ -62,5 +62,23 @@ namespace BoredCareers.Services.DatabaseService { return closePostalCodes.ToArray(); } + public async Task GetDistinctCountries() { + List closePostalCodes = new List(); + 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(); + } + } }