188 lines
8.8 KiB
C#
188 lines
8.8 KiB
C#
using BoredCareers.Entities;
|
|
using MySql.Data.MySqlClient;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
|
|
namespace BoredCareers.Services.DatabaseService {
|
|
public partial class DatabaseService {
|
|
|
|
public async Task<JobListing[]> GetJobListingPage(int PageNumber, int CountPerPage) {
|
|
List<JobListing> joblistings = new List<JobListing>();
|
|
using (MySqlConnection connection = GetConnection()) {
|
|
connection.Open();
|
|
string command = @"
|
|
SELECT *
|
|
FROM JobListing
|
|
WHERE IsDeleted = FALSE
|
|
ORDER BY CreatedTime DESC
|
|
LIMIT @PageSize OFFSET @PageNumber;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
|
cmd.Parameters.AddWithValue("@PageSize", CountPerPage);
|
|
cmd.Parameters.AddWithValue("@PageNumber", (PageNumber - 1) * CountPerPage);
|
|
|
|
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
if (reader == null) { break; }
|
|
int _id = reader.GetInt32("ID");
|
|
int _companyid = reader.GetInt32("CompanyID");
|
|
string _title = reader.GetString("Title");
|
|
string _postalcode = reader.GetString("PostalCode");
|
|
string _country = reader.GetString("Country");
|
|
string _state = reader.GetString("StateOrRegion");
|
|
string _city = reader.GetString("City");
|
|
int _salarymin = reader.GetInt32("SalaryMin");
|
|
int _salarymax = reader.GetInt32("SalaryMax");
|
|
string _jobtype = reader.GetString("JobType");
|
|
bool _remote = reader.GetBoolean("Remote");
|
|
string _description = reader.GetString("Description");
|
|
DateTime _createtime = reader.GetDateTime("CreatedTime");
|
|
DateTime _modifiedtime = reader.GetDateTime("ModifiedTime");
|
|
bool _isdeleted = reader.GetBoolean("IsDeleted");
|
|
|
|
joblistings.Add(new JobListing() {
|
|
ID = _id,
|
|
CompanyID = _companyid,
|
|
Title = _title,
|
|
PostalCode = _postalcode,
|
|
Country = _country,
|
|
StateOrRegion = _state,
|
|
City = _city,
|
|
SalaryMin = _salarymin,
|
|
SalaryMax = _salarymax,
|
|
JobType = _jobtype,
|
|
Remote = _remote,
|
|
Description = _description,
|
|
CreatedTime = _createtime,
|
|
ModifiedTime = _modifiedtime,
|
|
IsDeleted = _isdeleted
|
|
});
|
|
}
|
|
}
|
|
}
|
|
return joblistings.ToArray();
|
|
}
|
|
|
|
public async Task<JobListing?> GetJobListing(int JobListingID) {
|
|
JobListing? joblisting = null;
|
|
using (MySqlConnection connection = GetConnection()) {
|
|
connection.Open();
|
|
string command = @"
|
|
SELECT *
|
|
FROM JobListing
|
|
WHERE ID = @ID;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand(command, connection);
|
|
cmd.Parameters.AddWithValue("@ID", JobListingID);
|
|
|
|
using (DbDataReader reader = await cmd.ExecuteReaderAsync()) {
|
|
while (await reader.ReadAsync()) {
|
|
if (reader == null) { break; }
|
|
int _id = reader.GetInt32("ID");
|
|
int _companyid = reader.GetInt32("CompanyID");
|
|
string _title = reader.GetString("Title");
|
|
string _postalcode = reader.GetString("PostalCode");
|
|
string _country = reader.GetString("Country");
|
|
string _state = reader.GetString("StateOrRegion");
|
|
string _city = reader.GetString("City");
|
|
int _salarymin = reader.GetInt32("SalaryMin");
|
|
int _salarymax = reader.GetInt32("SalaryMax");
|
|
string _jobtype = reader.GetString("JobType");
|
|
bool _remote = reader.GetBoolean("Remote");
|
|
string _description = reader.GetString("Description");
|
|
DateTime _createtime = reader.GetDateTime("CreatedTime");
|
|
DateTime _modifiedtime = reader.GetDateTime("ModifiedTime");
|
|
bool _isdeleted = reader.GetBoolean("IsDeleted");
|
|
|
|
joblisting = new JobListing() {
|
|
ID = _id,
|
|
CompanyID = _companyid,
|
|
Title = _title,
|
|
PostalCode = _postalcode,
|
|
Country = _country,
|
|
StateOrRegion = _state,
|
|
City = _city,
|
|
SalaryMin = _salarymin,
|
|
SalaryMax = _salarymax,
|
|
JobType = _jobtype,
|
|
Remote = _remote,
|
|
Description = _description,
|
|
CreatedTime = _createtime,
|
|
ModifiedTime = _modifiedtime,
|
|
IsDeleted = _isdeleted
|
|
};
|
|
}
|
|
}
|
|
}
|
|
return joblisting;
|
|
}
|
|
|
|
public async Task SetJobListing( JobListing jobListing ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
connection.Open();
|
|
|
|
string command = @"
|
|
INSERT INTO JobListing
|
|
(ID,CompanyID,Title,PostalCode,Country,StateOrRegion,City,SalaryMin,SalaryMax,JobType,Remote,Description,CreatedTime,ModifiedTime,IsDeleted)
|
|
VALUES
|
|
(@ID,@CompanyID,@Title,@PostalCode,@Country,@StateOrRegion,@City,@SalaryMin,@SalaryMax,@JobType,@Remote,@Description,@CreatedTime,@ModifiedTime,@IsDeleted);
|
|
ON DUPLICATE KEY UPDATE
|
|
CompanyID = @CompanyID,
|
|
Title = @Title,
|
|
PostalCode = @PostalCode,
|
|
Country = @Country,
|
|
StateOrRegion = @StateOrRegion,
|
|
City = @City,
|
|
SalaryMin = @SalaryMin,
|
|
SalaryMax = @SalaryMax,
|
|
JobType = @JobType,
|
|
Remote = @Remote,
|
|
Description = @Description,
|
|
CreatedTime = @CreatedTime,
|
|
ModifiedTime = @ModifiedTime,
|
|
IsDeleted = @IsDeleted;
|
|
";
|
|
|
|
MySqlCommand cmd = new MySqlCommand( command , connection);
|
|
cmd.Parameters.AddWithValue("@ID", jobListing.ID);
|
|
cmd.Parameters.AddWithValue("@CompanyID", jobListing.CompanyID);
|
|
cmd.Parameters.AddWithValue("@Title", jobListing.Title);
|
|
cmd.Parameters.AddWithValue("@PostalCode", jobListing.PostalCode);
|
|
cmd.Parameters.AddWithValue("@Country", jobListing.Country);
|
|
cmd.Parameters.AddWithValue("@StateOrRegion", jobListing.StateOrRegion);
|
|
cmd.Parameters.AddWithValue("@City", jobListing.City);
|
|
cmd.Parameters.AddWithValue("@SalaryMin", jobListing.SalaryMin);
|
|
cmd.Parameters.AddWithValue("@SalaryMax", jobListing.SalaryMax);
|
|
cmd.Parameters.AddWithValue("@JobType", jobListing.JobType);
|
|
cmd.Parameters.AddWithValue("@Remote", jobListing.Remote);
|
|
cmd.Parameters.AddWithValue("@Description", jobListing.Description);
|
|
cmd.Parameters.AddWithValue("@CreatedTime", jobListing.CreatedTime.ToUniversalTime());
|
|
cmd.Parameters.AddWithValue("@ModifiedTime", jobListing.ModifiedTime.ToUniversalTime());
|
|
cmd.Parameters.AddWithValue("@IsDeleted", jobListing.IsDeleted);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
|
|
public async Task DeleteJobListing( int JobListingID ) {
|
|
using( MySqlConnection connection = GetConnection() ) {
|
|
MySqlCommand cmd;
|
|
connection.Open();
|
|
|
|
string command = @"
|
|
UPDATE JobListing
|
|
SET IsDeleted = TRUE
|
|
WHERE ID = @ID;
|
|
";
|
|
cmd = new MySqlCommand( command, connection );
|
|
cmd.Parameters.AddWithValue("@ID", JobListingID);
|
|
|
|
await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|