Change employee to a left join

This commit is contained in:
2025-07-20 21:14:39 -07:00
parent a5573dce4e
commit fb7e37bfb0
6 changed files with 91 additions and 12 deletions
+1 -1
View File
@@ -16,5 +16,5 @@ export class Company {
export class Employee { export class Employee {
public id: number = 0; public id: number = 0;
public accountID: number = 0; public accountID: number = 0;
public companyID: number = 0; public company: Company = new Company;
} }
@@ -29,6 +29,10 @@ namespace BoredCareers.Controllers {
Company? test = await _databaseService.GetCompany(company.ID); Company? test = await _databaseService.GetCompany(company.ID);
if (test == null) { if (test == null) {
await _databaseService.SetCompany(company); await _databaseService.SetCompany(company);
await _databaseService.SetEmployee(new Employee() {
AccountID = getLoggedInUserID(),
Company = company
});
return Ok(); return Ok();
} }
return NotFound("The company already exists"); return NotFound("The company already exists");
+2 -2
View File
@@ -34,7 +34,7 @@ namespace BoredCareers.Controllers {
[HttpPost] [HttpPost]
public async Task<IActionResult> SetEmployee([FromBody] Employee employee) { public async Task<IActionResult> SetEmployee([FromBody] Employee employee) {
if (isLoggedIn()) { if (isLoggedIn()) {
if (await isLoggedInUserEmployeeOf(employee.CompanyID)) { if (await isLoggedInUserEmployeeOf(employee.Company.ID)) {
await _databaseService.SetEmployee(employee); await _databaseService.SetEmployee(employee);
return Ok(); return Ok();
} }
@@ -48,7 +48,7 @@ namespace BoredCareers.Controllers {
if (isLoggedIn()) { if (isLoggedIn()) {
Employee? employee = await _databaseService.GetEmployee(EmployeeID); Employee? employee = await _databaseService.GetEmployee(EmployeeID);
if (employee != null) { if (employee != null) {
if (await isLoggedInUserEmployeeOf(employee.CompanyID)) { if (await isLoggedInUserEmployeeOf(employee.Company.ID)) {
await _databaseService.DeleteEmployee(EmployeeID); await _databaseService.DeleteEmployee(EmployeeID);
return Ok(); return Ok();
} }
@@ -36,7 +36,7 @@ namespace BoredCareers.Controllers {
} }
public async Task<bool> isLoggedInUserEmployeeOf(int CompanyID) { public async Task<bool> isLoggedInUserEmployeeOf(int CompanyID) {
Employee[] employees = await _databaseService.GetEmployees(CompanyID); Employee[] employees = await _databaseService.GetEmployeesFromCompany(CompanyID);
foreach (Employee cur in employees) { foreach (Employee cur in employees) {
if (getLoggedInUserID() == cur.AccountID) { if (getLoggedInUserID() == cur.AccountID) {
return true; return true;
+1 -1
View File
@@ -18,7 +18,7 @@ namespace BoredCareers.Entities {
public class Employee { public class Employee {
public int ID { get; set; } // PK public int ID { get; set; } // PK
public int AccountID { get; set; } // FK public int AccountID { get; set; } // FK
public int CompanyID { get; set; } // FK public Company Company { get; set; } = new Company(); // FK
} }
} }
@@ -13,7 +13,8 @@ namespace BoredCareers.Services.DatabaseService {
string command = @" string command = @"
SELECT * SELECT *
FROM Employee FROM Employee
WHERE ID = @ID; LEFT JOIN Company ON Employee.CompanyID = Company.ID
WHERE Employee.ID = @ID;
"; ";
MySqlCommand cmd = new MySqlCommand(command, connection); MySqlCommand cmd = new MySqlCommand(command, connection);
@@ -25,11 +26,35 @@ namespace BoredCareers.Services.DatabaseService {
int _id = reader.GetInt32("ID"); int _id = reader.GetInt32("ID");
int _accountid = reader.GetInt32("AccountID"); int _accountid = reader.GetInt32("AccountID");
int _companyid = reader.GetInt32("CompanyID"); 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() { employee = new Employee() {
ID = _id, ID = _id,
AccountID = _accountid, 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 = @" string command = @"
SELECT * SELECT *
FROM Employee FROM Employee
WHERE CompanyID = @CompanyID; LEFT JOIN Company ON Employee.CompanyID = Company.ID
WHERE Employee.CompanyID = @ID;
"; ";
MySqlCommand cmd = new MySqlCommand(command, connection); MySqlCommand cmd = new MySqlCommand(command, connection);
@@ -56,11 +82,35 @@ namespace BoredCareers.Services.DatabaseService {
int _id = reader.GetInt32("ID"); int _id = reader.GetInt32("ID");
int _accountid = reader.GetInt32("AccountID"); int _accountid = reader.GetInt32("AccountID");
int _companyid = reader.GetInt32("CompanyID"); 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() { employees.Add(new Employee() {
ID = _id, ID = _id,
AccountID = _accountid, 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 = @" string command = @"
SELECT * SELECT *
FROM Employee FROM Employee
WHERE AccountID = @AccountID; LEFT JOIN Company ON Employee.CompanyID = Company.ID
WHERE Employee.AccountID = @AccountID;
"; ";
MySqlCommand cmd = new MySqlCommand(command, connection); MySqlCommand cmd = new MySqlCommand(command, connection);
@@ -87,11 +138,35 @@ namespace BoredCareers.Services.DatabaseService {
int _id = reader.GetInt32("ID"); int _id = reader.GetInt32("ID");
int _accountid = reader.GetInt32("AccountID"); int _accountid = reader.GetInt32("AccountID");
int _companyid = reader.GetInt32("CompanyID"); 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() { employees.Add(new Employee() {
ID = _id, ID = _id,
AccountID = _accountid, 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); MySqlCommand cmd = new MySqlCommand(command, connection);
cmd.Parameters.AddWithValue("@ID", employee.ID); cmd.Parameters.AddWithValue("@ID", employee.ID);
cmd.Parameters.AddWithValue("@AccountID", employee.AccountID); cmd.Parameters.AddWithValue("@AccountID", employee.AccountID);
cmd.Parameters.AddWithValue("@CompanyID", employee.CompanyID); cmd.Parameters.AddWithValue("@CompanyID", employee.Company.ID);
await cmd.ExecuteNonQueryAsync(); await cmd.ExecuteNonQueryAsync();
} }