Change employee to a left join
This commit is contained in:
@@ -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;
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace BoredCareers.Controllers {
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> 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();
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace BoredCareers.Controllers {
|
||||
}
|
||||
|
||||
public async Task<bool> 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;
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user