From a88bc6514908c7770627b2eee80e4d416a1ed345 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Sun, 20 Jul 2025 20:11:30 -0700 Subject: [PATCH] Fix employee drivers --- src/Server/Controllers/EmployeeController.cs | 64 ++++++++++++++++ .../Services/DatabaseService/Employee.cs | 74 +++++++++++++++++-- 2 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 src/Server/Controllers/EmployeeController.cs diff --git a/src/Server/Controllers/EmployeeController.cs b/src/Server/Controllers/EmployeeController.cs new file mode 100644 index 0000000..debdc6d --- /dev/null +++ b/src/Server/Controllers/EmployeeController.cs @@ -0,0 +1,64 @@ +using Microsoft.AspNetCore.Mvc; +using BoredCareers.Services.DatabaseService; +using BoredCareers.Entities; +using System.Web.Http; + +namespace BoredCareers.Controllers { + [ApiController] + [Route("api/employee")] + public class EmployeeController : MistoxControllerBase { + + public EmployeeController(DatabaseService db) : base(db) {} + + [HttpGet("{CompanyID}")] + public async Task GetEmployees([FromRoute] int CompanyID) { + if (isLoggedIn()) { + if (await isLoggedInUserEmployeeOf(CompanyID)) { + Employee[] employees = await _databaseService.GetEmployeesFromCompany(CompanyID); + return Ok(employees); + } + return NotFound("You are not an employee of company"); + } + return NotFound("Not logged in"); + } + + [HttpGet] + public async Task GetEmployee() { + if (isLoggedIn()) { + Employee[] employees = await _databaseService.GetEmployeesFromAccount(getLoggedInUserID()); + return Ok(employees); + } + return NotFound("Not logged in"); + } + + [HttpPost] + public async Task SetEmployee([FromBody] Employee employee) { + if (isLoggedIn()) { + if (await isLoggedInUserEmployeeOf(employee.CompanyID)) { + await _databaseService.SetEmployee(employee); + return Ok(); + } + return NotFound("You are not an employee of company"); + } + return NotFound("Not logged in"); + } + + [HttpDelete] + public async Task DeleteEmployee(int EmployeeID) { + if (isLoggedIn()) { + Employee? employee = await _databaseService.GetEmployee(EmployeeID); + if (employee != null) { + if (await isLoggedInUserEmployeeOf(employee.CompanyID)) { + await _databaseService.DeleteEmployee(EmployeeID); + return Ok(); + } + return NotFound("You are not an employee of company"); + } + return NotFound("Employee not found"); + } + return NotFound("Not logged in"); + } + + } + +} diff --git a/src/Server/Services/DatabaseService/Employee.cs b/src/Server/Services/DatabaseService/Employee.cs index f31261d..ad7e2b9 100644 --- a/src/Server/Services/DatabaseService/Employee.cs +++ b/src/Server/Services/DatabaseService/Employee.cs @@ -6,8 +6,8 @@ using System.Data.Common; namespace BoredCareers.Services.DatabaseService { public partial class DatabaseService { - public async Task GetEmployees( int CompanyID ) { - List employees = new List(); + public async Task GetEmployee( int EmployeeID ) { + Employee? employee = null; using( MySqlConnection connection = GetConnection() ) { connection.Open(); string command = @" @@ -17,7 +17,7 @@ namespace BoredCareers.Services.DatabaseService { "; MySqlCommand cmd = new MySqlCommand(command, connection); - cmd.Parameters.AddWithValue("@ID", CompanyID); + cmd.Parameters.AddWithValue("@ID", EmployeeID); using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) { while( await reader.ReadAsync() ) { @@ -26,6 +26,68 @@ namespace BoredCareers.Services.DatabaseService { int _accountid = reader.GetInt32("AccountID"); int _companyid = reader.GetInt32("CompanyID"); + employee = new Employee() { + ID = _id, + AccountID = _accountid, + CompanyID = _companyid + }; + } + } + } + return employee; + } + + public async Task GetEmployeesFromCompany(int CompanyID) { + List employees = new List(); + using (MySqlConnection connection = GetConnection()) { + connection.Open(); + string command = @" + SELECT * + FROM Employee + WHERE CompanyID = @CompanyID; + "; + + MySqlCommand cmd = new MySqlCommand(command, connection); + cmd.Parameters.AddWithValue("@CompanyID", CompanyID); + + using (DbDataReader reader = await cmd.ExecuteReaderAsync()) { + while (await reader.ReadAsync()) { + if (reader == null) { break; } + int _id = reader.GetInt32("ID"); + int _accountid = reader.GetInt32("AccountID"); + int _companyid = reader.GetInt32("CompanyID"); + + employees.Add(new Employee() { + ID = _id, + AccountID = _accountid, + CompanyID = _companyid + }); + } + } + } + return employees.ToArray(); + } + + public async Task GetEmployeesFromAccount(int AccountID) { + List employees = new List(); + using (MySqlConnection connection = GetConnection()) { + connection.Open(); + string command = @" + SELECT * + FROM Employee + WHERE AccountID = @AccountID; + "; + + MySqlCommand cmd = new MySqlCommand(command, connection); + cmd.Parameters.AddWithValue("@AccountID", AccountID); + + using (DbDataReader reader = await cmd.ExecuteReaderAsync()) { + while (await reader.ReadAsync()) { + if (reader == null) { break; } + int _id = reader.GetInt32("ID"); + int _accountid = reader.GetInt32("AccountID"); + int _companyid = reader.GetInt32("CompanyID"); + employees.Add(new Employee() { ID = _id, AccountID = _accountid, @@ -37,8 +99,8 @@ namespace BoredCareers.Services.DatabaseService { return employees.ToArray(); } - public async Task SetEmployee( Employee employee ) { - using( MySqlConnection connection = GetConnection() ) { + public async Task SetEmployee(Employee employee) { + using (MySqlConnection connection = GetConnection()) { connection.Open(); string command = @" @@ -51,7 +113,7 @@ namespace BoredCareers.Services.DatabaseService { CompanyID = @CompanyID; "; - MySqlCommand cmd = new MySqlCommand( command , connection); + MySqlCommand cmd = new MySqlCommand(command, connection); cmd.Parameters.AddWithValue("@ID", employee.ID); cmd.Parameters.AddWithValue("@AccountID", employee.AccountID); cmd.Parameters.AddWithValue("@CompanyID", employee.CompanyID);