Start work on filtering logic
This commit is contained in:
@@ -7,21 +7,73 @@ using System.Data.Common;
|
||||
namespace BoredCareers.Services.DatabaseService {
|
||||
public partial class DatabaseService {
|
||||
|
||||
public async Task<JobListing[]> GetJobListingPage(int PageNumber, int CountPerPage) {
|
||||
public async Task<JobListing[]> GetJobListingPage(int PageNumber, int CountPerPage, string[]? PostalCodes = null, string? CountryCode = null, string? JobType = null, bool? Remote = null, int? SalaryMin = null, int? SalaryMax = null ) {
|
||||
List<JobListing> joblistings = new List<JobListing>();
|
||||
using (MySqlConnection connection = GetConnection()) {
|
||||
await connection.OpenAsync();
|
||||
string command = @"
|
||||
SELECT *
|
||||
FROM JobListing
|
||||
WHERE IsDeleted = FALSE
|
||||
ORDER BY CreatedTime DESC
|
||||
LIMIT @PageSize OFFSET @PageNumber;
|
||||
";
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(command, connection);
|
||||
string select = "SELECT * FROM JobListing";
|
||||
string order = " ORDER BY CreatedTime DESC";
|
||||
string limit = " LIMIT @PageSize OFFSET @PageNumber;";
|
||||
|
||||
List<string> Filters = new List<string>();
|
||||
List<string> Parameters = new List<string>();
|
||||
List<string> ParameterName = new List<string>();
|
||||
|
||||
if (PostalCodes != null) {
|
||||
for (int i = 0; i < PostalCodes.Length; i++) {
|
||||
Filters.Add("PostalCode");
|
||||
Parameters.Add(PostalCodes[i]);
|
||||
ParameterName.Add("@PostalCode" + i);
|
||||
}
|
||||
}
|
||||
|
||||
if (CountryCode != null) {
|
||||
Filters.Add("Country");
|
||||
Parameters.Add(CountryCode);
|
||||
ParameterName.Add("@CountryCode");
|
||||
}
|
||||
|
||||
if (JobType != null) {
|
||||
Filters.Add("JobType");
|
||||
Parameters.Add(JobType);
|
||||
ParameterName.Add("@JobType");
|
||||
}
|
||||
|
||||
if (Remote != null) {
|
||||
Filters.Add("Remote");
|
||||
Parameters.Add(Remote.Value.ToString());
|
||||
ParameterName.Add("@Remote");
|
||||
}
|
||||
|
||||
if (SalaryMin != null) {
|
||||
Filters.Add("SalaryMin");
|
||||
Parameters.Add(SalaryMin.Value.ToString());
|
||||
ParameterName.Add("@SalaryMin");
|
||||
}
|
||||
|
||||
if (SalaryMax != null) {
|
||||
Filters.Add("SalaryMax");
|
||||
Parameters.Add(SalaryMax.Value.ToString());
|
||||
ParameterName.Add("@SalaryMax");
|
||||
}
|
||||
|
||||
string filter = " WHERE IsDeleted = False";
|
||||
for (int i = 0; i < Filters.Count; i++) {
|
||||
if (Filters[i] == "PostalCode" && i != 0) {
|
||||
filter += " OR ";
|
||||
} else {
|
||||
filter += " AND ";
|
||||
}
|
||||
filter += Filters[i] + " = " + ParameterName[i];
|
||||
}
|
||||
|
||||
MySqlCommand cmd = new MySqlCommand(select + filter + order + limit, connection);
|
||||
cmd.Parameters.AddWithValue("@PageSize", CountPerPage);
|
||||
cmd.Parameters.AddWithValue("@PageNumber", (PageNumber - 1) * CountPerPage);
|
||||
for (int i = 0; i < Filters.Count; i++) {
|
||||
cmd.Parameters.AddWithValue( ParameterName[i], Parameters[i] );
|
||||
}
|
||||
|
||||
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
||||
while (await reader.ReadAsync()) {
|
||||
|
||||
@@ -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