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 GetCompany( int CompanyID ) { Company? company = null; using( MySqlConnection connection = GetConnection() ) { connection.Open(); string command = @" SELECT * FROM Company 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"); 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" ); company = new Company() { ID = _id, Name = _name, Email = _email, EmailVerified = _emailVerified, WebsiteURL = _websiteurl, LogoURL = _logourl, Phone = _phone, PostalCode = _postalcode, Country = _country, StateOrRegion = _state, City = _city, Description = _description }; } } } return company; } public async Task SetCompany( Company company ) { using (MySqlConnection connection = GetConnection()) { connection.Open(); string command = @" INSERT INTO Company (ID,Name,Email,EmailVerified,WebsiteURL,LogoURL,Phone,PostalCode,Country,StateOrRegion,City,Description) VALUES (@ID,@Name,@Email,@EmailVerified,@WebsiteURL,@LogoURL,@Phone,@PostalCode,@Country,@StateOrRegion,@City,@Description) ON DUPLICATE KEY UPDATE Name = @Name, Email = @Email, EmailVerified = @EmailVerified, WebsiteURL = @WebsiteURL, LogoURL = @LogoURL, Phone = @Phone, PostalCode = @PostalCode, Country = @Country, StateOrRegion = @StateOrRegion, City = @City, Description = @Description; Select LAST_INSERT_ID(); "; MySqlCommand cmd = new MySqlCommand(command, connection); cmd.Parameters.AddWithValue("@ID", company.ID); cmd.Parameters.AddWithValue("@Name", company.Name); cmd.Parameters.AddWithValue("@Email", company.Email); cmd.Parameters.AddWithValue("@EmailVerified", company.EmailVerified); cmd.Parameters.AddWithValue("@WebsiteURL", company.WebsiteURL); cmd.Parameters.AddWithValue("@LogoURL", company.LogoURL); cmd.Parameters.AddWithValue("@Phone", company.Phone); cmd.Parameters.AddWithValue("@PostalCode", company.PostalCode); cmd.Parameters.AddWithValue("@Country", company.Country); cmd.Parameters.AddWithValue("@StateOrRegion", company.StateOrRegion); cmd.Parameters.AddWithValue("@City", company.City); cmd.Parameters.AddWithValue("@Description", company.Description); object? result = await cmd.ExecuteScalarAsync(); int insertedId = result != null ? Convert.ToInt32(result) : 0; return insertedId; } } public async Task DeleteCompany( int CompanyID ) { using( MySqlConnection connection = GetConnection() ) { MySqlCommand cmd; connection.Open(); string command = @" DELETE FROM Company WHERE ID = @ID; "; cmd = new MySqlCommand( command, connection ); cmd.Parameters.AddWithValue("@ID", CompanyID); await cmd.ExecuteNonQueryAsync(); } } } }