From fb7e37bfb02ca0af8ba7e12c86d19aa1ae680866 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Sun, 20 Jul 2025 21:14:39 -0700 Subject: [PATCH] Change employee to a left join --- src/Client/src/app/models/Company.ts | 2 +- src/Server/Controllers/CompanyController.cs | 4 + src/Server/Controllers/EmployeeController.cs | 4 +- .../Controllers/MistoxControllerBase.cs | 2 +- src/Server/Entities/Company.cs | 2 +- .../Services/DatabaseService/Employee.cs | 89 +++++++++++++++++-- 6 files changed, 91 insertions(+), 12 deletions(-) diff --git a/src/Client/src/app/models/Company.ts b/src/Client/src/app/models/Company.ts index 684ae1e..4ceca30 100644 --- a/src/Client/src/app/models/Company.ts +++ b/src/Client/src/app/models/Company.ts @@ -16,5 +16,5 @@ export class Company { export class Employee { public id: number = 0; public accountID: number = 0; - public companyID: number = 0; + public company: Company = new Company; } \ No newline at end of file diff --git a/src/Server/Controllers/CompanyController.cs b/src/Server/Controllers/CompanyController.cs index 2424d44..601f869 100644 --- a/src/Server/Controllers/CompanyController.cs +++ b/src/Server/Controllers/CompanyController.cs @@ -29,6 +29,10 @@ namespace BoredCareers.Controllers { Company? test = await _databaseService.GetCompany(company.ID); if (test == null) { await _databaseService.SetCompany(company); + await _databaseService.SetEmployee(new Employee() { + AccountID = getLoggedInUserID(), + Company = company + }); return Ok(); } return NotFound("The company already exists"); diff --git a/src/Server/Controllers/EmployeeController.cs b/src/Server/Controllers/EmployeeController.cs index debdc6d..b00d61a 100644 --- a/src/Server/Controllers/EmployeeController.cs +++ b/src/Server/Controllers/EmployeeController.cs @@ -34,7 +34,7 @@ namespace BoredCareers.Controllers { [HttpPost] public async Task SetEmployee([FromBody] Employee employee) { if (isLoggedIn()) { - if (await isLoggedInUserEmployeeOf(employee.CompanyID)) { + if (await isLoggedInUserEmployeeOf(employee.Company.ID)) { await _databaseService.SetEmployee(employee); return Ok(); } @@ -48,7 +48,7 @@ namespace BoredCareers.Controllers { if (isLoggedIn()) { Employee? employee = await _databaseService.GetEmployee(EmployeeID); if (employee != null) { - if (await isLoggedInUserEmployeeOf(employee.CompanyID)) { + if (await isLoggedInUserEmployeeOf(employee.Company.ID)) { await _databaseService.DeleteEmployee(EmployeeID); return Ok(); } diff --git a/src/Server/Controllers/MistoxControllerBase.cs b/src/Server/Controllers/MistoxControllerBase.cs index 0084227..1253b5c 100644 --- a/src/Server/Controllers/MistoxControllerBase.cs +++ b/src/Server/Controllers/MistoxControllerBase.cs @@ -36,7 +36,7 @@ namespace BoredCareers.Controllers { } public async Task isLoggedInUserEmployeeOf(int CompanyID) { - Employee[] employees = await _databaseService.GetEmployees(CompanyID); + Employee[] employees = await _databaseService.GetEmployeesFromCompany(CompanyID); foreach (Employee cur in employees) { if (getLoggedInUserID() == cur.AccountID) { return true; diff --git a/src/Server/Entities/Company.cs b/src/Server/Entities/Company.cs index e0a5a97..dfa85eb 100644 --- a/src/Server/Entities/Company.cs +++ b/src/Server/Entities/Company.cs @@ -18,7 +18,7 @@ namespace BoredCareers.Entities { public class Employee { public int ID { get; set; } // PK public int AccountID { get; set; } // FK - public int CompanyID { get; set; } // FK + public Company Company { get; set; } = new Company(); // FK } } \ No newline at end of file diff --git a/src/Server/Services/DatabaseService/Employee.cs b/src/Server/Services/DatabaseService/Employee.cs index ad7e2b9..f37fb83 100644 --- a/src/Server/Services/DatabaseService/Employee.cs +++ b/src/Server/Services/DatabaseService/Employee.cs @@ -13,7 +13,8 @@ namespace BoredCareers.Services.DatabaseService { string command = @" SELECT * FROM Employee - WHERE ID = @ID; + LEFT JOIN Company ON Employee.CompanyID = Company.ID + WHERE Employee.ID = @ID; "; MySqlCommand cmd = new MySqlCommand(command, connection); @@ -25,11 +26,35 @@ namespace BoredCareers.Services.DatabaseService { int _id = reader.GetInt32("ID"); int _accountid = reader.GetInt32("AccountID"); int _companyid = reader.GetInt32("CompanyID"); + 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" ); employee = new Employee() { ID = _id, AccountID = _accountid, - CompanyID = _companyid + Company = new Company { + ID = _companyid, + Name = _name, + Email = _email, + EmailVerified = _emailVerified, + WebsiteURL = _websiteurl, + LogoURL = _logourl, + Phone = _phone, + PostalCode = _postalcode, + Country = _country, + StateOrRegion = _state, + City = _city, + Description = _description + } }; } } @@ -44,7 +69,8 @@ namespace BoredCareers.Services.DatabaseService { string command = @" SELECT * FROM Employee - WHERE CompanyID = @CompanyID; + LEFT JOIN Company ON Employee.CompanyID = Company.ID + WHERE Employee.CompanyID = @ID; "; MySqlCommand cmd = new MySqlCommand(command, connection); @@ -56,11 +82,35 @@ namespace BoredCareers.Services.DatabaseService { int _id = reader.GetInt32("ID"); int _accountid = reader.GetInt32("AccountID"); int _companyid = reader.GetInt32("CompanyID"); + 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" ); employees.Add(new Employee() { ID = _id, AccountID = _accountid, - CompanyID = _companyid + Company = new Company { + ID = _companyid, + Name = _name, + Email = _email, + EmailVerified = _emailVerified, + WebsiteURL = _websiteurl, + LogoURL = _logourl, + Phone = _phone, + PostalCode = _postalcode, + Country = _country, + StateOrRegion = _state, + City = _city, + Description = _description + } }); } } @@ -75,7 +125,8 @@ namespace BoredCareers.Services.DatabaseService { string command = @" SELECT * FROM Employee - WHERE AccountID = @AccountID; + LEFT JOIN Company ON Employee.CompanyID = Company.ID + WHERE Employee.AccountID = @AccountID; "; MySqlCommand cmd = new MySqlCommand(command, connection); @@ -87,11 +138,35 @@ namespace BoredCareers.Services.DatabaseService { int _id = reader.GetInt32("ID"); int _accountid = reader.GetInt32("AccountID"); int _companyid = reader.GetInt32("CompanyID"); + 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" ); employees.Add(new Employee() { ID = _id, AccountID = _accountid, - CompanyID = _companyid + Company = new Company { + ID = _companyid, + Name = _name, + Email = _email, + EmailVerified = _emailVerified, + WebsiteURL = _websiteurl, + LogoURL = _logourl, + Phone = _phone, + PostalCode = _postalcode, + Country = _country, + StateOrRegion = _state, + City = _city, + Description = _description + } }); } } @@ -116,7 +191,7 @@ namespace BoredCareers.Services.DatabaseService { MySqlCommand cmd = new MySqlCommand(command, connection); cmd.Parameters.AddWithValue("@ID", employee.ID); cmd.Parameters.AddWithValue("@AccountID", employee.AccountID); - cmd.Parameters.AddWithValue("@CompanyID", employee.CompanyID); + cmd.Parameters.AddWithValue("@CompanyID", employee.Company.ID); await cmd.ExecuteNonQueryAsync(); }