Start work on filtering logic
This commit is contained in:
@@ -7,64 +7,35 @@ using System.Text;
|
||||
namespace BoredCareers.Services.DatabaseService {
|
||||
public partial class DatabaseService {
|
||||
|
||||
public async Task<Location?> GetLocation(string PostalCode, string CountryCode) {
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
await connection.OpenAsync();
|
||||
string command = @"
|
||||
SELECT PostalCode, CountryCode, Latitude, Longitude, City
|
||||
FROM PostalCodes
|
||||
WHERE PostalCode = @PostalCode
|
||||
AND CountryCode = @CountryCode;
|
||||
";
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||
cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
|
||||
cmd.Parameters.AddWithValue("@CountryCode", CountryCode);
|
||||
|
||||
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
||||
while (await reader.ReadAsync()) {
|
||||
string _city = reader.GetString("City");
|
||||
string _postalCode = reader.GetString("PostalCode");
|
||||
string _countryCode = reader.GetString("CountryCode");
|
||||
float _latitude = reader.GetFloat("Latitude");
|
||||
float _longitude = reader.GetFloat("Longitude");
|
||||
|
||||
return new Location() {
|
||||
City = _city,
|
||||
PostalCode = _postalCode,
|
||||
CountryCode = _countryCode,
|
||||
Latitude = _latitude,
|
||||
Longitude = _longitude
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<Location[]> GetNearbyLocations(float Latitude, float Longitude, string CountryCode, float MaxDistanceKm) {
|
||||
public async Task<Location[]> GetNearbyLocations(string PostalCode, string CountryCode, float MaxDistanceKm) {
|
||||
List<Location> closePostalCodes = new List<Location>();
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
await connection.OpenAsync();
|
||||
string command = @"
|
||||
SELECT PostalCode, CountryCode, Latitude, Longitude, City, (
|
||||
6371 * acos(
|
||||
cos(radians(@Latitude)) *
|
||||
cos(radians(Latitude)) *
|
||||
cos(radians(Longitude) - radians(@Longitude)) +
|
||||
sin(radians(@Latitude)) *
|
||||
sin(radians(Latitude))
|
||||
)
|
||||
) AS distance_km
|
||||
FROM PostalCodes
|
||||
WHERE countrycode = @CountryCode
|
||||
SELECT
|
||||
pc2.PostalCode,
|
||||
pc2.CountryCode,
|
||||
pc2.Latitude,
|
||||
pc2.Longitude,
|
||||
pc2.City,
|
||||
(
|
||||
6371 * acos(
|
||||
cos(radians(pc1.Latitude)) *
|
||||
cos(radians(pc2.Latitude)) *
|
||||
cos(radians(pc2.Longitude) - radians(pc1.Longitude)) +
|
||||
sin(radians(pc1.Latitude)) *
|
||||
sin(radians(pc2.Latitude))
|
||||
)
|
||||
) AS distance_km
|
||||
FROM PostalCodes pc1
|
||||
JOIN PostalCodes pc2 ON pc2.CountryCode = 'us'
|
||||
WHERE pc1.PostalCode = @PostalCode AND pc1.CountryCode = @CountryCode
|
||||
HAVING distance_km <= @MaxDistanceKm
|
||||
ORDER BY distance_km;
|
||||
";
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||
cmd.Parameters.AddWithValue("@Latitude", Latitude);
|
||||
cmd.Parameters.AddWithValue("@Longitude", Longitude);
|
||||
cmd.Parameters.AddWithValue("@PostalCode", PostalCode);
|
||||
cmd.Parameters.AddWithValue("@CountryCode", CountryCode);
|
||||
cmd.Parameters.AddWithValue("@MaxDistanceKm", MaxDistanceKm);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user