using BoredCareers.Entities; using MySql.Data.MySqlClient; using System.Data; using System.Data.Common; using System.Text; namespace BoredCareers.Services.DatabaseService { public partial class DatabaseService { public async Task GetEmployee( int EmployeeID ) { Employee? employee = null; using( MySqlConnection connection = GetConnection() ) { await connection.OpenAsync(); string command = @" SELECT * FROM Employee LEFT JOIN Company ON Employee.CompanyID = Company.ID WHERE Employee.ID = @ID; "; MySqlCommand cmd = new MySqlCommand(command, connection); cmd.Parameters.AddWithValue("@ID", EmployeeID); using( DbDataReader reader = await cmd.ExecuteReaderAsync() ) { while( await reader.ReadAsync() ) { 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 _logo = Encoding.UTF8.GetString((byte[])reader["Logo"]); 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, Company = new Company { ID = _companyid, Name = _name, Email = _email, EmailVerified = _emailVerified, WebsiteURL = _websiteurl, Logo = _logo, Phone = _phone, PostalCode = _postalcode, Country = _country, StateOrRegion = _state, City = _city, Description = _description } }; } } } return employee; } public async Task GetEmployeesFromCompany(int CompanyID) { List employees = new List(); using (MySqlConnection connection = GetConnection()) { await connection.OpenAsync(); string command = @" SELECT * FROM Employee LEFT JOIN Company ON Employee.CompanyID = Company.ID WHERE Employee.CompanyID = @CompanyID; "; MySqlCommand cmd = new MySqlCommand(command, connection); cmd.Parameters.AddWithValue("@CompanyID", CompanyID); using (DbDataReader reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { 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 _logo = Encoding.UTF8.GetString((byte[])reader["Logo"]); 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, Company = new Company { ID = _companyid, Name = _name, Email = _email, EmailVerified = _emailVerified, WebsiteURL = _websiteurl, Logo = _logo, Phone = _phone, PostalCode = _postalcode, Country = _country, StateOrRegion = _state, City = _city, Description = _description } }); } } } return employees.ToArray(); } public async Task GetEmployeesFromAccount(int AccountID) { List employees = new List(); using (MySqlConnection connection = GetConnection()) { await connection.OpenAsync(); string command = @" SELECT * FROM Employee LEFT JOIN Company ON Employee.CompanyID = Company.ID WHERE Employee.AccountID = @AccountID; "; MySqlCommand cmd = new MySqlCommand(command, connection); cmd.Parameters.AddWithValue("@AccountID", AccountID); using (DbDataReader reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { 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 _logo = Encoding.UTF8.GetString((byte[])reader["Logo"]); 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, Company = new Company { ID = _companyid, Name = _name, Email = _email, EmailVerified = _emailVerified, WebsiteURL = _websiteurl, Logo = _logo, Phone = _phone, PostalCode = _postalcode, Country = _country, StateOrRegion = _state, City = _city, Description = _description } }); } } } return employees.ToArray(); } public async Task SetEmployee(Employee employee) { using (MySqlConnection connection = GetConnection()) { await connection.OpenAsync(); 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.Company.ID); await cmd.ExecuteNonQueryAsync(); } } public async Task DeleteEmployee( int EmployeeID ) { using( MySqlConnection connection = GetConnection() ) { MySqlCommand cmd; await connection.OpenAsync(); string command = @" DELETE FROM Employee WHERE ID = @ID; "; cmd = new MySqlCommand( command, connection ); cmd.Parameters.AddWithValue("@ID", EmployeeID); await cmd.ExecuteNonQueryAsync(); } } } }