From d8f6d606ae0cc9b385af0b0dba29d5908a741215 Mon Sep 17 00:00:00 2001 From: Derek Holloway Date: Tue, 15 Jul 2025 17:33:04 -0700 Subject: [PATCH] Cleanup for CompanyUser rename --- src/Server/Entities/Company.cs | 2 +- .../Services/DatabaseService/Employee.cs | 79 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 src/Server/Services/DatabaseService/Employee.cs diff --git a/src/Server/Entities/Company.cs b/src/Server/Entities/Company.cs index 40b57f4..e0a5a97 100644 --- a/src/Server/Entities/Company.cs +++ b/src/Server/Entities/Company.cs @@ -15,7 +15,7 @@ namespace BoredCareers.Entities { public string Description { get; set; } = ""; } - public class CompanyUser { + public class Employee { public int ID { get; set; } // PK public int AccountID { get; set; } // FK public int CompanyID { get; set; } // FK diff --git a/src/Server/Services/DatabaseService/Employee.cs b/src/Server/Services/DatabaseService/Employee.cs new file mode 100644 index 0000000..e317e89 --- /dev/null +++ b/src/Server/Services/DatabaseService/Employee.cs @@ -0,0 +1,79 @@ +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 GetEmployees( int CompanyID ) { + List employees = new List(); + using( MySqlConnection connection = GetConnection() ) { + connection.Open(); + string command = @" + SELECT * + FROM Employee + 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"); + 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 SetEmployee( Employee employee ) { + using( MySqlConnection connection = GetConnection() ) { + connection.Open(); + + string command = @" + INSERT INTO Employee + (ID,AccountID,CompanyID) + VALUES + (@ID,@AccountID,@CompanyID); + ON DUPLICATE KEY UPDATE + AccountID = @AccountID, + CompanyID = @CompanyID; + "; + + MySqlCommand cmd = new MySqlCommand( command , connection); + cmd.Parameters.AddWithValue("@ID", employee.ID); + cmd.Parameters.AddWithValue("@AccountID", employee.AccountID); + cmd.Parameters.AddWithValue("@CompanyID", employee.CompanyID); + + await cmd.ExecuteNonQueryAsync(); + } + } + + public async Task DeleteEmployee( int EmployeeID ) { + using( MySqlConnection connection = GetConnection() ) { + MySqlCommand cmd; + connection.Open(); + + string command = @" + DELETE FROM Employee WHERE ID = @ID; + "; + cmd = new MySqlCommand( command, connection ); + cmd.Parameters.AddWithValue("@ID", EmployeeID); + + await cmd.ExecuteNonQueryAsync(); + } + } + + } +}