diff --git a/database/mistox.sql b/database/mistox.sql index bb33233..f428885 100755 --- a/database/mistox.sql +++ b/database/mistox.sql @@ -149,14 +149,14 @@ CREATE TABLE IF NOT EXISTS `Company` ( `Name` varchar(100) DEFAULT NULL, `Email` varchar(255) DEFAULT NULL, `EmailVerified` boolean DEFAULT 0, - `WebsiteURL` VARCHAR(255) DEFAULT NULL, - `LogoURL` VARCHAR(2048) DEFAULT NULL, - `Phone` VARCHAR(20) DEFAULT NULL, + `WebsiteURL` varchar(255) DEFAULT NULL, + `LogoURL` varchar(2048) DEFAULT NULL, + `Phone` varchar(20) DEFAULT NULL, `PostalCode` varchar(20) NOT NULL, - `Country` CHAR(2) NOT NULL, + `Country` char(2) NOT NULL, `StateOrRegion` varchar(100) NOT NULL, - `City` VARCHAR(100), - `Description` TEXT, + `City` varchar(100), + `Description` text, PRIMARY KEY (`ID`) ) AUTO_INCREMENT=1; @@ -180,6 +180,9 @@ CREATE TABLE IF NOT EXISTS `JobListing` ( `JobType` varchar(20) NOT NULL, `Remote` boolean DEFAULT 0, `Description` text NOT NULL, + `CreatedTime` datetime Default NULL, + `ModifiedTime` datetime DEFAULT NULL, + `IsDeleted` boolean Default 0, PRIMARY KEY (`ID`), FOREIGN KEY (`CompanyID`) REFERENCES `Company`(`ID`) ON DELETE CASCADE ) AUTO_INCREMENT=1; diff --git a/src/Server/Entities/JobListing.cs b/src/Server/Entities/JobListing.cs index 3c02654..f88744e 100644 --- a/src/Server/Entities/JobListing.cs +++ b/src/Server/Entities/JobListing.cs @@ -13,6 +13,9 @@ namespace BoredCareers.Entities { public string JobType { get; set; } = ""; public bool Remote { get; set; } = false; public string Description { get; set; } = ""; + public DateTime CreatedTime { get; set; } + public DateTime ModifiedTime { get; set; } + public bool IsDeleted { get; set; } = false; } - + } \ No newline at end of file diff --git a/src/Server/Services/DatabaseService/JobListing.cs b/src/Server/Services/DatabaseService/JobListing.cs new file mode 100644 index 0000000..d16a3c1 --- /dev/null +++ b/src/Server/Services/DatabaseService/JobListing.cs @@ -0,0 +1,186 @@ +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 GetJobListingPage(int PageNumber, int CountPerPage) { + List joblistings = new List(); + using (MySqlConnection connection = GetConnection()) { + connection.Open(); + string command = @" + SELECT * + FROM JobListing + ORDER BY ModifiedTime <---- Need to update this + 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 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); + cmd.Parameters.AddWithValue("@ModifiedTime", jobListing.ModifiedTime); + 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(); + } + } + + } +}