diff --git a/database/mistox.sql b/database/mistox.sql index 9e77991..bb33233 100755 --- a/database/mistox.sql +++ b/database/mistox.sql @@ -160,6 +160,13 @@ CREATE TABLE IF NOT EXISTS `Company` ( PRIMARY KEY (`ID`) ) AUTO_INCREMENT=1; +CREATE TABLE IF NOT EXISTS `CompanyUser` ( + `ID` int NOT NULL AUTO_INCREMENT, + `AccountID` int NOT NULL, + `CompanyID` int NOT NULL, + PRIMARY KEY (`ID`) +) AUTO_INCREMENT=1; + CREATE TABLE IF NOT EXISTS `JobListing` ( `ID` int NOT NULL AUTO_INCREMENT, `CompanyID` int NOT NULL, diff --git a/src/Server/Services/DatabaseService/Company.cs b/src/Server/Services/DatabaseService/Company.cs new file mode 100644 index 0000000..2eafd16 --- /dev/null +++ b/src/Server/Services/DatabaseService/Company.cs @@ -0,0 +1,115 @@ +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 GetCompany( int CompanyID ) { + Company? company = null; + using( MySqlConnection connection = GetConnection() ) { + connection.Open(); + string command = @" + SELECT * + FROM Company + WHERE ID = @ID; + "; + + MySqlCommand cmd = new MySqlCommand(command, connection); + cmd.Parameters.AddWithValue("@ID", CompanyID); + + using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) { + while( await reader.ReadAsync() ) { + if( reader == null ) { break; } + int _id = reader.GetInt32("ID"); + string _name = reader.GetString("Name"); + string _email = reader.GetString("Email"); + bool _emailVerified = reader.GetBoolean("EmailVerified"); + string _websiteurl = reader.GetString("WebsiteURL"); + string _logourl = reader.GetString( "LogoURL" ); + string _phone = reader.GetString( "Phone" ); + string _postalcode = reader.GetString( "PostalCode" ); + string _country = reader.GetString( "Country" ); + string _state = reader.GetString( "StateOrRegion" ); + string _city = reader.GetString( "City" ); + string _description = reader.GetString( "Description" ); + + company = new Company() { + ID = _id, + Name = _name, + Email = _email, + EmailVerified = _emailVerified, + WebsiteURL = _websiteurl, + LogoURL = _logourl, + Phone = _phone, + PostalCode = _postalcode, + Country = _country, + StateOrRegion = _state, + City = _city, + Description = _description + }; + } + } + } + return company; + } + + public async Task SetCompany( Company company ) { + using( MySqlConnection connection = GetConnection() ) { + connection.Open(); + + string command = @" + INSERT INTO Account + (ID,Name,Email,EmailVerified,WebsiteURL,LogoURL,Phone,PostalCode,Country,StateOrRegion,City,Description) + VALUES + (@ID,@Name,@Email,@EmailVerified,@WebsiteURL,@LogoURL,@Phone,@PostalCode,@Country,@StateOrRegion,@City,@Description); + ON DUPLICATE KEY UPDATE + Name = @Name, + Email = @Email, + EmailVerified = @EmailVerified, + WebsiteURL = @WebsiteURL, + LogoURL = @LogoURL, + Phone = @Phone, + PostalCode = @PostalCode, + Country = @Country, + StateOrRegion = @StateOrRegion; + City = @City, + Description = @Description; + "; + + MySqlCommand cmd = new MySqlCommand( command , connection); + cmd.Parameters.AddWithValue("@ID", company.ID); + cmd.Parameters.AddWithValue("@Name", company.Name); + cmd.Parameters.AddWithValue("@Email", company.Email); + cmd.Parameters.AddWithValue("@EmailVerified", company.EmailVerified); + cmd.Parameters.AddWithValue("@WebsiteURL", company.WebsiteURL); + cmd.Parameters.AddWithValue("@LogoURL", company.LogoURL); + cmd.Parameters.AddWithValue("@Phone", company.Phone); + cmd.Parameters.AddWithValue("@PostalCode", company.PostalCode); + cmd.Parameters.AddWithValue("@Country", company.Country); + cmd.Parameters.AddWithValue("@StateOrRegion", company.StateOrRegion); + cmd.Parameters.AddWithValue("@City", company.City); + cmd.Parameters.AddWithValue("@Description", company.Description); + + await cmd.ExecuteNonQueryAsync(); + } + } + + public async Task DeleteCompany( int CompanyID ) { + using( MySqlConnection connection = GetConnection() ) { + MySqlCommand cmd; + connection.Open(); + + string command = @" + DELETE FROM Company WHERE ID = @ID; + "; + cmd = new MySqlCommand( command, connection ); + cmd.Parameters.AddWithValue("@ID", CompanyID); + + await cmd.ExecuteNonQueryAsync(); + } + } + + } +}